ziheng -飞机大战4.0

import random

import pygame

class di_ji():
    def __init__(self,chuangkou):
        pifu = [r"图片\alien_1.png", r"图片\alien_2.png", r"图片\alien_3.png",
                                r"图片\alien_4.png", r"图片\alien_5.png"]
        self.x = random.randint(0,480)
        self.y = random.randint(0,480)
        self.clothes = pygame.image.load(pifu[random.randint(0,4)])
        self.chuangkou = chuangkou
        self.su_du_x = 100

    def show(self):
        self.chuangkou.blit(self.clothes,(self.x,self.y))

    def yi_dong(self):
        self.x = self.x + self.su_du_x
        if self.x > 480:
            self.su_du_x = -10
        if self.x < 0:
            self.su_du_x = 10


# 导弹类
# 向前移动
# 判断有没有碰撞
# 属性:x,y,皮肤, 窗口
class dao_dan():
    def __init__(self,chuangkou,x,y,pifu):
        self.x = x
        self.y = y
        self.clothes = pygame.image.load(pifu)
        self.chuangkou = chuangkou
    def show(self):
        self.chuangkou.blit(self.clothes,(self.x,self.y))

    def shang(self):
        self.y = self.y - 10


# 敌机类
# 方法:发射子弹,移动
# 属性:x,y,肤, 窗口

class zhan_ji():
    def __init__(self, chuangkou):
        self.name = "战神号"
        self.x = 240
        self.y = 390
        self.clothes = pygame.image.load(r"图片\hero1.png")
        self.chuangkou = chuangkou
        self.wu_qi = []

    def show(self):
        self.chuangkou.blit(self.clothes,(self.x,self.y))
        for dao in self.wu_qi:
            dao.show()
            dao.shang()


    def shang(self):
        self.y = self.y - 10
        if self.y < 0:
            self.y = 30

    def xia(self):
        self.y = self.y + 10
        if self.y > 780:
            self.y = 750

    def zuo(self):
        self.x = self.x - 10
        if self.x < 0:
            self.x = 30

    def you(self):
        self.x = self.x + 10
        if self.x > 480:
            self.x = 380

    def fa_she(self):
        self.wu_qi.append(dao_dan(self.chuangkou,self.x,self.y,r"图片\bomb.png"))


def anjian_kongzhi(zhan_shen):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                zhan_shen.you()
            elif event.key == pygame.K_LEFT:
                zhan_shen.zuo()
            elif event.key == pygame.K_DOWN:
                zhan_shen.xia()
            elif event.key == pygame.K_UP:
                zhan_shen.shang()
            elif event.key == pygame.K_SPACE:
                zhan_shen.fa_she()



pygame.init()
screen = pygame.display.set_mode((480,780))
pygame.display.set_caption("飞机大战")
jiazai_tupian = pygame.image.load(r"图片\background.png")
zhan_shen = zhan_ji(screen)
# 让电脑每隔多少毫秒响应一次按钮
pygame.key.set_repeat(1,1)
di_fang_zhan_ji = di_ji(screen)

while 1 > 0:
    anjian_kongzhi(zhan_shen )
    screen.blit(jiazai_tupian, (0, 0))

    di_fang_zhan_ji.show()
    di_fang_zhan_ji.yi_dong()
    zhan_shen.show()
    pygame.display.update()

猜你喜欢

转载自blog.csdn.net/houlaos/article/details/107007675