效果图
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