鱼C论坛

 找回密码
 立即注册
查看: 2645|回复: 2

求大神看看 为什么我的小飞机只能向左移动 不能向右移动

[复制链接]
发表于 2019-5-3 15:34:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
感觉就是不能向右走 走不动
设置飞船的代码
  1. #  添加飞船图像

  2. import  pygame

  3. class Ship():
  4.     #  一个表示飞船的类

  5.     def __init__(self , ai_settings , screen):
  6.         """初始化飞船 并设置其初始位置"""
  7.         self.screen = screen    #  指定要将飞船绘制在哪(窗口位置)

  8.         self.ai_settings = ai_settings

  9.         #  加载飞船图像并获取外接矩形

  10.         self.image = pygame.image.load('666.bmp')   #  加载图像文件 返回图片对象

  11.         self.rect = self.image.get_rect()       #  获得图片对象的属性

  12.         self.screen_rect = screen.get_rect()


  13.         #  将每一艘飞船放在屏幕底部中央
  14.         """要将游戏元素居中 可设置相应的rect对象属性 """

  15.         self.rect.centerx = self.screen_rect.centerx

  16.         self.rect.bottom = self.screen_rect.bottom

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

  19.         #  移动标识(飞船不动时 标识为False)  玩家按下右箭头是 将标识设为True
  20.         #  松开是将标识设为False
  21.         self.moving_right = False

  22.         self.moving_left = False


  23.     def  update(self):
  24.         """根据移动标识调整飞船位置"""
  25.         #  更新飞船的center值 而不是rect
  26.         if self.moving_right:            #  飞船向右移动
  27.             self.center+=self.ai_settings.ship_speed_factor

  28.         if self.moving_left :             #  飞船向左移动
  29.             self.center-=self.ai_settings.ship_speed_factor


  30.         self.rect.centerx = self.center


  31.     def blitme(self):
  32.         """在指定位置绘制飞船"""

  33.         self.screen.blit( self.image , self.rect)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-3 15:36:17 | 显示全部楼层
这是响应事件的代码
  1. #  创建一个名为game_functions的新模块 将存储大量让游戏运行的函数
  2. import sys
  3. import pygame

  4. #  管理事件的函数

  5. def chenck_events(ship):
  6.     "响应按键和鼠标事件"
  7.     """ pygame.event.get() 获得所有事件的列表"""
  8.     for event in  pygame.event.get():
  9.         if event.type == pygame.QUIT:  #  鼠标点击关闭窗口事件
  10.             sys.exit()

  11.         if event.type == pygame.KEYDOWN:
  12.         #  每当用户按键时都将在pygame注册一个事件 我们需要指定要检查那些事件
  13.         #  每次按键都被注册为一个KEYDOWN事件
  14.             if event.key == pygame.K_RIGHT:     #  按下右箭头键
  15.                 # 向右移动飞船
  16.                 print("向右走")
  17.                 ship.moveing_right = True

  18.             elif event.key == pygame.K_LEFT:    #  按下左箭头
  19.                 print("向zuo走")
  20.                 ship.moving_left = True

  21.         if event.type == pygame.KEYUP:   #  (松开按键)
  22.             if event.key == pygame.K_RIGHT:     #  按下右箭头键
  23.                 # 向右移动飞船
  24.                 ship.moveing_right = False

  25.             elif event.key == pygame.K_LEFT:
  26.                 ship.moving_left = False


  27. #  更新屏幕

  28. def  update_screen(ai_settings , screen , ship):
  29.     """更新屏幕上的图像 并切换到新屏幕"""
  30.     screen.fill(ai_settings.bg_color)  #  设置屏幕背景

  31.     ship.blitme()    #  绘制一艘飞船

  32.     # 让最近绘制的屏幕可见
  33.     pygame.display.flip()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-3 15:38:53 | 显示全部楼层
这是执行代码
  1. #  创建Pygame窗口 以及响应用户输入

  2. #import sys   #  sys模块:该模块提供对解释器使用或维护的一些变量的访问
  3. """ 把定义的方法和变量存放在文件中"""

  4. import pygame     #  开发游戏所需要的功能

  5. from settings import Settings   #  引入设置类

  6. from  ship import Ship      #  引入飞船图像类

  7. from game_functions import *   #  引入运行函数模块

  8. def run_game():
  9.     #  初始化游戏 并创建一个屏幕对象

  10.     pygame.init()  #  检查pygame是否完整 能否正常提供给我们帮助
  11.     """检查电脑上一些需要的硬件调用接口,基础功能是否有问题
  12.         如果有 他会在程序运行之前反馈给你"""

  13.     ai_settings = Settings()  #  创建类变量

  14.     screen = pygame.display.set_mode(
  15.         (ai_settings.screen_width,ai_settings.screen_height) )  #  创建窗口(宽 高)

  16.     pygame.display.set_caption("外星人大战")    #  设置窗口标题

  17.     #  创建一艘飞船
  18.     ship = Ship(ai_settings , screen)

  19.     #  设置背景颜色 创建变量(三基色:红 绿 蓝   范围: 0—255)
  20.     #bg_color = (230,230,230)


  21.     #  开始游戏的主循环
  22.     while True:

  23.         #  监视键盘和鼠标事件
  24.         chenck_events(ship)
  25.         ship.update()  #  每次循环时调用飞船的方法

  26.         #  每次循环都重绘窗口
  27.         # 让最近绘制的屏幕可见
  28.         update_screen(ai_settings , screen , ship)



  29. run_game()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-15 15:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表