Why is nothing drawn in PyGame at all?

You need to update the display. You are actually drawing on a Surface object. If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visibel, when the display is updated with either pygame.display.update() or pygame.display.flip(). See pygame.display.flip(): This will update the contents of … Read more

Why is my PyGame application not running at all?

Your application works well. However, you haven’t implemented an application loop: import pygame from pygame.locals import * pygame.init() win = pygame.display.set_mode((400,400)) pygame.display.set_caption(“My first game”) clock = pygame.time.Clock() run = True while run: # handle events for event in pygame.event.get(): if event.type == pygame.QUIT: run = False # update game objects # […] # clear display … Read more

Could not open resource file, pygame error: “FileNotFoundError: No such file or directory.”

The resource (image, font, sound, etc.) file path has to be relative to the current working directory. The working directory is possibly different from the directory of the python file. It is not enough to put the files in the same directory or sub directory. You also need to set the working directory. Alternatively, you … Read more

How do I rotate an image around its center using Pygame?

Short answer: Get the rectangle of the original image and set the position. Get the rectangle of the rotated image and set the center position through the center of the original rectangle. Return a tuple of the rotated image and the rectangle: def rot_center(image, angle, x, y): rotated_image = pygame.transform.rotate(image, angle) new_rect = rotated_image.get_rect(center = … Read more

How to detect collisions between two rectangular objects or images in pygame

Use pygame.Rect objects and colliderect() to detect the collision between the bounding rectangles of 2 objects or 2 images: rect1 = pygame.Rect(x1, y1, w1, h1) rect2 = pygame.Rect(x2, y2, w2, h2) if rect1.colliderect(rect2): # […] If you have to images (pygame.Surface objects), the bounding rectangle of can be get by get_rect(), where the location of … Read more

pygame installation issue in mac os

Here (OSX Mavericks) I got able to install this way: brew install sdl sdl_image sdl_mixer sdl_ttf portmidi pip install https://bitbucket.org/pygame/pygame/get/default.tar.gz (“default” branch is on commit e3ae850 right now) Source: https://bitbucket.org/pygame/pygame/issue/139/sdlh-not-found-even-thought-it-exists#comment-3822470 See this other StackOverflow question too: PyGame in a virtualenv on OS X with brew?

How to get keyboard input in pygame?

You can get the events from pygame and then watch out for the KEYDOWN event, instead of looking at the keys returned by get_pressed()(which gives you keys that are currently pressed down, whereas the KEYDOWN event shows you which keys were pressed down on that frame). What’s happening with your code right now is that … Read more

tech