Python飞机大战教程3:各种敌机
本帖最后由 KeyError 于 2023-1-31 13:00 编辑敌只因
敌机有小型,中型,大型,分别写一个类{:10_265:}
#enemy.py
import pygame
from random import randint
class SmallEnemy(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load("images/enemy1.png").convert_alpha()
self.rect=self.image.get_rect()
self.width,self.height=bg_size,bg_size
self.speed=2
self.active=True
self.rect.left,self.rect.top=\
randint(0,self.width-self.rect.width),\
randint(-5*self.height,0)
self.mask=pygame.mask.from_surface(self.image)
def move(self):
if self.rect.top<self.height:
self.rect.top+=self.speed
else:
self.reset()
#重新出现
def reset(self):
self.active=True
self.rect.left,self.rect.top=\
randint(0,self.width-self.rect.width),\
randint(-5*self.height,0)
class MidEnemy(pygame.sprite.Sprite):
energy=8
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load("images/enemy2.png").convert_alpha()
self.image_hit=pygame.image.load("images/enemy2_hit.png").convert_alpha()
self.rect=self.image.get_rect()
self.width,self.height=bg_size,bg_size
self.speed=1
self.active=True
self.rect.left,self.rect.top=\
randint(0,self.width-self.rect.width),\
randint(-10*self.height,-self.height)
self.mask=pygame.mask.from_surface(self.image)
self.energy=8
self.hit=False
def move(self):
if self.rect.top<self.height:
self.rect.top+=self.speed
else:
self.reset()
def reset(self):
self.active=True
self.energy=8
self.rect.left,self.rect.top=\
randint(0,self.width-self.rect.width),\
randint(-10*self.height,-self.height)
class BigEnemy(pygame.sprite.Sprite):
energy=20
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image1=pygame.image.load("images/enemy3_n1.png").convert_alpha()
self.image2=pygame.image.load("images/enemy3_n2.png").convert_alpha()
self.image_hit=pygame.image.load("images/enemy3_hit.png").convert_alpha()
self.rect=self.image1.get_rect()
self.width,self.height=bg_size,bg_size
self.speed=1
self.active=True
self.rect.left,self.rect.top=\
randint(0,self.width-self.rect.width),\
randint(-15*self.height,-5*self.height)
self.mask=pygame.mask.from_surface(self.image1)
self.energy=BigEnemy.energy
self.hit=False
self.bullet=[]
def move(self):
if self.rect.top<self.height:
self.rect.top+=self.speed
else:
self.reset()
def reset(self):
self.active=True
self.energy=BigEnemy.energy
self.rect.left,self.rect.top=\
randint(0,self.width-self.rect.width),\
randint(-15*self.height,-5*self.height)
static/image/hrline/1.gif
#main.py
import pygame
import sys
import traceback
import myplne
import enemy
from pygame.locals import *
pygame.init()
pygamr.mixer.init()
bg_size = width,height = 480,700 #尺寸
screen = pygame.display.set_mode(bg_size) #画布
pygame.display.set_caption("飞机大战") #标题
#颜色常量
BLACK=(0,0,0)
GREEN=(0,255,0)
WHITE=(255,255,255)
RED=(255,0,0)
YELLOW=(255,255,0)
background=pygame.image.load("images/background.png").convert()
def main():
clock = pygame.time.Clock()
me = myplne.MyPlane(bg_size)
def add_small(a,b,num):
for i in range(num):
e1=myenemy.SmallEnemy(bg_size)
a.add(e1)
b.add(e1)
def add_mid(a,b,num):
for i in range(num):
e2=myenemy.MidEnemy(bg_size)
a.add(e2)
b.add(e2)
def add_big(a,b,num):
for i in range(num):
e3=myenemy.BigEnemy(bg_size)
bu_i=0
BU_N=4
for i in range(BU_N):
e3.bullet.append(mybullet.Bullet1(e3.rect.midtop))
a.add(e3)
b.add(e3)
def add_speed(feji,add):
for i in feji:
i.speed+=add
small=pygame.sprite.Group()
add_small(small,enemi,15)
mid=pygame.sprite.Group()
add_mid(mid,enemi,4)
big=pygame.sprite.Group()
add_big(big,enemi,2)
qie = True
while True:
for event in pygame.event.get():
if event.type == QUIT: #如果用户点击关闭按钮
pygame.quit()
sys.exit()
#控制飞机
key_p=pygame.key.get_pressed()
if key_p or key_p:
me.moveUp()
if key_p or key_p:
me.moveDown()
if key_p or key_p:
me.moveLeft()
if key_p or key_p:
me.moveRight()
if qie:
screen.blit(me.image1,me.rect)
else:
screen.blit(me.image2,me.rect)
for each in big:
if each.active:
each.move()
if each.hit:
screen.blit(each.image_hit,each.rect)
each.hit=False
else:
if qie:
screen.blit(each.image1,each.rect)
else:
screen.blit(each.image2,each.rect)
pygame.draw.line(screen,BLACK,\
(each.rect.left,each.rect.top-5),\
(each.rect.right,each.rect.top-5),\
2)
energy_r=each.energy/myenemy.BigEnemy.energy
if energy_r>0.2:
energy_c=GREEN
else:
energy_c=RED
pygame.draw.line(screen,energy_c,\
(each.rect.left,each.rect.top-5),\
(each.rect.left+each.rect.width*energy_r,each.rect.top-5),\
2)
for b in each.bullet:
screen.blit(b.image,b.rect)
b.move2()
if each.rect.bottom==-50:
enemy3_fly_sound.play()
else:
if not(delay%3):
if e3_i==0:
enemy3_down_sound.play()
screen.blit(each.destroy_images,each.rect)
e3_i=(e3_i+1)%6
if not(bool(e3_i)):
me_down_sound.stop()
each.reset()
for each in mid:
if each.active:
if each.hit==False:
screen.blit(each.image_hit,each.rect)
hit=False
else:
screen.blit(each.image,each.rect)
each.move()
pygame.draw.line(screen,BLACK,\
(each.rect.left,each.rect.top-5),\
(each.rect.right,each.rect.top-5),\
2)
en_r=each.energy/myenemy.MidEnemy.energy
if en_r>0.2:
en_c=GREEN
else:
en_c=RED
pygame.draw.line(screen,en_c,\
(each.rect.left,each.rect.top-5),\
(each.rect.left+each.rect.width*en_r,\
each.rect.top-5),2)
else:
if not(delay%3):
if e2_i==0:
enemy2_down_sound.play()
screen.blit(each.destroy_images,each.rect)
e2_i=(e2_i+1)%4
if e2_i==0:
each.reset()
for each in small:
if each.active:
each.move()
screen.blit(each.image,each.rect)
else:
if not(delay%3):
if e1_i==0:
enemy1_down_sound.play()
screen.blit(each.destroy_images,each.rect)
e1_i=(e1_i+1)%4
if e1_i==0:
each.reset()
screen.blit(background, (0,0)) #绘制背景
if not (delay % 5):
qie=not qie
delay-=1
if not delay:
delay=100
pygame.display.flip()
clock.tick(60) #限制帧率
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
except:
traceback.print_exc() #报告异常
pygame.quit()
input()
背景阴乐
小甲鱼老师说过:
没有声音的游戏就好比是不沾番茄酱的薯条,忘记带枪的战士……
所以,让我们添加一点阴乐{:10_256:}
......
YELLOW = (255,255,0)
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
background=pygame.image.load("images/background.png").convert()
......
def main():
pygame.mixer.music.play(-1)
clock = pygame.time.Clock()
......
上一篇: 传送门
下一篇: 停更了,别敬请期待了。
页:
[1]