Python,请帮助我,为什么我的直接移动

时间:2016-04-07 23:03:34

标签: python-3.x

我正在尝试使用pygame制作一个基本游戏,但我按下键后创建的矩形无法移动,请教我如何修复它,谢谢

import pygame, sys, time
from pygame.locals import *
class Tile:
    def __init__(self,surface):
        self.surface = surface
        self.x = 250
        self.y = 200
        self.position = (self.x, self.y)
        self.color = pygame.Color('red')
        self.speed = 5
        self.size = 30
        self.rect =        pygame.Rect(self.position[0],self.position[1],self.size,self.size)
    def draw(self):
        pygame.draw.rect(self.surface,self.color,self.rect)
    def moveUp(self):
        if self.rect.top < self.speed:
            self.speed = self.rect.top
        self.y = self.y - self.speed
    def moveDown(self):
        maxBottom = self.surface.get_height()
        if maxBottom - self.rect.bottom < self.speed:
            self.speed = maxBottom - self.rect.bottom
        self.y = self.y + self.speed
    def moveLeft(self):
        if self.rect.left < self.speed:
            self.speed = self.rect.left
        self.x = self.x - self.speed 
    def moveRight(self):
        maxRight = self.surface.get_width()
        if maxRight - self.rect.right < self.speed:
            self.speed = maxRight - self.rect.right
        self.x = self.x + self.speed
    def move(self,key):
        if key == K_w:
            self.moveUp()
        if key == K_s:
            self.moveDown()
        if key == K_a:
            self.moveLeft()
        if key == K_d:
            self.moveRight()
def main():
    pygame.init()
    pygame.key.set_repeat(20, 20)
    surfaceSize = (500, 400)
    windowTitle = 'Pong'
    frameDelay = 0.01
    surface = pygame.display.set_mode(surfaceSize, 0, 0)
    pygame.display.set_caption(windowTitle)
    gameOver = False
    tile = Tile(surface)
    tile.draw()
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN and not gameOver:
                tile.move(event.key)
    pygame.display.update()
    time.sleep(frameDelay)
main()

我正在尝试使用pygame制作一个基本游戏,但我按下键后创建的矩形无法移动,请教我如何修复它,谢谢

1 个答案:

答案 0 :(得分:0)

你只是更新你自己的x和y变量,而不是更新你的直接的PyGame变量,你可以在draw函数中最​​快地修复它,但它确实是一个更新操作,所以应该进入这些函数:

def draw(self):
    self.rect.top = self.y  # You need to update the position of the rect
    self.rect.left = self.x  # and not just your own x, y variables.
    pygame.draw.rect(self.surface,self.color,self.rect)

然后您更新显示的缩进已关闭,但无论如何您还需要额外的行:

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN and not gameOver:
            tile.move(event.key)
    surface.fill((0, 0, 0))  # Fill the screen with black to hide old rect
    tile.draw()  # Re-draw the tile in new position
    pygame.display.update() # Update the display

要修复的最后一件事,不要使用time.sleep作为帧延迟,使用PyGame的内置时钟功能。这是一个教程:https://www.youtube.com/watch?v=pNjSyBlbl_Q&nohtml5=False