Pygame 简单实例(3)键盘事件

 
# coding=utf-8

import pygame
from pygame.locals import *
from sys import exit

background_image_file='front.png'

pygame.init()
pygame.display.set_caption("Sugar")
screen=pygame.display.set_mode((500,500),FULLSCREEN,32)
background=pygame.image.load(background_image_file).convert()

x,y=0,0
move_x,move_y=0,0
while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            exit()
        if event.type==KEYDOWN:
            if event.key==K_LEFT:
                move_x=-1
            elif event.key==K_RIGHT:
                move_x=1
            elif event.key==K_UP:
                move_y=-1
            elif event.key==K_DOWN:
                move_y=1
        elif event.type==KEYUP:
            move_x=0

            move_y=0

            #计算新的坐
    x+=move_x
    y+=move_y

    screen.fill((0,0,0))
    screen.blit(background,(x,y))
    pygame.display.update()
运行结果:
  可以左右上下移动图像。

 

你可能感兴趣的:(python练习,python,pygame)