我正在使用pygame在Python 3.4中制作一个Brickbreaker游戏。
我已经编写了代码,当您点击A或左,D或右时,左右手柄会左右移动。但是当您移动鼠标或按下不应移动桨的键时,它仍会移动。我是python的新手,我不知道导致代码的原因是什么,以及如何修复它,我的同学们也无法弄明白。
导致问题的原因是什么?如何解决?谢谢!
import sys, pygame, brick, ball, paddle, os
from pygame.locals import *
class BrickBreaker:
# Constructor of the basic game class.
# This constructor calls initialize and main_loop method.
def __init__(self):
self.initialize()
self.main_loop()
# Initialization method. Allows the game to initialize different parameters and load assets before the game runs
def initialize(self):
pygame.init()
pygame.key.set_repeat(1, 1) #This means when I hold A or D it repeats it for you so it doesn't move only a little
os.environ['SDL_VIDEO_CENTERED'] = '1'
self.width = 1280
self.height = 720
self.screen = pygame.display.set_mode((self.width, self.height))
self.caption = "Brick Breaker!" #This makes the top window say "Brick Breaker"
pygame.display.set_caption(self.caption)
self.framerate = 60
self.clock = pygame.time.Clock()
# Sprites
self.background_color = (255, 255, 255)
self.ball = ball.ball()
self.brick_list = pygame.sprite.Group()
self.paddle = paddle.paddle()
self.create_bricks()
self.sprites = pygame.sprite.Group()
self.sprites.add(self.ball,self.brick_list,self.paddle)
#The location of the sprites
self.paddle.rect.x = 520
self.paddle.rect.y = 650
self.ball.rect.x = 610
self.ball.rect.y = 595
def create_bricks(self):
offsetx = 90
offsety = 20
y = 37
for i in range(5):
x = 138
for j in range(8):
b = brick.brick()
b.rect.x = x*j + offsetx
b.rect.y = y*i + offsety
self.brick_list.add(b)
j+=1
i+=1
# main loop method keeps the game running
# calls the update and draw methods to keep the game alive.
def main_loop(self):
while True:
gametime = self.clock.get_time()
self.update(gametime)
self.draw(gametime)
self.clock.tick(self.framerate)
# Update method contains game update logic, such as updating the game
# variables, checking for collisions, gathering input, and
# playing audio.
def update(self, gametime):
self.ball.rect.y -= 10
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
# This allows you top move the paddle left and right
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
self.paddle.rect.x -= 15
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
self.paddle.rect.x += 15
# Stopping the paddle from going off screen
self.paddle.rect.x = self.paddle.rect.x - 1
if self.paddle.rect.x < 0:
self.paddle.rect.x = 0
self.paddle.rect.x = self.paddle.rect.x - 1
if self.paddle.rect.x > 1065:
self.paddle.rect.x = 1065
# Draw method, draws the current state of the game on the screen
def draw(self, gametime):
self.screen.fill(self.background_color)
self.sprites.draw(self.screen)
self.sprites.update()
pygame.display.flip()
if __name__ == "__main__":
game = BrickBreaker()
答案 0 :(得分:1)
此代码导致它不断向左移动球拍
#Stopping the paddle from going off screen
self.paddle.rect.x = self.paddle.rect.x - 1
if self.paddle.rect.x < 0:
self.paddle.rect.x = 0
self.paddle.rect.x = self.paddle.rect.x - 1
if self.paddle.rect.x > 1065:
self.paddle.rect.x = 1065
尝试删除两个分配行,即&#39; self.paddle.rect.x = self.paddle.rect.x - 1&#39;