import pygame
import time
pygame.init()
ck = pygame.display.set_mode((800,600)) # 游戏窗口
background = pygame.image.load("images/gui/g1.jpg")
pygame.display.set_caption("雷霆战机") # 窗口名
clock = pygame.time.Clock()
start_ck = pygame.Surface(ck.get_size()) # 充当开始菜单界面的画布
start_ck2 = pygame.Surface(ck.get_size()) # 充当第一关的画布界面暂时占位
start_ck = start_ck.convert()
start_ck2 = start_ck2.convert()
start_ck=pygame.image.load("images/gui/g1.jpg")
start_ck2.fill((0,255,0))
# 加载各个素材图片 并且赋予变量名
i1 = pygame.image.load("images/gui/a1.png")
i1.convert()
i11 = pygame.image.load("images/gui/a2.png")
i11.convert()
i2 = pygame.image.load("images/gui/b2.png")
i2.convert()
i21 = pygame.image.load("images/gui/b1.png")
i21.convert()
i3 = pygame.image.load('images/gui/m2.png')
i3.convert()
i31 = pygame.image.load('images/gui/m1.png')
i31.convert()
bg = pygame.image.load('images/gui/bj.png')
bg.convert()
# 以下为选择开始界面鼠标检测结构。
n1 = True
while n1:
clock.tick(30)
buttons = pygame.mouse.get_pressed()
x1, y1 = pygame.mouse.get_pos()
if x1 >= 210 and x1 <= 500 and y1 >= 260 and y1 <=300:
start_ck.blit(i11, (200, 240))
if buttons[0]:
n1 = False
elif x1 >= 227 and x1 <= 555 and y1 >= 381 and y1 <=447:
start_ck.blit(i21, (200, 360))
if buttons[0]:
pygame.quit()
exit()
elif x1 >= 227 and x1 <= 555 and y1 >= 501 and y1 <=567:
start_ck.blit(i31, (200, 480))
else:
start_ck.blit(i1, (200, 240))
start_ck.blit(i2, (200, 360))
start_ck.blit(i3, (200, 480))
ck.blit(start_ck,(0,0))
pygame.display.update()
# 监听事件
for event in pygame.event.get():
# 判断事件类型是否是退出事件
if event.type == pygame.QUIT:
print("游戏退出...")
pygame.quit()
exit()
ck.blit(start_ck2,(0,0))
pygame.display.update()
n2 = True
while n2:
from pygame import locals
from sys import exit
from pygame.sprite import Sprite, Group
import random, time, math
class BGSprite(Sprite): # 背景精灵
def __init__(self, imagepath):
super().__init__()
self.image = pygame.image.load(imagepath)
self.rect = self.image.get_rect()
class EnemySprite(Sprite): # 敌方战机
def __init__(self, imagepath, pos, speed, screen, movetype, bullettype, hp):
super().__init__()
self.image = pygame.image.load(imagepath)
self.rect = self.image.get_rect()
self.rect.x = pos[0]
self.rect.y = pos[1]
self.speed = speed
self.xspeed = random.randint(0, speed + 1) * movetype
self.timer = 0
self.timer2 = 15
self.bulletgroup = Group()
self.screen = screen
self.bullettype = bullettype
self.hp = hp
def update(self):
self.bulletgroup.update()
self.bulletgroup.draw(self.screen)
if self.bullettype == 0:
self.timer += 0.01
if self.timer > 1: # print("敌机发射子弹")
bullet = EnemyBulletSprite("images/bullet/enemy_bullet1.png",
[self.rect.x + 40, self.rect.y + 39.5],
-2, 0)
bullet2 = EnemyBulletSprite("images/bullet/enemy_bullet1.png",
[self.rect.x + 40, self.rect.y + 39.5],
-2, -1)
bullet3 = EnemyBulletSprite("images/bullet/enemy_bullet1.png",
[self.rect.x + 40, self.rect.y + 39.5],
-2, 1)
self.bulletgroup.add(bullet)
self.bulletgroup.add(bullet2)
self.bulletgroup.add(bullet3)
self.timer = 0
else:
self.timer += 0.01
self.timer2 += 0.01
if self.timer > 1:
bullet = EnemyBulletSprite("images/bullet/enemy_bullet2.png",
[self.rect.x + 40, self.rect.y + 39.5],
-2, -1)
bullet2 = EnemyBulletSprite("images/bullet/enemy_bullet2.png",
[self.rect.x + 120, self.rect.y + 39.5],
-2, 1)
self.bulletgroup.add(bullet)
self.bulletgroup.add(bullet2)
self.timer = 0
if self.timer2 > 4: # BOSS发射导弹
bullet = EnemyBulletSprite("images/bullet/boss_bullet.png", [self.rect.x + 80, self.rect.y + 39.5],
-2,
0)
self.bulletgroup.add(bullet)
self.timer2 = 0
self.rect.y += self.speed
self.rect.x += self.xspeed
if self.rect.x > 400:
self.xspeed *= -1
self.rect.x += self.xspeed
if self.rect.x < 0:
self.xspeed *= -1
self.rect.x += self.xspeed
if self.rect.y > 800:
self.kill()
def bomb(self):
bombimages = ["images/boom/boom01.png",
"images/boom/boom02.png",
"images/boom/boom03.png",
"images/boom/boom04.png",
"images/boom/boom05.png",
"images/boom/boom06.png",
]
for i in bombimages:
self.screen.blit(pygame.image.load(i), (self.rect.x, self.rect.y))
time.sleep(0.001)
pygame.display.update()
self.kill()
class BulletSprite(Sprite): # 玩家子弹精灵租
def __init__(self, imagepath, pos, speed, direction):
super().__init__()
self.image = pygame.image.load(imagepath)
self.rect = self.image.get_rect()
self.rect.x = pos[0]
self.rect.y = pos[1]
self.speed = speed
self.dir = direction
def move(self):
if self.dir == 0:
self.rect.y -= self.speed
def update(self):
self.move()
class EnemyBulletSprite(Sprite): # 敌机子弹精灵组
def __init__(self, imagepath, pos, speed, direction):
super().__init__()
self.image = pygame.image.load(imagepath)
self.rect = self.image.get_rect()
self.rect.x = pos[0]
self.rect.y = pos[1]
self.speed = speed
self.dir = direction
def move(self):
self.rect.y -= self.speed
if self.rect.y < -40:
self.kill()
def move2(self):
self.rect.y -= self.speed
self.rect.x -= self.speed / 2
if self.rect.y < -40:
self.kill()
def move3(self):
self.rect.y -= self.speed
self.rect.x += self.speed / 2
if self.rect.y < -40:
self.kill()
def update(self): # 子弹发射方向
if self.dir == 0:
self.move()
elif self.dir == -1:
self.move2()
elif self.dir == 1:
self.move3()
class PlayerSprite(Sprite): # 玩家战机
def __init__(self, imagepath, rect, pos, speed, screen):
super().__init__()
self.image = pygame.image.load(imagepath)
self.image = self.image.subsurface(rect)
self.rect = self.image.get_rect()
self.rect.x = pos[0]
self.rect.y = pos[1]
self.speed = speed
self.screen = screen
self.bulletgroup = Group()
self.score = 0
self.hp = 100
self.timer = 300
self.bullettype = 0
self.bulletnum = 10
def move(self):
keys = pygame.key.get_pressed()
if keys[locals.K_RIGHT]:
self.rect.x += self.speed
if self.rect.x > 412:
self.rect.x = 412
if keys[locals.K_LEFT]:
self.rect.x -= self.speed
if self.rect.x < 0:
self.rect.x = 0
if keys[locals.K_UP]:
self.rect.y -= self.speed
if self.rect.y < 0:
self.rect.y = 0
if keys[locals.K_DOWN]:
self.rect.y += self.speed
if self.rect.y > 702:
self.rect.y = 702
def fire(self):
self.timer += 1
if self.timer > 50:
self.timer = 0
if self.bullettype == 0:
bullet = BulletSprite("images/bullet/bullet1.png", [self.rect.x + 45, self.rect.y - 20], 6, 0)
self.bulletgroup.add(bullet)
if self.bulletnum <= 0:
self.bullettype = 1
def update(self):
self.bulletgroup.update()
self.bulletgroup.draw(self.screen)
self.move()
self.fire()
CREATE_ENEMY = locals.USEREVENT + 1
CREATE_BOSS = locals.USEREVENT + 2
CREATE_BULLET_HELP = locals.USEREVENT + 3
def main():
pygame.init()
pygame.display.set_caption("雷霆战机")
screen = pygame.display.set_mode((512, 768))
bggroup = Group()
bgsprite = BGSprite("images/background/bg1.jpg")
bggroup.add(bgsprite)
playergroup = Group()
playersprite = PlayerSprite("images/hero/hero_b_03.png", [0, 0, 120, 100], [bgsprite.rect.w / 2 - 61, 600], 5,
screen)
playergroup.add(playersprite)
enemygroup = Group()
bullethelpgroup = Group()
bossgroup = Group()
pygame.time.set_timer(CREATE_ENEMY, random.randint(500, 1000))
pygame.time.set_timer(CREATE_BOSS, random.randint(3000, 5000))
pygame.time.set_timer(CREATE_BULLET_HELP, random.randint(3000, 5000))
font = pygame.font.Font(None, 32)
pygame.mixer.init()
pygame.mixer.music.load("music/NANO-FXXKYIG,Avicii - Levels(NANO-FXXKYIG Bootleg).mp3")
pygame.mixer.music.play(-1,0)
while True:
bggroup.update()
bggroup.draw(screen)
playergroup.update()
playergroup.draw(screen)
enemygroup.update()
enemygroup.draw(screen)
bossgroup.update()
bossgroup.draw(screen)
bullethelpgroup.update()
bullethelpgroup.draw(screen)
screen.blit(font.render("Score:" + str(playersprite.score), True, (255, 0, 0)), (20, 20))
screen.blit(font.render("HP:" + str(playersprite.hp), True, (255, 0, 0)), (20, 60))
pygame.display.update()
check1 = pygame.sprite.groupcollide(playersprite.bulletgroup, enemygroup, True, False) # 碰撞检测 销毁子弹 销毁敌机
if check1:
enemy.hp -= 1
if enemy.hp <= 0:
list(check1.values())[0][0].bomb()
playersprite.score += 100
check11 = pygame.sprite.groupcollide(playersprite.bulletgroup, bossgroup, True, False) # 碰撞检测 销毁子弹 销毁敌机
if check11:
boss.hp -= 2
if boss.hp <= 0:
list(check11.values())[0][0].bomb()
playersprite.score += 500
check2 = pygame.sprite.groupcollide(playergroup, enemygroup, False, True)
if check2:
list(check2.values())[0][0].bomb()
playersprite.hp -= 1
check22 = pygame.sprite.groupcollide(playergroup, bossgroup, False, True)
if check22:
list(check22.values())[0][0].bomb()
playersprite.hp -= 1
for enemy in enemygroup:
check3 = pygame.sprite.groupcollide(playergroup, enemy.bulletgroup, False, True)
if check3:
playersprite.hp -= 2
for boss in bossgroup:
check33 = pygame.sprite.groupcollide(playergroup, boss.bulletgroup, False, True)
if check33:
playersprite.hp -= 5
if playersprite.hp <= 0:
playersprite.kill()
check4 = pygame.sprite.groupcollide(playergroup, bullethelpgroup, False, True)
if check4:
if playersprite.bullettype <= 1:
playersprite.bullettype += 1
playersprite.bulletnum = 10
else:
playersprite.bulletnum = 10
playersprite.fire()
for event in pygame.event.get():
if event.type == locals.QUIT:
pygame.quit()
exit()
if event.type == CREATE_ENEMY:
enemy = EnemySprite("images/enemy/enemy1.png", [random.randint(0, 410), -50], 2, screen,
random.randrange(-1, 1, 2), 0, 1)
enemygroup.add(enemy)
if event.type == CREATE_BOSS:
boss = EnemySprite("images/enemy/boss.png", [random.randint(0, 410), -50], 1, screen, 0, 1, 15)
bossgroup.add(boss)
if __name__ == '__main__':
main()
clock.tick(30)
ck.blit(start_ck2, (0, 0))
start_ck2.blit(bg,(0,0))
pygame.display.update()
for event in pygame.event.get():
# 判断事件类型是否是退出事件
if event.type == pygame.QUIT:
print("游戏退出...")
pygame.quit()
exit()
一键复制
编辑
Web IDE
原始数据
按行查看
历史