设为首页收藏本站

Crossin的编程教室

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

【Pygame 第8课】 火力全开

[复制链接]

0

主题

0

好友

28

积分

新手上路

Rank: 1

楼主
发表于 2018-1-23 15:59:55 |显示全部楼层
  1. # -*- coding:utf-8 -*-
  2. import random
  3. from sys import exit

  4. import pygame


  5. # 子弹类
  6. class Bullet:
  7.     def __init__(self):

  8.         self.x1 = 0
  9.         self.y1 = -1

  10.         self.x2 = -1
  11.         self.y2 = -1

  12.         self.x3 = 1
  13.         self.y3 = -1
  14.         self.image = pygame.image.load('bullet.png').convert_alpha()
  15.         # 默认不激活
  16.         self.active = False

  17.     def move(self):
  18.         # 激活状态下,向上移动
  19.         if self.active:
  20.             self.y1 -= 3
  21.             self.y2 -= 3
  22.             self.y3 -= 3
  23.         # 当飞出屏幕,就设为不激活
  24.         if self.y1 < 0:
  25.             self.active = False

  26.     def restart(self):
  27.         # 重置子弹位置
  28.         mouseX, mouseY = pygame.mouse.get_pos()
  29.         pygame.mouse.set_visible(False)
  30.         self.x1 = mouseX - self.image.get_width() / 2 - 10
  31.         self.y1 = mouseY - self.image.get_height() / 2

  32.         self.x2 = mouseX - self.image.get_width() / 2
  33.         self.y2 = mouseY - self.image.get_height() / 2

  34.         self.x3 = mouseX - self.image.get_width() / 2 + 10
  35.         self.y3 = mouseY - self.image.get_height() / 2
  36.         # 激活子弹
  37.         self.active = True


  38. # 敌机类
  39. class Enemy:
  40.     def __init__(self):
  41.         self.restart()
  42.         self.image = pygame.image.load('enemy.png').convert_alpha()

  43.     def move(self):
  44.         if self.y < 800:
  45.             self.y += self.speed
  46.         else:
  47.             self.restart()

  48.     def restart(self):
  49.         self.x = random.randint(50, 400)
  50.         self.y = random.randint(-200, -50)
  51.         self.speed = random.random() + 0.1


  52. pygame.init()
  53. screen = pygame.display.set_mode((450, 800), 0, 32)
  54. pygame.display.set_caption("天空,超爱你!")
  55. # 加载相关图像
  56. sky = 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. # 创建子弹对象和敌机对象
  70. enemy = Enemy()

  71. while True:
  72.     for event in pygame.event.get():
  73.         if event.type == pygame.QUIT:
  74.             pygame.quit()
  75.             exit()
  76.     screen.blit(sky, (0, 0))

  77.     # 发射间隔递减
  78.     interval_b -= 1
  79.     # 当间隔小于0时,激活一发子弹
  80.     if interval_b < 0:
  81.         bullets[index_b].restart()
  82.         # 重置间隔时间
  83.         interval_b = 100
  84.         # 子弹序号周期性递增
  85.         index_b = (index_b + 1) % count_b
  86.     # 判断每个子弹的状态
  87.     for b in bullets:
  88.         # 处于激活状态的子弹,移动位置并绘制
  89.         if b.active:
  90.             b.move()
  91.             screen.blit(b.image, (b.x1, b.y1))
  92.             screen.blit(b.image, (b.x2, b.y2))
  93.             screen.blit(b.image, (b.x3, b.y3))

  94.     # 敌机开始运动
  95.     enemy.move()
  96.     screen.blit(enemy.image, (enemy.x, enemy.y))
  97.     x, y = pygame.mouse.get_pos()
  98.     x -= plane.get_width() / 2
  99.     y -= plane.get_height() / 2
  100.     screen.blit(plane, (x, y))
  101.     pygame.display.update()
复制代码

微信截图_20180123155723.png (120.83 KB, 下载次数: 325)

微信截图_20180123155723.png

回复

使用道具 举报

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

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

GMT+8, 2024-5-9 04:15 , Processed in 0.016858 second(s), 26 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部