马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
主函数:import pygame
import random
import sys
import traceback
import me
import bullet
from pygame.locals import *
#初始化pygame
pygame.init()
#定义场景
bg_size = width, height = 512, 768
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
#载入背景
background = []
background.extend([
pygame.image.load(r"C:\Users\WL\OneDrive\桌面\wyh.文件夹\Python.py\IDLE-2.py\pygame\飞机大战素材\img_bg_level_1.jpg").convert(),
pygame.image.load(r"C:\Users\WL\OneDrive\桌面\wyh.文件夹\Python.py\IDLE-2.py\pygame\飞机大战素材\img_bg_level_2.jpg").convert(),
pygame.image.load(r"C:\Users\WL\OneDrive\桌面\wyh.文件夹\Python.py\IDLE-2.py\pygame\飞机大战素材\img_bg_level_3.jpg").convert(),
pygame.image.load(r"C:\Users\WL\OneDrive\桌面\wyh.文件夹\Python.py\IDLE-2.py\pygame\飞机大战素材\img_bg_level_4.jpg").convert(),
pygame.image.load(r"C:\Users\WL\OneDrive\桌面\wyh.文件夹\Python.py\IDLE-2.py\pygame\飞机大战素材\img_bg_level_5.jpg").convert()
])
#载入音乐
pygame.mixer.music.load(r"C:\Users\WL\OneDrive\桌面\wyh.文件夹\Python.py\IDLE-2.py\pygame\飞机大战素材\bgm.ogg")
pygame.mixer.music.set_volume(0.2)
overenemy_sound = pygame.mixer.Sound(r"C:\Users\WL\OneDrive\桌面\wyh.文件夹\Python.py\IDLE-2.py\pygame\飞机大战素材\bomb.wav")
overenemy_sound.set_volume(0.2)
game_over = pygame.mixer.Sound(r"C:\Users\WL\OneDrive\桌面\wyh.文件夹\Python.py\IDLE-2.py\pygame\飞机大战素材\gameover.wav")
game_over.set_volume(0.5)
#主函数
def main():
pygame.mixer.music.play(-1)
clock = pygame.time.Clock()
running = True
level = 0
background_y = 0
speed = 1
delay = 100
hero = me.Myplane(bg_size)
bullet1 = []
bullet1_index = 0
BULLET_NUM = 4
for i in range(BULLET_NUM):
bullet1.append(bullet.Bullet1())
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pressed = pygame.key.get_pressed()
if pressed[K_w]:
hero.move_up()
elif pressed[K_s]:
hero.move_down()
elif pressed[K_a]:
hero.move_left()
elif pressed[K_d]:
hero.move_right()
background_y += speed
if background_y > height:
background_y = 0
delay -= 1
if delay == 0:
delay = 100
if not(delay % 20):
bullet1[bullet1_index].reset(hero.rect.midtop)
bullet1_index = (bullet1_index + 1) % BULLET_NUM
screen.blit(background[level], (0, background_y - height))
screen.blit(background[level], (0, background_y))
screen.blit(hero.image, hero.rect)
calc_energy = 100 - hero.energy
pygame.draw.rect(screen, (255, 255, 255), (495, height / 2 - 148, 10, 300))
if hero.energy <= 20:
COLOR = (255, 0, 0)
elif hero.energy <= 50:
COLOR = (255, 255, 0)
else:
COLOR = (0, 255, 0)
pygame.draw.rect(screen, COLOR, (495, 106 + calc_energy * 3 + 130, 10, 300 - calc_energy * 3))
for i in bullet1:
if i.active:
i.move()
screen.blit(i.image, i.rect)
pygame.display.flip()
clock.tick(50)
if hero.energy <= 0:
pygame.mixer.music.stop()
pygame.mixer.stop()
game_over.play()
pygame.time.delay(3000)
pygame.quit()
sys.exit()
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
hero模块:import pygame
class Myplane(pygame.sprite.Sprite):
energy = 100
def __init__(self, bg_size):
pygame.sprite.Sprite.__init__(self)
self.energy = Myplane.energy
self.image = pygame.image.load(r"C:\Users\WL\OneDrive\桌面\wyh.文件夹\Python.py\IDLE-2.py\pygame\飞机大战素材\hero.png").convert()
self.rect = self.image.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.rect.left, self.rect.top = (self.width - self.rect.width) // 2, (self.height - self.rect.height) - 60
self.speed = 10
def move_up(self):
if self.rect.top > 0:
self.rect.top -= self.speed
def move_down(self):
if self.rect.bottom < self.height:
self.rect.bottom += self.speed
def move_left(self):
if self.rect.left > 0:
self.rect.left -= self.speed
def move_right(self):
if self.rect.right < self.width:
self.rect.right += self.speed
bullet模块:import pygame
class Bullet1(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r"C:\Users\WL\OneDrive\桌面\wyh.文件夹\Python.py\IDLE-2.py\pygame\飞机大战素材\bullet_9.png").convert_alpha()
self.rect = self.image.get_rect()
self.speed = 13
self.active = True
def reset(self, position):
self.rect.left, self.rect.top = position
self.active = True
def move(self):
if self.rect.bottom > 0:
self.rect.top -= self.speed
else:
self.active = False
效果:
游戏中1
游戏中2
|