- 帖子
- 12
- 精华
- 0
- 积分
- 66
- 阅读权限
- 20
- 注册时间
- 2018-4-9
- 最后登录
- 2018-8-28
|
我的飞机打中之后没有被重置是为什么?
# -*- coding: utf-8 -*-
import pygame, random
from sys import exit
class Bullet:
def __init__(self):
self.x = 0
self.y = -1
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 restart(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
class Enemy:
def restart(self):
self.x = random.randint(50, 800)
self.y = random.randint(-200, -50)
self.speed = random.random() + 0.1
def __init__(self):
self.restart()
self.image = pygame.image.load('enemy.png').convert_alpha()
def move(self):
if self.y < 800:
self.y += self.speed
else:
self.restart()
def checkHit(enemy, bullet):
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()):
enemy.restart()
bullet.active = False
pygame.init()
screen = pygame.display.set_mode((450, 800), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load('back.jpg').convert()
plane = pygame.image.load('plane.png').convert_alpha()
# 创建子弹的list
bullets = []
# 向list添加5发子弹
for i in range(5):
bullets.append(Bullet())
# 子弹总个数
count_b = len(bullets)
# 即将激发的子弹序号
index_b = 0
# 发射子弹的间隔
interval_b = 0
# 创建敌机的list
enemies = []
# 向list中添加5架敌机
for i in range(5):
enemies.append(Enemy())
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(background, (0, 0))
interval_b -= 1
if interval_b < 0:
bullets[index_b].restart()
interval_b = 100
index_b = (index_b + 1) % count_b
for b in bullets:
#处于激活状态的子弹,移动位置并绘制
if b.active:
b.move()
screen.blit(b.image, (b.x, b.y))
for b in bullets:
if b.active:
for e in enemies:
checkHit(e, b)
# 处理每架敌机的运动
for e in enemies:
e.move()
screen.blit(e.image, (e.x, e.y))
x, y = pygame.mouse.get_pos()
x -= plane.get_width() / 2
y -= plane.get_height() / 2
screen.blit(plane, (x, y))
pygame.display.update() |
|