Python / Pygame Tutorial 6 - Keyboard Events

preview_player
Показать описание
This is the next tutorial in my Python / Pygame series. In this video we look at how to move an object around on the screen using the arrow keys on the keyboard. This should be the start of a cool game we will start to make next tutorial! Please subscribe if you like the videos.

Download this code:

Code:
import pygame

black = (0,0,0)
white=(255,255,255)

x,y=0,0

moveX,moveY=0,0

gameLoop=True
while gameLoop:

gameLoop=False

moveX = -5

moveX = 5

moveY = -5

moveY = 5

moveX=0

moveX=0

moveY=0

moveY=0

x+=moveX
y+=moveY

Рекомендации по теме
Комментарии
Автор

For those who dont understand KEYDOWN and the KEYUP events, KEYDOWN is when you press down a key in your keyboard and KEYUP is when you lift your finger from the keyboard. Hope this helped..Like it so others can see as well :D

AzamFahmy
Автор

Thank you SO MUCH. You are seriously brilliant. I got stuck trying to make my char. move, and couldn't figure out how to keep the key pressed down. This is exactly what I needed. Thank you much. Really.

yothazme
Автор

do you know that this video changed my whole life !!
thanks man u r the best !

basselsak
Автор

It is shorthand for x = x + moveX. This basically means that you are taking the value of x, adding on the value of moveX, and assigning the result to x. Hope this helps.

Animations
Автор

Suggestion:

x+=moveX
y+=moveY
moveX =0
moveY=0

That way you don't need the KEYUP events to zero moveX and moveY...

geissonrodrigues
Автор

you can also minimize code with only define movX and movY together to 0 when pygame.event is happening directly... and catch key.get_pressed() function to get hold key pressed for 'loop moving' (inside the while loop, but outside the event loop)

DGDG
Автор

just to mention, you could also specify the colours in the object

SotraEngine
Автор

Hello! you do this in a very long way! A much easier way to do this would be to use pygame.key.get_pressed()

here is an example

    key = pygame.key.get_pressed()
    if key[pygame.K_LEFT]:
        x = x-10
    if key[pygame.K_RIGHT]:
        x = x+10
    if key[pygame.K_UP]:
        y = y-10
    if key[pygame.K_DOWN]:
        y = y+10
what happens here is if the key is being press you move however much you specify. instead of having to do keydown and keyup you can just use this!

jeffmarcus
Автор

When is next vid out, this is very helpful!

StianTutorials
Автор

Cherl, Try making the directory that is in the veriable trace back all the way back to your c drive.

obwonkonobe
Автор

Probably sometime during this week. Sorry that I haven't been making these tutorials for so long.

Animations
Автор

What do you mean by solid objects? Do you mean the objects cannot go inside other objects? If so, that is quite complicated, but I will probably cover it in a future tutorial.

Animations
Автор

thank you so much!!! this was so helpful...

mariaendres
Автор

Also, Great Tutorials! It really helped in understanding pygame.

Eliteruler
Автор

ty for the tutorials, hopefully you keep with it!

trico
Автор

Thanks very much. This video helped me alot

ghostbatter
Автор

Instead of " if (event.key==pygame.K_LEFT): ", do " if (event.key==pygame.K_a): " and the same for the different keys, e.g. K_RIGHT would be K_d. Hope this helps!

Animations
Автор

I'm a bit new to Python but it strikes me that if you insert ( at the beginning of the for loop ) the bit indicated:

for event in pygame.event.get():
moveX, MoveY = 0, 0 # This is the bit I'm talking about

you don't need the KEYUP section. Having said that I wanted to look at a video that showed me how to use KEYUP and yours provided me with what I needed. Thank you.
Also I tried using this without setting up the window cos I just wanted to print out the key values. It doesn't work without setting up the window. It had me puzzled for a while.

robhobbo
Автор

I cant get the rectangle to move, I typed the code exactly as you have it written. Everything loads fine, but the rectangle does not move.

Eliteruler
Автор

I FIGURED OUT WHAT WAS WRONG..I typed event.type instead of event.key in the nested if statements so it was not registering my key presses.

Eliteruler