栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 飞机大战

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 飞机大战

import pygame
from random  import randint
import random
pygame.init()
screen = pygame.display.set_mode((480, 700))
done = False
class Plane():
    def __init__(self,x,y,imagefile):
        self.x=x
        self.y=y
        image=pygame.image.load(imagefile)
        self.icon = pygame.transform.scale(image,(80,80))
    def move(self,fx):
        if fx=='left':
            self.x=self.x-2
        elif fx=='right':
            self.x=self.x+2
        else:
            pass
class Bullet():
    def __init__(self,x,y,imagefile):
        self.x=x
        self.y=y
        self.icon=image = pygame.image.load(imagefile).convert_alpha()
    def move(self):
        self.y=self.y-3
        screen.blit(self.icon,(self.x,self.y))
    def __del__(self):
        del self
class Enemy():
    def __init__(self,x,y,step,imagefile):
        self.x=x
        self.y=y
        self.step=step
        self.icon = pygame.image.load(imagefile).convert_alpha()
    def move(self):
        self.y=self.y+self.step+0.2
        screen.blit(self.icon,(self.x,self.y))
    def __del__(self):
        del self
def initdestrop():
    for i in range(10):
        enemy=Enemy(randint(50,430),100,random.random()*2,r'images/enemy1_down2.png')
        destroy_list.append(enemy)

import time
myPlane=None
bullet=None
bullet_list=list()
enemy_list=list()
destroy_list=list()
destroy_list=[]
jishi=0
initdestrop()
baozha_sound=pygame.mixer.Sound('mp3/baozha.mp3')
bullet_sound=pygame.mixer.Sound('mp3/bullet.mp3')
for i in range(randint(3,8)):
    enemy=Enemy(randint(50,430),100,random.random()*2,r'images/enemy1.png')
    enemy_list.append(enemy)


myPlane=Plane(240,600,r'imagesme1.png')
while not done:
    jishi=jishi+1
    if jishi==200:
        jishi=0
        print('add enemy')
        enemy=Enemy(randint(50,430),0,random.random()*2,r'images/enemy1.png')

        enemy_list.append(enemy)
    image = pygame.image.load(r'imagesbackground.png').convert_alpha()
    screen.blit(image,(0,0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type==pygame.KEYUP:
            if event.key==pygame.K_UP:
                bullet_sound.play()
                bullet=Bullet(myPlane.x+(myPlane.icon.get_width())/2,myPlane.y-10,r'imagesbullet1.png')
                bullet_list.append(bullet)
    keyvalue = pygame.key.get_pressed()
    if keyvalue[pygame.K_LEFT]:
        myPlane.move('left')
    if keyvalue[pygame.K_RIGHT]:
        myPlane.move('right')

    for i,item in enumerate(enemy_list):
        item.move()
        if item.y>650:
            print('del enemy')
            baozha_sound.play()
            for baozhao in destroy_list:
                screen.blit(baozhao.icon,(item.x,item.y))
            del enemy_list[i]
            del item
        else:
            pass



    for i,item in enumerate(bullet_list):
        item.move()
        if item.y<=0:
            print('del')
            del bullet_list[i]
            del item
        else:
            for e,enemy in enumerate(enemy_list):
                if abs(enemy.x+(enemy.icon.get_width())/2-item.x)<(enemy.icon.get_width()/2+item.icon.get_width()/2) and abs(enemy.y+enemy.icon.get_height()/2-item.y)<(enemy.icon.get_height()/2+item.icon.get_height()/2):
                    for baozhao in destroy_list:
                        screen.blit(baozhao.icon,(enemy.x,enemy.y))
                    baozha_sound.play()
                    del enemy_list[e]
                    del e
                    print('enemy fall')


    screen.blit(myPlane.icon,(myPlane.x,myPlane.y))
    pygame.display.update()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/317055.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号