设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

【Pygame 第10课】 命中目标

[复制链接]

0

主题

0

好友

18

积分

新手上路

Rank: 1

楼主
发表于 2017-9-18 19:18:31 |显示全部楼层
本帖最后由 lsp84ch83 于 2017-9-18 19:22 编辑

以下是我对照论坛写的飞机代码,但是我的子弹距离很短,附件图片为运行效果
  1. # -*- coding:utf-8 -*-
  2. import pygame
  3. import random
  4. from sys import exit


  5. # 定义子弹的速度、运动方向
  6. class Bullet:
  7.     def __init__(self):
  8.         # 初始化成员变量:x,y,image
  9.         self.x = 0
  10.         self.y = -1
  11.         self.image = pygame.image.load('bullet.png').convert_alpha()
  12.         # 默认不激活
  13.         self.active = False

  14.     def move(self):
  15.         # 激活状态下,向上移动
  16.         if self.active:
  17.             self.y -= 0.8
  18.         # 当飞出屏幕,就设为不激活
  19.         if self.y < 0:
  20.             self.active = False

  21.     def restart(self):
  22.         # 重置子弹位置
  23.         mouseX, mouseY = pygame.mouse.get_pos()
  24.         self.x = mouseX - self.image.get_width() / 2
  25.         self.y = mouseY - self.image.get_height() / 2
  26.         # 激活子弹
  27.         self.active = True

  28. # 定义敌机的速度、出现位置
  29. class Enemy:
  30.     def restart(self):
  31.         # 重置敌机位置和速度
  32.         self.x = random.randint(50,400)
  33.         self.y = random.randint(-200,-50)
  34.         self.speed = random.random() + 0.1

  35.     def __init__(self):
  36.         # 初始化
  37.         self.restart()
  38.         self.image = pygame.image.load('enemy.png').convert_alpha()

  39.     def move(self):
  40.         if self.y < 800:
  41.             # 向下移动
  42.             self.y += self.speed
  43.         else:
  44.             # 重置
  45.             self.restart()


  46. # 检测子弹是否命中        
  47. def checkHit(enemy,bullet):
  48.     if (bullet.x > enemy.x and bullet.x < enemy.x + enemy.image.get_width()) and (
  49.                     bullet.y > enemy.y and bullet.y < enemy.y + enemy.image.get_height()):
  50.         enemy.restart()
  51.         bullet.active = False

  52. # 初始化
  53. pygame.init()
  54. screen = pygame.display.set_mode((450,800), 0, 32)
  55. pygame.display.set_caption('飞机')
  56. background = pygame.image.load('back.jpg').convert()
  57. plane = pygame.image.load('plane.png').convert_alpha()
  58. # 创建子弹的list
  59. bullets = []
  60. # 向list中添加 5 发子弹
  61. for i in range(5):
  62.     bullets.append(Bullet())
  63. # 子弹总数
  64. count_b = len(bullets)
  65. # 即将激活的子弹序号
  66. index_b = 0
  67. # 发射子弹的间隔
  68. interval_b = 0
  69. # 创建敌机的list
  70. enemies = []
  71. # 向list总添加 5 架敌机
  72. for i in range(5):
  73.     enemies.append(Enemy())

  74. # 游戏主体
  75. while True:
  76.     # 判断退出条件
  77.     for event in pygame.event.get():
  78.         if event.type == pygame.QUIT:
  79.             pygame.quit()
  80.             exit()
  81.     screen.blit(background,(0,0))
  82.     # 发射间隔递减
  83.     interval_b -= 1
  84.     # 当间隔小于0时,激活一发子弹
  85.     if interval_b < 0:
  86.         bullets[interval_b].restart()
  87.         # 重置间隔时间
  88.         interval_b = 200
  89.         # 子弹序号周期性递增
  90.         index_b = (index_b + 1) % count_b
  91.     # 判断每个子弹的状态
  92.     for b in bullets:
  93.         # 处于激活状态的子弹,移动位置并绘制
  94.         if b.active:
  95.             for e in enemies:
  96.                 checkHit(e,b)
  97.             b.move()
  98.             screen.blit(b.image, (b.x, b.y))
  99.     # 更新敌机位置,处理每架敌机的运动
  100.     for e in enemies:
  101.         e.move()
  102.         screen.blit(e.image,(e.x,e.y))
  103.     # 绘制子弹,数据来自其成员变量
  104.     x,y = pygame.mouse.get_pos()
  105.     x -= plane.get_width() / 2
  106.     y -= plane.get_height() / 2
  107.     screen.blit(plane,(x,y))
  108.     pygame.display.update()
复制代码

新建位图图像.jpg (48.94 KB, 下载次数: 293)

新建位图图像.jpg

回复

使用道具 举报

0

主题

0

好友

18

积分

新手上路

Rank: 1

沙发
发表于 2017-9-19 16:40:06 |显示全部楼层
本帖最后由 lsp84ch83 于 2017-9-19 16:59 编辑
crossin先生 发表于 2017-9-18 22:54
那你可以通过调节 speed 或者 interval_b 来修改。

我通过工具对比了下,代码里有个变量应用错了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-17 11:19 , Processed in 0.017641 second(s), 24 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部