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

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



Hi,

For continuous movement, you deal with the keyboard a different way.  You don't check for a keyboard event, because the events only happens when you press the key down or let up on the key.  Instead, every time through your loop, you query the state of the keyboard.  It gives you back a Boolean list of all the keys that are currently down.  Then you can index into that list to see if a key is down.  

Here are the changes to the previous program (commented out old code dealing with key downs, added new):

                # 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

        keyPressedList = pygame.key.get_pressed()
        if keyPressedList[pygame.K_LEFT]:
            circleX = circleX - N_PIXELS_TO_MOVE
        if keyPressedList[pygame.K_RIGHT]:
            circleX = circleX + N_PIXELS_TO_MOVE
        if keyPressedList[pygame.K_UP]:
            circleY = circleY - N_PIXELS_TO_MOVE
        if keyPressedList[pygame.K_DOWN]:
            circleY = circleY + N_PIXELS_TO_MOVE


This approach allows you to hold down the down arrow and the right arrow keys (for example), and the circle will move diagonally.

Irv


On Nov 30, 2018, at 11:44 AM, Marc-Alexandre Espiaut <marc-alexandre.espiaut@xxxxxxxxx> wrote:

Hello,

Thank you for your answer.

I’m hoping the this was not a homework assignment (I am a teacher).

No, I am a PhD student. It was for my own sake.

But, because you showed your code, I’ll help you out.

Thank you, as I’ve been a student in the past, and might be a teacher also in a near future, I know asking for help always works better when you have something to show.

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

Thank you for your code. I’m quite surprised many of the thing I’ve done were redundant.

The code works fine, but now I’m wondering how to keep the circle moving while the key is pressed. I’ve tried to change some if into while but that only gets the program stuck.

The idea should be something like while event.type == pygame.KEYDOWN then update the position before drawing the screen. But I don’t see how, yet.

Thank you again, and best regards.

Marc-Alexandre Espiaut

On 11/30/18 5:28 PM, Irv Kalb wrote:



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.espiaut@xxxxxxxxx> 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