用户:
LLeo_淡退查看:0 回复:0 评论:0 创建时间:2023-09-23T17:18:26
【作品展示】

【作品介绍】
空格射击↑↓←→控制
未更新完.......
【作品源代码】
import random
import time
import pygame
import sys
screenx = 480
screeny = 700
enemy_list = []
bullet_list = []
enemy_bullet = []
score = 0
Flag = True
class Me:
def __init__(self,x1,y1):
self.x = x1
self.y = y1
self.img =pygame.image.load(r'我机.png').convert_alpha()
self.moving_left = False
self.moving_right = False
self.moving_down = False
self.moving_up = False
self.blood = 10
def move_up(self):
if(self.moving_up == True):
if(self.y>0):
self.y = self.y - 0.5
def move_down(self):
if(self.moving_down == True):
if(self.y<570):
self.y = self.y + 0.5
def move_right(self):
if(self.moving_right == True):
if(self.x<380):
self.x = self.x + 0.5
def move_left(self):
if(self.moving_left == True):
if(self.x>0):
self.x = self.x - 0.5
def Event(fj):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
fj.moving_right = True
elif event.key == pygame.K_LEFT:
fj.moving_left = True
elif event.key == pygame.K_DOWN:
fj.moving_down = True
elif event.key == pygame.K_UP:
fj.moving_up = True
elif event.key == pygame.K_SPACE:
if(len(bullet_list)<10):
b = Bullet(fj)
bullet_list.append(b)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
fj.moving_right = False
elif event.key == pygame.K_LEFT:
fj.moving_left = False
elif event.key == pygame.K_DOWN:
fj.moving_down = False
elif event.key == pygame.K_UP:
fj.moving_up = False
fj.move_right()
fj.move_left()
fj.move_down()
fj.move_up()
class Enemy:
def __init__(self,x1,y1):
self.x = x1
self.y = y1
self.img = pygame.image.load(r'敌机.png').convert_alpha()
def move_down(self):
if (self.y<700):
self.y = self.y + 0.1
else:
del self
def Me_enemy_impact(d_list,fj):
global Flag
for d in d_list:
if((fj.x+102)>d.x and fj.x<(d.x+57)):
if((fj.y+126)>d.y and fj.y<(d.y+43)):
if(fj.blood>0):
fj.blood = fj.blood - 1
else:
Flag = False
d_list.remove(d)
class Bullet:
def __init__(self,fj):
self.x = fj.x+51
self.y = fj.y
self.img = pygame.image.load(r'我方子弹.png').convert_alpha()
def move_up(self):
if(self.y>0):
self.y-=0.2
else:
del self
def Attack_enemy(d_list,z_list):
global score
for d in d_list:
for z in z_list:
if(z.x>d.x and z.x<(d.x+57)):
if(z.y>d.y and z.y<(d.y+43)):
d_list.remove(d)
z_list.remove(z)
score += 100
class Enemy_Bullet:
def __init__(self,fj):
self.x = fj.x + 28
self.y = fj.y + 22
self.img = pygame.image.load(r'敌方子弹.png').convert_alpha()
def move_down(self):
if(self.y<700):
self.y+=0.1
else:
del self
def Being_attacked(fj,z_list):
global Flag
for z in z_list:
if (z.x>fj.x and z.x < (fj.x+102)):
if (z.y > fj.y and z.y < (fj.y + 126)):
if (fj.blood > 0):
fj.blood-=1
else:
Flag = False
z_list.remove(z)
if __name__ == '__main__':
pygame.init()
font = pygame.font.SysFont('arial',20)
screen = pygame.display.set_mode((screenx,screeny))
pygame.display.set_caption('飞机大战')
background = pygame.image.load(r'背景.png').convert()
screen.blit(background,(0,0))
Plane = Me(190,560)
while Flag:
screen.blit(background,(0,0))
screen.blit(Plane.img,(Plane.x,Plane.y))
if (len(enemy_list)<8):
dj1 = Enemy(random.randint(0, 400), random.randint(0, 10))
enemy_list.append(dj1)
for i in enemy_list:
i.move_down()
if i.y > 700:
enemy_list.remove(i)
else:
screen.blit(i.img, (i.x, i.y))
for b in bullet_list:
b.move_up()
if b.y < 10:
bullet_list.remove(b)
else:
screen.blit(b.img,(b.x,b.y))
if (len(enemy_bullet)<2):
s = random.randint(0,len(enemy_list))
d = Enemy_Bullet(enemy_list[s-1])
enemy_bullet.append(d)
for b in enemy_bullet:
b.move_down()
if b.y > 700:
enemy_bullet.remove(b)
else:
b.move_down()
screen.blit(b.img,(b.x,b.y))
Me_enemy_impact(enemy_list,Plane)
Attack_enemy(enemy_list,bullet_list)
Being_attacked(Plane,enemy_bullet)
text1 = font.render('Score:%s'%str(score),True,(100,150,200))
screen.blit(text1,(20,5))
text2 = font.render('blood:%s' % str(Plane.blood), True, (100, 150, 200))
screen.blit(text2, (350, 5))
pygame.display.update()
Event(Plane)
screen.blit(background,(0,0))
text1 = font.render('Score:%s' % str(score), True, (100, 150, 200))
screen.blit(text1, (200, 260))
pygame.display.update()
time.sleep(2)
sys.exit()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn