前言
之前出过一期雷霆战机的小游戏,但是有粉丝说,难度有点大!! 这不宠粉狂魔的我,写了一个超简单的雷霆战机游戏!! 适合新手小白学习参考哟!!源码领取看相关文件!
相关文件
关注小编,私信小编领取哟! 当然别忘了一件三连哟~~
公众号:Python日志 可以关注小编公众号,会不定时的发布一下Python小技巧,还有很多资源可以免费领取哟!! 源码领取:加Python学习交流群:494958217可以领取哟
开发工具
Python版本:3.7.8 相关模块: pygame模块; random模块; time模块; os模块; 以及一些python自带的模块。
环境搭建
安装Python并添加到环境变量,pip安装需要的相关模块即可。
效果展示
代码展示
模块安装
import pygame, os
import time
import random
from pygame.sprite import Sprite
from pygame.sprite import Group
Boss类
class Boss(Sprite):
def __init__(self,boss_img_name):
super().__init__()
self.image = pygame.image.load('图片/boss_1.png').convert_alpha()
self.rect = self.image.get_rect()
self.blood = 1000
self.speed = 3.5
def move(self):
if self.rect.centerx>=512:
self.speed =-self.speed
if self.rect.centerx<=0:
self.speed = -self.speed
self.rect.centerx +=self.speed
Enemy类
class Enemy(Sprite):
def __init__(self,screen):
super().__init__()
self.screen = screen
alien_num = random.randint(1,5)
self.image = pygame.image.load('图片/alien_' + str(alien_num) + '.png')
self.image = pygame.transform.scale(self.image,(62,62))
self.rect = self.image.get_rect()
self.score = 10
self.bullet_img = pygame.image.load("图片/alien_bullet.png").convert_alpha()
self.bullet_img = pygame.transform.scale(self.bullet_img, (12, 12))
self.speed = random.randint(3,5)
self.bullets = Group()
self.shoot_frequency = 0
def move(self):
self.rect.top += 5
def shoot(self):
if self.shoot_frequency % 200 == 0:
bullet = Enemy_Bullet(self.bullet_img, self.rect.midbottom)
self.bullets.add(bullet)
self.shoot_frequency += 1
if self.shoot_frequency > 200:
self.shoot_frequency = 1
def moveBullet(self):
for bullet in self.bullets:
bullet.move()
if bullet.rect.bottom < 0:
self.bullets.remove(bullet)
def drawBullets(self, scr):
self.bullets.draw(scr)
Enemy_Bullet类
class Enemy_Bullet(pygame.sprite.Sprite):
def __init__(self, init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("图片/alien_bullet.png").convert_alpha()
self.image = pygame.transform.scale(self.image, (12, 12))
self.rect = self.image.get_rect()
self.rect.midbottom = init_pos
self.rect.centery +=36
self.speed = 8
def move(self):
self.rect.top += self.speed
MyHero类
class MyHero(Sprite):
_rate = 100
def __init__(self,screen,size = 1):
super().__init__()
self.screen = screen
self.image_big = pygame.image.load('图片/hero.png').convert_alpha()
self.image = self.image_big.subsurface(pygame.Rect(120, 0, 318 - 240, 87))
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.centery = self.screen_rect.bottom - self.rect.height
self.centerX = float(self.rect.centerx)
self.centerY = float(self.rect.centery)
self.air = None
self.air_rect = pygame.Rect(self.centerX - 20,self.centerY+int((self.rect.height+72)/2)-10-36,40,72)
self.bullets = Group()
self.bullet_image = pygame.image.load('图片/bullet_1.png').convert_alpha()
def shoot(self):
bullet = Bullet(self.bullet_image,self.rect.midtop)
self.bullets.add(bullet)
def moveBullet(self):
for bullet in self.bullets:
bullet.move()
if bullet.rect.bottom < 0:
self.bullets.remove(bullet)
def drawBullets(self, scr):
self.bullets.draw(scr)
def set_air(self, case):
if case == 'up':
air = pygame.image.load('图片/air.png').convert_alpha()
img = air.subsurface(pygame.Rect(80, 0, 50, 87))
self.air = img
elif case == 'remove':
self.air = None
def set_image(self, case):
if case=='left':
rect = pygame.Rect(195,0,318-248,87)
image = self.image_big.subsurface(rect)
elif case =='right':
rect = pygame.Rect(195,0,318-248,87)
image = pygame.transform.flip(self.image_big.subsurface(rect), True, False)
elif case == 'up' or case == 'down':
rect = pygame.Rect(120, 0, 318 - 240, 87)
image = self.image_big.subsurface(rect)
self.image = image
游戏主循环
game = 'wait'
while True:
if game =='wait':
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if start_rect.collidepoint(event.pos):
student_plane.__init__(screen)
game = 'ing'
screen.blit(bg_img0, (0, 0))
screen.blit(start_img, start_rect)
pygame.display.flip()
time.sleep(0.05)
elif game == 'ing':
screen.blit(bg_img1, (0, pos_y1))
screen.blit(bg_img2, (0, pos_y2))
pos_y1 += 1
pos_y2 += 1
if pos_y1 >= 0:
pos_y1 = -768
if pos_y2 >= 768:
pos_y2 = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYUP:
student_plane.set_image('down')
student_plane.air = None
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and len(student_plane.bullets) <6:
fire_music()
student_plane.shoot()
student_plane.drawBullets(screen)
student_plane.moveBullet()
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
student_plane.set_image('left')
if student_plane.rect.centerx>=40:
student_plane.rect.centerx -=8.5
elif keys[pygame.K_d]:
student_plane.set_image('right')
if student_plane.rect.centerx <= 478:
student_plane.rect.centerx +=8.5
elif keys[pygame.K_w]:
student_plane.set_image('up')
student_plane.set_air('up')
if student_plane.rect.centery >= 45:
student_plane.rect.centery -=8.5
elif keys[pygame.K_s]:
student_plane.set_image('down')
if student_plane.rect.centery <= 727:
student_plane.rect.centery +=8.5
screen.blit(student_plane.image,student_plane.rect)
if student_plane.air != None:
screen.blit(student_plane.air, (student_plane.rect.centerx-30, student_plane.rect.centery+33))
sec +=1
rhy = random.randint(ran1,ran2)
if sec%rhy ==0 and len(enemies) < max_enemies or sec ==1:
enemy = Enemy(screen)
enemy.rect.centerx=random.randint(0,512)
enemy_bullet = Enemy_Bullet((enemy.rect.centerx,enemy.rect.centery))
enemies.add(enemy)
enemy_bullets.add(enemy_bullet)
enemies.draw(screen)
enemy_bullets.draw(screen)
for enemy in enemies:
enemy.move()
collision_over1 = pygame.sprite.collide_rect(student_plane, enemy)
if collision_over1:
enemies.remove(enemy)
life -= 1
if enemy.rect.bottom >768:
enemies.remove(enemy)
for enemy_bullet in enemy_bullets:
enemy_bullet.move()
collision_over2 = pygame.sprite.collide_rect(student_plane, enemy_bullet)
if collision_over2:
enemy_bullets.remove(enemy_bullet)
life -= 1
if enemy_bullet.rect.bottom >768:
enemy_bullets.remove(enemy_bullet)
if life <=0:
game = "over"
enemy_bullets.remove(enemy_bullet)
if score >=140:
ran1,ran2 = 15,25
max_enemies = 17
screen.blit(boss.image,boss.rect)
boss.move()
for my_bullet in student_plane.bullets:
hit_boss = pygame.sprite.collide_rect(boss,my_bullet)
if hit_boss:
boss.blood -=1.2
score+=1
if boss.blood <=0:
game = 'success'
collisions = pygame.sprite.groupcollide(student_plane.bullets, enemies, True, True)
if collisions:
score+=10
surface1 = my_font.render(u"当前得分:%s"%(score),True,[0,255,0])
screen.blit(surface1,[20,20])
surface2 = my_font.render(u"当前生命数:%s" % (life), True, [255, 0, 0])
screen.blit(surface2, [200, 20])
pygame.display.flip()
time.sleep(0.05)
elif game == 'over':
score = 0
life = 3
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if restart_rect.collidepoint(event.pos):
student_plane.__init__(screen)
game = 'ing'
screen.blit(bg_img1, (0, pos_y1))
screen.blit(bg_img2, (0, pos_y2))
screen.blit(gameover_img, (163, 310))
screen.blit(restart_img, restart_rect)
pos_y1 += 0
pos_y2 += 0
pygame.display.flip()
time.sleep(0.05)
elif game == 'success':
score = 0
life = 3
boss.blood = 1000
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if restart_rect.collidepoint(event.pos):
student_plane.__init__(screen)
game = 'ing'
screen.blit(bg_img1, (0, pos_y1))
screen.blit(bg_img2, (0, pos_y2))
screen.blit(gamesuccess, (170, 220))
screen.blit(restart_img, restart_rect)
pos_y1 += 0
pos_y2 += 0
pygame.display.flip()
time.sleep(0.05)
总结
游戏的部分代码我已经放在文章里面啦,也是一些主要的实现过程,希望对新入门小伙伴的学习能够起到帮助,要是有啥不懂的都可以直接私信小编,源码的领取的话可以看相关文件哟!!
|