|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
main.py:
import pygame
import sys
import traceback
import my_plane
import enemy
import bullet
from pygame.locals import *
pygame.init()
bg_size = weight, height = 400, 700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background = pygame.image.load('背景.png').convert()
def add_enemies(group, num):
for i in range(num):
e = enemy.Enemy(bg_size)
group.add(e)
def main():
me = my_plane.MyPlane(bg_size)
enemies = pygame.sprite.Group()
add_enemies(enemies, 15)
bullet1 = []
bullet1_index = 0
BULLET1_NUM = 4
for i in range(BULLET1_NUM):
bullet1.append(bullet.Bullet1(me.rect.midtop))
clock = pygame.time.Clock()
delay = 100
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
key_pressed = pygame.key.get_pressed()
if key_pressed[K_w] or key_pressed[K_UP]:
me.move_up()
if key_pressed[K_s] or key_pressed[K_DOWN]:
me.move_down()
if key_pressed[K_a] or key_pressed[K_LEFT]:
me.move_left()
if key_pressed[K_d] or key_pressed[K_RIGHT]:
me.move_right()
screen.blit(background, (0, 0))
if not(delay % 10):
bullet1[bullet1_index].reset(me.rect.midtop)
bullet1_index = (bullet1_index + 1) % BULLET1_NUM
for b in bullet1:
if b.active:
b.move()
screen.blit(b.image, b.rect)
enemy_hit = pygame.sprite.spritecollide(b, enemies, False)
if enemy_hit:
b.active = False
for e in enemy_hit:
e.active = False
for each in enemies:
if each.active:
each.move()
screen.blit(each.image, each.rect)
else:
each.reset()
enemies_down = pygame.sprite.spritecollide(me, enemies, False)
if enemies_down:
me.active = False
for e in enemies_down:
e.active = False
if me.active:
screen.blit(me.image, me.rect)
else:
running = False
pygame.display.flip()
clock.tick(60)
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
traceback.print_exc()
pygame.quit()
input()
bullet.py:
import pygame
class Bullet1(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('子弹.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
self.speed = 12
self.active = True
self.mask = pygame.mask.from_surface(self.image)
def move(self):
self.rect.top -= self.speed
if self.rect.top < 0:
self.active = False
def reset(self, position):
self.rect.left, self.rect.top = position
self.active = True
谁知道为什么子弹的射程这么短?
import pygame
import sys
import traceback
import my_plane
import enemy
import bullet
from pygame.locals import *
pygame.init()
bg_size = weight, height = 400, 700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background = pygame.image.load('背景.png').convert()
def add_enemies(group, num):
for i in range(num):
e = enemy.Enemy(bg_size)
group.add(e)
def main():
me = my_plane.MyPlane(bg_size)
enemies = pygame.sprite.Group()
add_enemies(enemies, 15)
bullet1 = []
bullet1_index = 0
BULLET1_NUM = 4
for i in range(BULLET1_NUM):
bullet1.append(bullet.Bullet1(me.rect.midtop))
clock = pygame.time.Clock()
delay = 100
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
key_pressed = pygame.key.get_pressed()
if key_pressed[K_w] or key_pressed[K_UP]:
me.move_up()
if key_pressed[K_s] or key_pressed[K_DOWN]:
me.move_down()
if key_pressed[K_a] or key_pressed[K_LEFT]:
me.move_left()
if key_pressed[K_d] or key_pressed[K_RIGHT]:
me.move_right()
screen.blit(background, (0, 0))
if not (delay % 10):
for b in bullet1:
if not b.active:
b.reset(me.rect.midtop)
break
for b in bullet1:
if b.active:
b.move()
screen.blit(b.image, b.rect)
enemy_hit = pygame.sprite.spritecollide(b, enemies, False)
if enemy_hit:
b.active = False
for e in enemy_hit:
e.active = False
for each in enemies:
if each.active:
each.move()
screen.blit(each.image, each.rect)
else:
each.reset()
enemies_down = pygame.sprite.spritecollide(me, enemies, False)
if enemies_down:
me.active = False
for e in enemies_down:
e.active = False
if me.active:
screen.blit(me.image, me.rect)
else:
running = False
pygame.display.flip()
clock.tick(60)
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
traceback.print_exc()
pygame.quit()
input()
import pygame
class Bullet1(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('子弹.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
self.speed = 12
self.active = True
self.mask = pygame.mask.from_surface(self.image)
def move(self):
self.rect.top -= self.speed
if self.rect.top < 0:
self.active = False
def reset(self, position):
self.rect.left, self.rect.top = position
self.active = True
|
|