按下按键时物体不动

Object not moving when keys are pressed

我刚开始学习 pygame 为项目创建一个简单的游戏。我有一个对象 "Rectangle",它有一个改变 x 和 y 位置的方法。但是,由于某种原因,该对象没有移动。我发现了一些例子,当按下一个键并使用新坐标绘制新对象时,x 和 y 会发生变化。但是,我不想那样做,而是想调用一个方法来移动那个矩形而不是创建一个新的矩形。最好的方法是什么?

我的代码:

import pygame

#init the pygame and create a screen
pygame.init()
screen = pygame.display.set_mode((1080,720))
done = False

#colours
blue = (0,0,255)
red = (255,0,0)
green = (0,255,0)
black = (0,0,0)
white = (255,255,255)
yellow = (255,255,0)

x = 540
y = 660

#starts the game clock
clock = pygame.time.Clock()

#class for all of the objects on the screen
class shape():
    def __init__(self, place, colour, x, y):
        self.place = place
        self.colour = colour
        self.x = x
        self.y = y

#class for a rectangle
class rectangle(shape):
    def __init__(self, place, colour, x, y, length, width):
        super().__init__(place, colour, x, y)
        self.length = length
        self.width = width

        pygame.draw.rect(screen, colour, pygame.Rect(x, y, length, width))

    def move_up(self):
        self.y = self.y + 3

    def move_down(self):
        self.y = self.y - 3

    def move_right(self):
        self.x = self.x + 3

    def move_left(self):
        self.x = self.x - 3


#main loop
while not done:

        #checking for game events
        for event in pygame.event.get():

                #quitting gamw when window is closed
                if event.type == pygame.QUIT:
                        done = True

        Rectangle = rectangle(screen, yellow, x, y, 30, 30)

        #detecting key presses
        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_UP]:Rectangle.move_up()
        if pressed[pygame.K_DOWN]:Rectangle.move_down()
        if pressed[pygame.K_LEFT]:Rectangle.move_left()
        if pressed[pygame.K_RIGHT]:Rectangle.move_right()

        pygame.display.update() 

        #controls FPS
        clock.tick(60)

你实际上做的是在每一帧的初始位置创建一个新的矩形。矩形的位置更新了,但是由于在下一帧创建了一个新的矩形,所以矩形似乎是不动的。

不要在其构造函数中绘制矩形,而是在class中添加一个draw方法 rectangle:

class rectangle(shape):
    def __init__(self, place, colour, x, y, length, width):
        super().__init__(place, colour, x, y)
        self.length = length
        self.width = width

    def move_up(self):
        self.y = self.y - 3
    def move_down(self):
        self.y = self.y + 3
    def move_right(self):
        self.x = self.x + 3
    def move_left(self):
        self.x = self.x - 3

    def draw(self):
        pygame.draw.rect(screen, self.colour, pygame.Rect(self.x, self.y, self.length, self.width))

在主应用循环之前创建一个矩形对象并在主应用循环中调用draw()方法:

# create rectangle object
Rectangle = rectangle(screen, yellow, x, y, 30, 30)

#main loop
while not done:

        #checking for game events
        for event in pygame.event.get():
                #quitting gamw when window is closed
                if event.type == pygame.QUIT:
                        done = True

        # detecting key presses and change location of the rectangle
        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_UP]:Rectangle.move_up()
        if pressed[pygame.K_DOWN]:Rectangle.move_down()
        if pressed[pygame.K_LEFT]:Rectangle.move_left()
        if pressed[pygame.K_RIGHT]:Rectangle.move_right()

        # clear display
        screen.fill(0)

        # draw rectangle
        Rectangle.draw()

        # update diesplay
        pygame.display.update() 

        #controls FPS
        clock.tick(60)