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

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
  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您一段外星人飞船大战的小游戏,以下是示例代码: ```python import pygame import random # 初始化Pygame pygame.init() # 设置屏幕大小和标题 screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Alien Invasion") # 加载背景图片 background_image = pygame.image.load("background.jpg").convert() # 加载飞船图片 player_image = pygame.image.load("player.png").convert_alpha() # 加载外星人图片 alien_image = pygame.image.load("alien.png").convert_alpha() # 加载子弹图片 bullet_image = pygame.image.load("bullet.png").convert_alpha() # 初始化玩家飞船位置和速度 player_x = 360 player_y = 480 player_speed = 5 # 初始化外星人位置和速度 alien_x = random.randint(0, 700) alien_y = random.randint(50, 150) alien_speed = 2 # 初始化子弹列表 bullets = [] # 初始化分数 score = 0 # 设置游戏循环标志 running = True # 游戏循环 while running: # 处理游戏事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: bullets.append([player_x + 32, player_y - 32]) # 获取键盘输入 keys = pygame.key.get_pressed() # 移动玩家飞船 if keys[pygame.K_LEFT]: player_x -= player_speed if keys[pygame.K_RIGHT]: player_x += player_speed if keys[pygame.K_UP]: player_y -= player_speed if keys[pygame.K_DOWN]: player_y += player_speed # 移动外星人 alien_y += alien_speed if alien_y > 600: alien_x = random.randint(0, 700) alien_y = random.randint(50, 150) # 移动子弹 for bullet in bullets: bullet[1] -= 10 # 绘制背景 screen.blit(background_image, (0, 0)) # 绘制玩家飞船 screen.blit(player_image, (player_x, player_y)) # 绘制外星人 screen.blit(alien_image, (alien_x, alien_y)) # 绘制子弹 for bullet in bullets: screen.blit(bullet_image, (bullet[0], bullet[1])) # 检测碰撞 for bullet in bullets: if bullet[1] < 0: bullets.remove(bullet) elif bullet[0] > alien_x and bullet[0] < alien_x + 64 and bullet[1] > alien_y and bullet[1] < alien_y + 64: bullets.remove(bullet) alien_x = random.randint(0, 700) alien_y = random.randint(50, 150) score += 1 # 绘制分数 font = pygame.font.Font(None, 32) text = font.render("Score: " + str(score), True, (255, 255, 255)) screen.blit(text, (20, 20)) # 更新屏幕 pygame.display.update() # 退出Pygame pygame.quit() ``` 以上代码实现了一个简单的外星人飞船大战小游戏。玩家可以使用键盘控制飞船移动,按下空格键发射子弹;外星人会不断下落,玩家需要尽可能击中它们并获得分数。游戏结束时,玩家可以按下关闭按钮退出游戏。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值