簡體   English   中英

pygame中不應有的運動

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM