使用if语句进行pygame常量运动

时间:2017-04-27 13:02:44

标签: python-2.7 pygame

我正在努力制作一款简单的游戏,但运动并不能正常运作 我希望它在按下按钮的同时不断移动但是尊重屏幕的边框,当前按钮将整体工作,但它会导致矩形断断续续,这是我的代码,

import pygame

pygame.init()

SD = pygame.display.set_mode((640,480))
x = 16
y = 16

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
            if y > 0:
                y -= 8
        if keys[pygame.K_a]:
            if x > 0:
                x -= 8
        if keys[pygame.K_s]:
            if y < 448:
                y += 8
        if keys[pygame.K_d]:
            if x < 608:
                x += 8
    SD.fill((255,255,255))
    pygame.draw.rect(SD, (255,0,0), (x,y, 30, 30))
    pygame.display.update()

2 个答案:

答案 0 :(得分:1)

pygame.event.get()仅在发生硬件事件(如按键)时才会返回事件。因此,当没有任何事情发生时,您的if keys[...]将不会被评估(按下的键事件不会重复)

将你的ifs向上移动一层并且它会在没有卡顿的情况下工作但是你必须减慢你的盒子的移动速度(sleep(0.1)会为这个例子做些但你可能想要更多的东西先进,因为你不想在你的画圈中睡觉)

答案 1 :(得分:1)

在pygame(以及大多数其他游戏引擎)中处理按键的方式是,只有在按下或释放按键时才能获得事件。你的角色运动看起来如此跳跃的原因是因为正在处理按键,就像在文本编辑器中按住键一样。如果按住键,会出现一封信,过了一会儿,你会收到很多重复的信。

你真正想要做的是为你获得按键事件时设置为True的每个键设置一个布尔值,当你获得一个键释放事件时设置为False(仔细查看process_events函数)

我已修改您的代码以执行此操作(以及之后我将解释的其他一些更改):

import pygame


class Game(object):
    def __init__(self):
        """
        Initialize our game.
        """
        # The initial position.
        self.x = 16
        self.y = 16

        # The keyboard state.
        self.keys = {
            pygame.K_w: False,
            pygame.K_a: False,
            pygame.K_s: False,
            pygame.K_d: False,
        }

        # Create the screen.
        self.SD = pygame.display.set_mode((640,480))

    def move_character(self):
        """
        Move the character according to the current keyboard state.
        """
        # Process vertical movement.
        if self.keys[pygame.K_w] and self.y > 0:
            self.y -= 1
        if self.keys[pygame.K_s] and self.y < 448:
            self.y += 1

        # Process horizontal movement.
        if self.keys[pygame.K_a] and self.x > 0:
            self.x -= 1
        if self.keys[pygame.K_d] and self.x < 608:
            self.x += 1

    def process_events(self):
        """
        Go through the pending events and process them.
        """
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            # If the event is a key press or release event, then register it
            # with our keyboard state.
            elif event.type == pygame.KEYDOWN:
                self.keys[event.key] = True
            elif event.type == pygame.KEYUP:
                self.keys[event.key] = False

    def draw(self):
        """
        Draw the game character.
        """
        self.SD.fill((255,255,255))
        pygame.draw.rect(self.SD, (255,0,0), (self.x, self.y, 30, 30))
        pygame.display.update()

    def run(self):
        while True:
            self.process_events()
            self.move_character()
            self.draw()


def main():
    pygame.init()

    game = Game()
    game.run()


# This just means that the main function is called when we call this file
# with python.
if __name__ == '__main__':
    main()

我所做的最大改变是将您的游戏转移到一个类中,以便您更好地访问函数中的变量。它还允许您将代码分区为不同的函数,以便于阅读。