[英]Movement in pygame when there shouldn't be
我正在使用pygame在Python 3.4中制作Brickbreaker游戏。
我编写了代码,以使您在按A或向左或D或向右时使操纵杆左右移动。 但是,当您移动鼠标或击打不应移动Paddle的键时,它仍然会移动。 我是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()
此代码使它不断将操纵杆向左移动
#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
尝试删除两条分配行,即“ self.paddle.rect.x = self.paddle.rect.x-1”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.