Events 带Pygame的突破游戏-处理事件

Events 带Pygame的突破游戏-处理事件,events,pygame,key,breakout,Events,Pygame,Key,Breakout,我目前正在尝试用Pygame创建一个简单版本的游戏Breakout。问题是我想让我的球棒在屏幕上移动,为此我需要处理一些事件,以及当你按下右/左箭头时,球棒会立即向右/向左移动的事实。然而,我的代码不起作用;每当我按键时,球棒的长度都在增加,而不是简单地移动。我已经浏览了很多代码和示例,但我还是迷路了 这是我的密码: import pygame, sys pygame.init() width, height = 800, 600 screen = pygame.display.set_mo

我目前正在尝试用Pygame创建一个简单版本的游戏Breakout。问题是我想让我的球棒在屏幕上移动,为此我需要处理一些事件,以及当你按下右/左箭头时,球棒会立即向右/向左移动的事实。然而,我的代码不起作用;每当我按键时,球棒的长度都在增加,而不是简单地移动。我已经浏览了很多代码和示例,但我还是迷路了

这是我的密码:

import pygame, sys

pygame.init()

width, height = 800, 600
screen = pygame.display.set_mode([width, height])
bat_speed = 30
bat = pygame.image.load('bat.png').convert()
batrect = bat.get_rect()

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:                        
                batrect = batrect.move(-bat_speed, 0)     
                if (batrect.left < 0):                           
                    batrect.left = 0      
            if event.key == pygame.K_RIGHT:                    
                batrect = batrect.move(bat_speed, 0)
                if (batrect.right > width):                            
                    batrect.right = width

    screen.blit(bat, batrect)
    pygame.display.flip()

pygame.quit()
import pygame,sys
pygame.init()
宽度,高度=800600
screen=pygame.display.set_模式([宽度,高度])
蝙蝠速度=30
bat=pygame.image.load('bat.png').convert()
batrect=bat.get_rect()
而1:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
sys.exit()
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
batrect=batrect.移动(-bat_速度,0)
如果(batrect.left<0):
batrect.left=0
如果event.key==pygame.K_RIGHT:
batrect=batrect.移动(bat_速度,0)
如果(batrect.right>宽度):
batrect.right=宽度
屏幕。blit(bat,batrect)
pygame.display.flip()
pygame.quit()

当你在屏幕上闪动一些东西时,它会留在那里。现在的情况是,你在不同的位置快速移动球棒,使得球棒的宽度看起来增加了。简单的解决方法是在你画球棒之前清除屏幕

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:                        
                batrect = batrect.move(-bat_speed, 0)     
                if (batrect.left < 0):                           
                    batrect.left = 0      
            elif event.key == pygame.K_RIGHT:                    
                batrect = batrect.move(bat_speed, 0)
                if (batrect.right > width):                            
                    batrect.right = width

    screen.fill((0, 0, 0))  # This will fill the screen with a black color.
    screen.blit(bat, batrect)
    pygame.display.flip()
而1:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
sys.exit()
elif event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
batrect=batrect.移动(-bat_速度,0)
如果(batrect.left<0):
batrect.left=0
elif event.key==pygame.K_RIGHT:
batrect=batrect.移动(bat_速度,0)
如果(batrect.right>宽度):
batrect.right=宽度
屏幕。填充((0,0,0))#这将用黑色填充屏幕。
屏幕。blit(bat,batrect)
pygame.display.flip()
另外,如果您不想检查每个条件,请使用
elif
而不是多个
if
,就像我上面所做的那样