Python 之飞机大战

强大的意志进行艰难的抉择,

最后,在宁静的宇宙中观看日落美景。。。。。

import pygame
import time
import random
from pygame.locals import *
class Basedraw(object):
    def __init__(self, screem, x, y, image_name):
        self.x = x
        self.y = y
        self.screem = screem
        self.shen = pygame.image.load(image_name)

class Jzi(object):
    def __init__(self, screem, x, y, image_name):
        Basedraw.__init__(self,screem,x,y,image_name)
        self.bullent_list = []

    def ji_display(self):
        self.screem.blit(self.shen, (self.x, self.y))
        for temp in self.bullent_list:
            if temp.judge():
                temp.move()
                temp.B_display()
            else:
                self.bullent_list.remove(temp)
class Buent(Basedraw):
    def __init__(self,screem,x,y,image_name):
        Basedraw.__init__(self,screem,x,y,image_name)
    def B_display(self):
        self.screem.blit(self.shen, (self.x, self.y))

    def judge(self):
        if self.y >= 0 and self.y <= 600:
            return True
        else:
            return False
class Bullent(Buent):
    def __init__(self,screem,x,y):
        Buent.__init__(self,screem,x+50,y-20,"./resource/zidan.png")
    def move(self):
        self.y -= 10
class D_bullent(Buent):
    def __init__(self,screem,x,y):
        Buent.__init__(self,screem,x + 40,y + 20,"./resource/hongdian.png")
    def move(self):
        self.y += 10

class Hero(Jzi):
    def __init__(self,screem):
        Jzi.__init__(self,screem, 150, 500, "./resource/feiji.png")
    def mov_left(self):
        self.x -= 5
    def mov_right(self):
        self.x += 5
    def mov_up(self):
        self.y -= 5
    def mov_down(self):
        self.y += 5
    def fire(self):
        print("fire")
        self.bullent_list.append(Bullent(self.screem,self.x,self.y))

class Diji(Jzi):
    def __init__(self,screem):
        Jzi.__init__(self, screem, 0, 0, "./resource/diji.png")
        self.direction = "right"

    def mov(self):
        if self.direction == "right":
            self.x += 5
        elif self.direction == "left":
            self.x -= 5
        if self.x >= 330:
            self.direction = "left"
        elif self.x <= 0:
            self.direction = "right"
    def fire(self):
        num = random.randint(1,100)
        if num == 1:
            self.bullent_list.append(D_bullent(self.screem, self.x, self.y))
        else:
            pass


def key_ctrl(hero):
    for event in pygame.event.get():

        # 判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        # 判断是否是按下了键
        elif event.type == KEYDOWN:
            # 检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                hero.mov_left()
            # 检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                hero.mov_right()
            elif event.key == K_w or event.key == K_UP:
                print("up")
                hero.mov_up()
            elif event.key == K_s or event.key == K_DOWN:
                print("down")
                hero.mov_down()
            # 检测按键是否是空格键
            elif event.key == K_SPACE:
                hero.fire()
def main():
    screem = pygame.display.set_mode((400,600),0,32)
    background = pygame.image.load("./resource/background.jpg")
    hero = Hero(screem)
    diji = Diji(screem)
    while True:
        screem.blit(background, (0, 0))
        hero.ji_display()
        diji.ji_display()
        diji.mov()
        diji.fire()
        pygame.display.update()
        time.sleep(0.01)
        key_ctrl(hero)

if __name__ == "__main__":
    main()

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值