[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] How to draw a simple circle and make it move with keyboard input?



Hello,

I’ve tried to make a simple pygame code.

I would like to draw a circle, and make it move with keyboard input.

I would like to achieve that with the most minimalist code possible, but so far I don’t manage to do such a thing.

Here is my non working code for reference:

#!/usr/bin/env python

import pygame
import pygame.draw

def main():
    pygame.init()

    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption('pygame test')
    screen.fill((0, 0, 0))

    s = pygame.Surface(screen.get_size(), pygame.SRCALPHA, 32)

    pygame.draw.circle(s, (255, 255, 255, 255), (0, 0), 50)

    screen.blit(s, (0, 0))
    pygame.display.flip()

    try:
        while True:
            event = pygame.event.wait()
            if event.type == pygame.QUIT:
                break
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE or event.unicode == 'q':
                    break
            pygame.display.flip()
    finally:
        pygame.quit()

if __name__ == '__main__':
    main()

I’ve looked at the pygame examples, but I really struggle to get a clear idea about this. So far aliens.py and chimp.py uses a pygame.Rect in some way… but as I’ve said, the code is not clear. I don’t understand how the binding of the Rect happens with the moving objects, and how updating is handled.

If anybody can enlighten me on that matter, I would be happy to read it.

Best regards,

Marc-Alexandre Espiaut