[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [pygame] key refrence~
black wrote:
> I searched Key and Event manual but only find event types. where does
> those key reference live ? for example I need a surface moving 10 pixel
> when left key is pressed.
black, now sounds like the time to really look at the pygame examples. many
of them include moving objects. look at the "simple aliens" one, which
moves a car left and right based on the arrow keys. also, make sure you
read and can understand the tutorial
Help! How do I Make It Move?
http://pygame.org/docs/tut/MoveIt.html
this tutorial starts a little slow, but it really should teach you what is
going on when you move an image. it will teach you how to build your own
classes for objects that move. the first examples with moving images even
do it by 10 pixels at a time.
as for the keys. you can find a list of the different key types here in the
docs, http://pygame.org/docs/ref/pygame_constants.html#keyboard
in any event, i've attached a way simple program that moves a block based
on the keyboard. you can easily replace the red block with any image you load.
#move an image from the keyboard
import pygame
from pygame.locals import *
def main():
pygame.init()
screen = pygame.display.set_mode((400, 300)) #create window
image = pygame.Surface((100, 30)) #create image
image.fill((255, 0, 0)) #fill with red
rect = image.get_rect()
rect.center = 200, 150 #center the position
while 1: #loop until the game ends
screen.fill(0, rect) #erase the old image
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN:
if event.key == K_LEFT:
rect = rect.move(-10, 0)
elif event.key == K_RIGHT:
rect = rect.move(10, 0)
elif event.key == K_UP:
rect = rect.move(0, -10)
elif event.key == K_DOWN:
rect = rect.move(0, 10)
screen.blit(image, rect) #draw the new image
pygame.display.flip() #update screen
pygame.time.wait(10) #slow it down a little
main()