Hi,

I'm hoping the this was not a homework assignment (I am a teacher).  But, 
because you showed your code, I'll help you out.

Most of your code was fine, but some things were in the wrong place.  

The basic idea is that there should be a main loop, and in that loop, you check 
for events (key down, etc.), adjust anything you need to adjust, then draw 
everything.

Hope this helps:

#!/usr/bin/env python

import pygame
import sys  # needed to quit gracefully
#import pygame.draw

N_PIXELS_TO_MOVE = 5  # how many pixels to move on each keypress
FRAMES_PER_SECOND = 30  # How fast to draw - this says draw every 30th of a 
second


def main():
    pygame.init()
    clock = pygame.time.Clock()   # added to allow things to slow down
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption('pygame test')

    # You don't need this.  screen.fill below does this for you.
    #s = pygame.Surface(screen.get_size(), pygame.SRCALPHA, 32)

    # Pick a starting X, Y location for the circle
    circleX = 0
    circleY = 0

    while True:
        # You don't need this
        #screen.blit(s, (0, 0))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()  # quit pygame
                sys.exit()   # quit the program
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE or event.unicode == 'q':
                    pygame.quit()
                    sys.exit()

                # If user presses left, right, up, or down, change the 
appropriate coordinate
                elif event.key == pygame.K_LEFT:
                    circleX = circleX - N_PIXELS_TO_MOVE
                elif event.key == pygame.K_RIGHT:
                    circleX = circleX + N_PIXELS_TO_MOVE
                elif event.key == pygame.K_UP:
                    circleY = circleY - N_PIXELS_TO_MOVE
                elif event.key == pygame.K_DOWN:
                    circleY = circleY + N_PIXELS_TO_MOVE

        # Now do all the drawing, first the background (fill)
        screen.fill((0, 0, 0))
        # Then draw the circle at the circle's coordinates
        pygame.draw.circle(screen, (255, 255, 255, 255), (circleX, circleY), 50)
                
        pygame.display.flip()  #  or:     pygame.display.update()
        clock.tick(FRAMES_PER_SECOND)  # make PyGame wait the correct amount


if __name__ == '__main__':
    main()



Irv


> On Nov 30, 2018, at 7:29 AM, Marc-Alexandre Espiaut 
> <marc-alexandre.espi...@posteo.eu> wrote:
> 
> 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
> 

Reply via email to