Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的Pygame角色没有持续移动_Python_Pygame - Fatal编程技术网

Python 我的Pygame角色没有持续移动

Python 我的Pygame角色没有持续移动,python,pygame,Python,Pygame,实际上,我正在pygame中构建一个名为“外星人入侵”的游戏,其中一艘飞船向外星人发射子弹。我在游戏的早期阶段,但我这里有一个问题。我的游戏正常工作时,我并没有添加连续移动,但现在它并没有像我预期的那样移动 我有三个PYTHON文件: 1:aline_.py import pygame import sys from settings import Settings from ship import Ship class AlienInvasion: ""&quo

实际上,我正在pygame中构建一个名为“外星人入侵”的游戏,其中一艘飞船向外星人发射子弹。我在游戏的早期阶段,但我这里有一个问题。我的游戏正常工作时,我并没有添加连续移动,但现在它并没有像我预期的那样移动

我有三个PYTHON文件:

1:aline_.py

import pygame
import sys
from settings import Settings
from ship import Ship


class AlienInvasion:
    """Overall class to control game assets and behaviour"""

    def __init__(self):
        pygame.init()
        self.setting = Settings()
        self.screen = pygame.display.set_mode((self.setting.screen_width, self.setting.screen_height))
        self.title = self.setting.game_title
        self.ship = Ship(self)


    def run_game(self):
        while True:
            self._check_events()
            self.ship.update()
            self._update_screen()

    def _check_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    self.ship.moving_right = True
                elif event.key == pygame.K_LEFT:
                    self.ship.moving_left = True

            elif event.type == pygame.K_UP:
                if event.key == pygame.K_RIGHT:
                    self.ship.moving_right = False
                elif event.key == pygame.K_LEFT:
                    self.ship.moving_left = False

    def _update_screen(self):
        self.screen.fill(self.setting.bg_color)
        self.ship.blit_me()

        pygame.display.flip()


if __name__ == '__main__':
    ai = AlienInvasion()
    ai.run_game()
2:settings.py

import pygame


class Settings:
    def __init__(self):
        self.screen_width = 900
        self.screen_height = 600
        self.bg_color = (230, 230, 230)
        self.game_title = pygame.display.set_caption('Alien Invasion')
3:ship.py

import pygame


class Ship:
    def __init__(self, ai_game):
        self.screen = ai_game.screen
        self.screen_rect = self.screen.get_rect()
        self.moving_right = False
        self.moving_left = False

        # Load the ship image and get its rect
        self.image = pygame.image.load('images/ship.png')
        self.rect = self.image.get_rect()

        # Start each new ship at the bottom center of the screen.
        self.rect.midbottom = self.screen_rect.midbottom

    def update(self):
        if self.moving_right:
            self.rect.x += 1
        if self.moving_left:
            self.rect.x -= 1

    def blit_me(self):
        # Draw the ship at its current location
        self.screen.blit(self.image, self.rect)

现在我建议改变登记按钮按下的方式(我不知道是否更好,但有人有类似的问题,这对他有帮助):


等等

我不确定问题出在哪里,但在函数
\u check\u events
中,将
elif event.type==pygame.K\u UP:
更改为
elif event.type==pygame.KEYUP:
,看看它是否解决了问题
pygame.K_UP
是向上键的枚举数常量。用于释放密钥的枚举数常量是
pygame.KEYUP
。它起作用了!
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        
    
    keys = pygame.key.get_pressed()
    if keys[pygame.K_q]:
        # [...]
    if keys[pygame.K_a]:
        # [...]
    if keys[pygame.K_LEFT]:
        # [...]