How do I scale a PyGame image (Surface) with respect to its center?

You missed to update the size of self.pixeltitlerect after the Surface has been scaled: self.pixeltitle = pg.transform.scale(self.pixeltitle,(xsize,ysize)) # size of surface has been changed get the new rectangle self.pixeltitlerect = self.pixeltitle.get_rect() self.pixeltitlerect.center = (250,120) self.screen.blit(self.pixeltitle,self.pixeltitlerect) Or even shorter (see pygame.Surface.get_rect()): self.pixeltitle = pg.transform.scale(self.pixeltitle,(xsize,ysize)) self.pixeltitlerect = self.pixeltitle.get_rect(center = (250,120)) self.screen.blit(self.pixeltitle,self.pixeltitlerect) Do not scale the original Surface. … Read more

How create a camera on PyOpenGL that can do “perspective rotations” on mouse movements?

You can use glRotate to rotate around an axis, by an amount which is given by the relative mouse movement (pygame.mouse.get_rel()): mouseMove = pygame.mouse.get_rel() glRotatef(mouseMove[0]*0.1, 0.0, 1.0, 0.0) But that won’t satisfy you, because the solution won’t work any more, if the mouse leaves the window. You’ve to center the mouse in the middle of … Read more

Pyinstaller with pygame

I searched a lot in the PyInstaller doc to get my own game working. I don’t know much about Ubuntu, but I got everything working in Windows and it should be very similar. The key here is to get PyInstaller to package your ressources (images, sounds, etc.) with your Python code. The best distribution is … Read more

How to scale the font size in pygame based on display resolution?

You’ve to scale the font manually. If a font is suited for a window with a height of 768, the you’ve to scale the font by current_height/768. e.g.: h = screen.get_height(); largeText = pygame.font.Font(‘digifaw.ttf’, int(450*h/768)) Note, you can use the pygame.freetype module: import pygame.freetype font = pygame.freetype.Font(‘digifaw.ttf’) and the method .render_to(), to render the font … Read more

How to render transparent text with alpha channel in PyGame?

I’m not sure why, but after some experimentation I have discovered that the surface created with font.render cannot have it’s alpha value changed. Just blit that surface to another surface, and change the alpha value of the new surface. textsurface=font.render(‘Test’, True, (0, 0, 0)) surface=pygame.Surface((100, 30)) surface.fill((255, 255, 255)) surface.blit(textsurface, pygame.Rect(0, 0, 10, 10)) surface.set_alpha(50) … Read more

How to add pause mode to Python program

Your code is quite large, but to pause the game is very general task: Add a pause state. Toggle the pause state on an certain event, e.g. when the p key is pressed. Skip the game processing if pauseis stated. e,.g. pause = False # pause state while True: ticks = pygame.time.get_ticks() for event in … Read more

How to make a circle move diagonally from corner to corner in pygame

I will offer a more general approach. Define a list of positions (circle_pos_list) that the object should visit on its way. Set the start position (circle_pos), the speed (circle_speed) and the index of the next target position in the list (next_pos_index). circle_pos_list = [(100, 100), (200, 150), (100, 150)] circle_pos = circle_pos_list[0] circle_speed = 5 … Read more

How can I drag more than 2 images in PyGame?

Create a list of 4 images: images = [img1, img2, img3, img4] Create a list of image rectangles: img_rects = [images[i].get_rect(topleft = (20+40*i, 20)) for i in range(len(images))] Use pygame.Rect.collidelist to find the image which is clicked: if e.type == pygame.MOUSEBUTTONDOWN: mouse_rect = pygame.Rect(e.pos, (1, 1)) current_image = mouse_rect.collidelist(img_rects) Draw the current_image: if e.type == … Read more

techhipbettruvabetnorabahisbahis forumu