python实现微信打飞机游戏

时间:2023-01-28 08:22:08

环境:Ubuntu 16.04 LTS

Python 2.7.11 +  Pygame + Pycharm

代码:

 # -*- coding: UTF-8 -*-
import pygame, random
from sys import exit class Plane:
def restart(self):
self.x = 200
self.y = 600 def __init__(self):
self.restart()
self.image = pygame.image.load('plane.png').convert_alpha() def move(self):
x, y = pygame.mouse.get_pos()
x -= self.image.get_width() / 2
y -= self.image.get_height() / 2
self.x = x
self.y = y class Enemy:
def start(self):
self.speed = random.random() + 0.1
self.x = random.randint(0, 450)
self.y = 0 def __init__(self):
self.start()
self.image = pygame.image.load('enemy.png').convert_alpha() def move(self):
if self.y < 800:
self.y += self.speed
if self.y > 800:
self.start() class Bullet:
def __init__(self):
self.x = 0
self.y = 0
self.image = pygame.image.load('bullet.png').convert_alpha()
self.active = False def move(self):
if self.active:
self.y -= 3
if self.y < 0:
self.active = False def start(self):
mouseX, mouseY = pygame.mouse.get_pos()
self.x = mouseX - self.image.get_width() / 2
self.y = mouseY - self.image.get_height() / 2
self.active = True def Shoot(bullet, enemy):
if (bullet.x > enemy.x and bullet.x < enemy.x + enemy.image.get_width()) and bullet.y > enemy.y and (
bullet.y < enemy.y + enemy.image.get_height()):
bullet.active = False
enemy.start()
return True
else:
return False def Crash(plane, enemy):
if (plane.x + 0.7 * plane.image.get_width() > enemy.x) and (
plane.x + 0.3 * plane.image.get_width() < enemy.x + enemy.image.get_width()) and (
plane.y + 0.7 * plane.image.get_height() > enemy.y) and (
plane.y + 0.3 * plane.image.get_height() < enemy.y + enemy.image.get_height()):
return True
else:
return False pygame.init()
screen = pygame.display.set_mode((450, 800), 0, 32)
pygame.display.set_caption('World of plane craft')
bg = pygame.image.load('bg.jpg').convert_alpha()
bullet = Bullet()
bullets = []
for i in range(5):
bullets.append(bullet)
count_b = len(bullets)
index_b = 0
interval_b = 0 enemy = Enemy()
enemys = []
for i in range(5):
enemys.append(enemy) plane = Plane()
gameover = False
score = 0
font = pygame.font.Font(None, 32)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if gameover and event.type == pygame.MOUSEBUTTONUP:
plane.restart()
for e in enemys:
e.start()
for b in bullets:
b.active = False
score = 0
gameover = False
screen.blit(bg, (0, 0))
if not gameover:
interval_b -= 1
if interval_b < 0:
bullets[index_b].start()
interval_b = 100
index_b = (index_b + 1) % count_b
for b in bullets:
if b.active:
for e in enemys:
if Shoot(b, e):
score += 100
b.move()
screen.blit(b.image, (b.x, b.y)) for e in enemys:
if Crash(plane, e):
gameover = True
e.move()
screen.blit(e.image, (e.x, e.y)) plane.move()
screen.blit(plane.image, (plane.x, plane.y))
text = font.render("Socre: %d" % score, 1, (0, 0, 0))
screen.blit(text, (0, 0))
else:
text = font.render("Socre : %d" % score, 1, (0, 0, 0))
screen.blit(text, (150, 300))
text = font.render("Click mouse and restart", 1, (0, 0, 0))
screen.blit(text, (100, 330))
pygame.display.update()

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