求大佬指点!请问python从入门到实践飞船游戏中怎么使飞船上下移动啊?运行时可以左右移动就是不能上下移动。。。

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('images/ship.png')
        self.rect=self.image.get_rect()#rect表示矩形对象,此处获取了飞船的外接矩形
        self.screen_rect=screen.get_rect()

        #飞船放中央
        self.rect.centerx=self.screen_rect.centerx
        #center,centerx,centery分别表示放在x和y,x,y中央
        self.rect.centery=self.screen_rect.centery

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

        self.moving_right=False
        self.moving_left=False
        self.moving_top=False
        self.moving_bottom=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_top and self.rect.top<self.screen_rect.top:
            self.centery-=self.ai_settings.ship_speed_factor
        if self.moving_bottom and self.rect.bottom>0:
            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)#绘制这艘船

alien_invasion.py

import pygame#开发游戏所需功能

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

def run_game():
    pygame.init()#初始化背景设置
    ai_settings=Settings()
    screen=pygame.display.set_mode(#创建显示窗口
            (ai_settings.screen_width,ai_settings.screen_height))
    screen=pygame.display.set_mode((1200,700))#指定游戏尺寸宽和高
    pygame.display.set_caption("Alien Invasion")

    ship=Ship(ai_settings,screen)

    while True:#检测是否正常运行
        gf.check_events(ship)
        ship.update()#飞船位置更新
        gf.update_screen(ai_settings,screen,ship)


run_game()#初始化游戏并开始循环

game_functions.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_DOWN:
        ship.moving_BOTTOM=True
    elif event.key==pygame.K_UP:
        ship.moving_TOP=True
def check_keyup_events(event,ship):
    """响应松开"""
    if event.key==pygame.K_RIGHT:
        ship.moving_right=False
    if event.key==pygame.K_LEFT:
        ship.moving_left=False
    if event.key==pygame.K_DOWN:
        ship.moving_bottom=False
    if event.key==pygame.K_UP:
        ship.moving_top=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()

settings.py

class Settings():
    "储存类"
    def __init__(self):
        """初始化游戏设置"""
        #屏幕设置
        self.screen_width=1200
        self.screen_height=700
        self.bg_color=(230,230,230)#设置背景色,RGB颜色定义
        self.ship_speed_factor=0.5

2个回答

我猜是大小写
elif event.key==pygame.K_DOWN:
ship.moving_BOTTOM=True
elif event.key==pygame.K_UP:
ship.moving_TOP=True

            if event.key==pygame.K_DOWN:
    ship.moving_bottom=False
if event.key==pygame.K_UP:
    ship.moving_top=False


               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_top and self.rect.top<self.screen_rect.top:
        self.centery-=self.ai_settings.ship_speed_factor
    if self.moving_bottom and self.rect.bottom>0:
        self.centery+=self.ai_settings.ship_speed_factor
lanyi_ly
lanyi_ly 非常感谢大佬,没想到这么快就有回复了
9 个月之前 回复
lanyi_ly
lanyi_ly 谢谢啦,其实还有一个地方,就是top的判断条件的是大于而不是小于。
9 个月之前 回复

self. rect. top>self. screen_rect. top

self. rect. bottom<self. screen_rect. bottom

屏幕左上角是原点,往下和往右是y和x增大

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐