python实现微信打飞机游戏

环境:Ubuntu 16.04 LTS 

          Python 2.7.11 +  Pygame + Pycharm

代码:

  1 # -*- coding: UTF-8 -*-
  2 import pygame, random
  3 from sys import exit
  4 
  5 
  6 class Plane:
  7     def restart(self):
  8         self.x = 200
  9         self.y = 600
 10 
 11     def __init__(self):
 12         self.restart()
 13         self.image = pygame.image.load(plane.png).convert_alpha()
 14 
 15     def move(self):
 16         x, y = pygame.mouse.get_pos()
 17         x -= self.image.get_width() / 2
 18         y -= self.image.get_height() / 2
 19         self.x = x
 20         self.y = y
 21 
 22 
 23 class Enemy:
 24     def start(self):
 25         self.speed = random.random() + 0.1
 26         self.x = random.randint(0, 450)
 27         self.y = 0
 28 
 29     def __init__(self):
 30         self.start()
 31         self.image = pygame.image.load(enemy.png).convert_alpha()
 32 
 33     def move(self):
 34         if self.y < 800:
 35             self.y += self.speed
 36         if self.y > 800:
 37             self.start()
 38 
 39 
 40 class Bullet:
 41     def __init__(self):
 42         self.x = 0
 43         self.y = 0
 44         self.image = pygame.image.load(bullet.png).convert_alpha()
 45         self.active = False
 46 
 47     def move(self):
 48         if self.active:
 49             self.y -= 3
 50         if self.y < 0:
 51             self.active = False
 52 
 53     def start(self):
 54         mouseX, mouseY = pygame.mouse.get_pos()
 55         self.x = mouseX - self.image.get_width() / 2
 56         self.y = mouseY - self.image.get_height() / 2
 57         self.active = True
 58 
 59 
 60 def Shoot(bullet, enemy):
 61     if (bullet.x > enemy.x and bullet.x < enemy.x + enemy.image.get_width()) and bullet.y > enemy.y and (
 62                 bullet.y < enemy.y + enemy.image.get_height()):
 63         bullet.active = False
 64         enemy.start()
 65         return True
 66     else:
 67         return False
 68 
 69 
 70 def Crash(plane, enemy):
 71     if (plane.x + 0.7 * plane.image.get_width() > enemy.x) and (
 72                     plane.x + 0.3 * plane.image.get_width() < enemy.x + enemy.image.get_width()) and (
 73                     plane.y + 0.7 * plane.image.get_height() > enemy.y) and (
 74                     plane.y + 0.3 * plane.image.get_height() < enemy.y + enemy.image.get_height()):
 75         return True
 76     else:
 77         return False
 78 
 79 
 80 pygame.init()
 81 screen = pygame.display.set_mode((450, 800), 0, 32)
 82 pygame.display.set_caption(World of plane craft)
 83 bg = pygame.image.load(bg.jpg).convert_alpha()
86 bullet = Bullet() 87 bullets = [] 88 for i in range(5): 89 bullets.append(bullet) 90 count_b = len(bullets) 91 index_b = 0 92 interval_b = 0 93 94 enemy = Enemy() 95 enemys = [] 96 for i in range(5): 97 enemys.append(enemy) 98 99 plane = Plane() 100 gameover = False 101 score = 0 102 font = pygame.font.Font(None, 32) 103 while True: 104 for event in pygame.event.get(): 105 if event.type == pygame.QUIT: 106 pygame.quit() 107 exit() 108 if gameover and event.type == pygame.MOUSEBUTTONUP: 109 plane.restart() 110 for e in enemys: 111 e.start() 112 for b in bullets: 113 b.active = False 114 score = 0 115 gameover = False 116 screen.blit(bg, (0, 0)) 117 if not gameover: 118 interval_b -= 1 119 if interval_b < 0: 120 bullets[index_b].start() 121 interval_b = 100 122 index_b = (index_b + 1) % count_b 123 for b in bullets: 124 if b.active: 125 for e in enemys: 126 if Shoot(b, e): 127 score += 100 128 b.move() 129 screen.blit(b.image, (b.x, b.y)) 130 131 for e in enemys: 132 if Crash(plane, e): 133 gameover = True 134 e.move() 135 screen.blit(e.image, (e.x, e.y)) 136 137 plane.move() 138 screen.blit(plane.image, (plane.x, plane.y)) 139 text = font.render("Socre: %d" % score, 1, (0, 0, 0)) 140 screen.blit(text, (0, 0)) 141 else: 142 text = font.render("Socre : %d" % score, 1, (0, 0, 0)) 143 screen.blit(text, (150, 300)) 144 text = font.render("Click mouse and restart", 1, (0, 0, 0)) 145 screen.blit(text, (100, 330)) 146 pygame.display.update()

运行所需图片:python实现微信打飞机游戏python实现微信打飞机游戏python实现微信打飞机游戏python实现微信打飞机游戏

python实现微信打飞机游戏

上一篇:微信公众平台java开发详解(工程代码+解析)


下一篇:钉钉、企业微信等9款协同办公产品互相厮杀,你赌谁赢?