python的飞机大战_python-飞机大战

效果图

main.py

importtimeimportpygamefrom EnemyPlane importEnemyPlanefrom HeroPlane importHeroPlanefrom KeyControl importkey_controldefmain():

screen= pygame.display.set_mode((515, 960), 0, 32)

background= pygame.image.load("./images/background.png")

hero=HeroPlane(screen)

enemy=EnemyPlane(screen)

right=0

down=0whileTrue:

screen.blit(background, (0, 0))

hero.display()

hero.move(right, down)

enemy.display()

enemy.move()

enemy.fire()

pygame.display.update()

right, down=key_control(hero, right, down)

time.sleep(0.01)if __name__ == "__main__":

main()

Base.py

importpygame#加载素材

classBase(object):def __init__(self, screen_temp, x, y, image_path):

self.x=x

self.y=y

self.screen=screen_temp

self.image= pygame.image.load(image_path)

BasePlane.py

from Base importBase#飞机基类

classBasePlane(Base):def __init__(self, screen_temp, x, y, image_path):

super().__init__(screen_temp, x, y, image_path)

self.bullet_list=[]#展示飞机

defdisplay(self):

self.screen.blit(self.image, (self.x, self.y))#循环展示子弹

for bullet inself.bullet_list:

bullet.display()

bullet.move()#判断子弹是否越界

ifbullet.judge():

self.bullet_list.remove(bullet)

BaseBullet.py

from Base importBase#子弹基类

classBaseBullet(Base):#展示子弹

defdisplay(self):

self.screen.blit(self.image, (self.x, self.y))

HeroPlane.py

from BasePlane importBasePlanefrom HeroBullet importHeroBullet#英雄飞机类

classHeroPlane(BasePlane):def __init__(self, screen_temp):

super().__init__(screen_temp, 204, 800, "./images/me.png")defmove(self, x_right, y_down):

self.x+=x_right

self.y+=y_down#开火

deffire(self):#子弹列表创建子弹对象

self.bullet_list.append(HeroBullet(self.screen, self.x, self.y))

EnemyPlane.py

importrandomfrom BasePlane importBasePlanefrom EnemyBullet importEnemyBullet#敌机类

classEnemyPlane(BasePlane):def __init__(self, screen_temp):

super().__init__(screen_temp, 0, 0, "./images/e0.png")

self.direction= "right"

#左右移动

defmove(self):if self.direction == "right":

self.x+= 5

elif self.direction == "left":

self.x-= 5

#改变移动方向

if self.x > 399:

self.direction= "left"

elif self.x <0:

self.direction= "right"

#开火

deffire(self):#子弹列表创建子弹对象

random_num = random.randint(1, 100)if random_num == 25 or random_num == 75:

self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y))

HeroBullet.py

from BaseBullet importBaseBullet#子弹类

classHeroBullet(BaseBullet):def __init__(self, screen_temp, x, y):

super().__init__(screen_temp, x + 53, y - 20, "./images/pd.png")#子弹移动

defmove(self):

self.y-= 10

#判断子弹是否越界

defjudge(self):if self.y <0:returnTrueelse:return False

EnemyBullet.py

from BaseBullet importBaseBullet#敌机子弹

classEnemyBullet(BaseBullet):def __init__(self, screen_temp, x, y):

super().__init__(screen_temp, x + 53, y + 82, "./images/epd.png")#子弹移动

defmove(self):

self.y+= 10

#判断子弹是否越界

defjudge(self):if self.y > 960:returnTrueelse:return False

KeyControl.py

importpygamefrom pygame.locals import *

#按键控制

defkey_control(hero_temp, right, down):for event inpygame.event.get():if event.type ==QUIT:print("exit")

exit()elif event.type ==KEYUP:print("keyup")

right=0

down=0elif event.type ==KEYDOWN:#按左键

if event.key == K_a or event.key ==K_LEFT:print('left')

right= -10

#按右键

elif event.key == K_d or event.key ==K_RIGHT:print('right')

right= 10

#按上键

elif event.key == K_w or event.key ==K_UP:print('up')

down= -10

#按下键

elif event.key == K_s or event.key ==K_DOWN:print("down")

down= 10

#按空格键

elif event.key ==K_SPACE:print('space')

hero_temp.fire()return right, down

最后在Main.py里运行即可

注意点:

1.py文件名和里面的类名不需要相同,py文件里可以放class,也可以只有一个函数,这点和java非常不一样

2.py文件开头导包的时候必须是  from EnemyPlane import EnemyPlane (导入XXX文件里的某某内容) ,不能只写 import EnemyPlane

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值