求助,为什么我这个代码写出来之后飞船只能左右移动不能上下移动(打外星人程序)

Ship.py

import pygame

class Ship():
"""管理飞船的大部分行为"""
def __init__(self,ai_settings,screen):
    """初始化飞船并设置其初始位置"""
    self.screen = screen
    self.ai_settings = ai_settings

    # 加载飞船图像并获取其外接矩形
    self.image = pygame.image.load("image/ship.bmp")
    self.rect = self.image.get_rect()
    self.screen_rect = screen.get_rect()

    #将每艘新飞船放在屏幕底部中央
    self.rect.centerx = self.screen_rect.centerx
    self.rect.centery = self.screen_rect.centery
    self.rect.bottom = self.screen_rect.bottom
    

    # 在飞船的属性center中存储小数值
    self.centerx = float(self.rect.centerx)
    self.centery = float(self.rect.centery)

    # 移动标志
    self.moving_right = False
    self.moving_left = False
    self.moving_up = False
    self.moving_down = False

def update(self):
    """根据移动标志调整飞船的位置"""
    # 更新飞船的center值,而不是rect
    """if self.moving_right and self.rect.right < self.screen_rect.right:
        self.centerx += self.ai_settings.ship_speed_factor
    if self.moving_left and self.rect.left > 0:
        self.centerx -= self.ai_settings.ship_speed_factor"""
    if self.moving_up and self.rect.top > 0:
        self.centery -= self.ai_settings.ship_speed_factor
    if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
        self.centery += self.ai_settings.ship_speed_factor

    # 根据self.center更新rect对象
    self.rect.centerx = self.centerx
    self.rect.centery = self.centery
    
def blitme(self):
    """在指定位置绘制飞船"""
    self.screen.blit(self.image,self.rect)

game_function.py

import sys

import pygame

def check_keydown_events(event, ship):
"""响应按键"""
if event.key == pygame.K_RIGHT:
    ship.moving_right = True
elif event.key == pygame.K_LEFT:
    ship.moving_left = True
elif event.key == pygame.K_UP:
    ship.moving_up == True
elif event.key == pygame.K_DOWN:
    ship.moving_down == True

def check_keyup_events(event,ship):
"""响应松开"""
if event.key == pygame.K_RIGHT:
    ship.moving_right = False
elif event.key == pygame.K_LEFT:
    ship.moving_left = False
elif event.key == pygame.K_UP:
    ship.moving_up == False
elif event.key == pygame.K_DOWN:
    ship.moving_down == False

def check_events(ship):
"""响应按键和鼠标事件"""
for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event,ship)

        elif event.type == pygame.KEYUP:
            check_keyup_events(event,ship)

def update_screen(ai_settings,screen,ship):
"""更新屏幕上的图像,并切换到新屏幕"""
# 每次循环时都重绘屏幕
screen.fill(ai_settings.bg_color)
ship.blitme()

# 让最近绘制的屏幕可见
pygame.display.flip()

alien_invasion.py

import pygame
"""from pygame.sprite import Group"""

from settings import Settings
from ship import Ship
import game_functions as gf

def run_game():
# 初始化pygame、设置和屏幕对象
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
    (ai_settings.screen_width,ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")

#建造一艘飞船
ship = Ship(ai_settings,screen)

"""# 创建一个用于存储子弹的编组
bullets = Group()"""

# 开始游戏的主循环
while True:
    gf.check_events(ship)
    ship.update()
    gf.update_screen(ai_settings,screen,ship)

run_game()

settings.py

class Settings():
"""储存《外星人入侵》的所有设置的类"""

def __init__(self):
    """初始化游戏的设置"""
    # 屏幕设置
    self.screen_width = 1200
    self.screen_height = 800
    self.bg_color = (230, 230, 230)

    # 飞船的设置
    self.ship_speed_factor = 1.5

    # 子弹设置
    self.bullet_speed_factor = 1
    self.bullet_width = 3
    self.bullet_height = 15
    self.bullet_color = 60,60,60

你可能感兴趣的:(求助,为什么我这个代码写出来之后飞船只能左右移动不能上下移动(打外星人程序))