import pygame
import time
import random
from pygame.locals import *
class BasePlane(object):
def __init__(self,screen_temp,hp,star,x,y,image_name):
self.x = x
self.y = y
self.hp = hp
self.screen = screen_temp
self.star = star
self.bullet_list = []
self.image = pygame.image.load(image_name)
def display(self):
if self.hp>0:
self.screen.blit(self.image,(self.x,self.y))
elif self.hp<=0:
pass
for bullet in self.bullet_list:
bullet.display()
if bullet.judge():
self.bullet_list.remove(bullet)
class EnemyPlane1(BasePlane):
def __init__(self,screen_temp):
BasePlane.__init__(self,screen_temp,100,0,200,0,'./enemy1.png')
self.count = 1
self.random = 1
def automove(self):
if self.x<0 or self.x>423:
self.count += 1
if self.count%2!=0:
self.x-=5
else:
self.x+=5
def autofire(self):
if self.random!=23:
self.random=random.randint(1,50)
else:
self.bullet_list.append(Bullet(self.screen))
self.bullet_list[-1].x = self.x +28.5
self.bullet_list[-1].y = self.y +53
self.random = 0
class HeroPlane(BasePlane):
def __init__(self,screen_temp):
BasePlane.__init__(self,screen_temp,100,0,200,700,'./me1.png')
def fire(self):
self.bullet_list.append(Bullet(self.screen))
print(self.bullet_list)
self.bullet_list[-1].x = self.x + 51
self.bullet_list[-1].y = self.y - 10
def move_left(self):
if self.x>0:
self.x-=25
if self.x<0:
self.x=0
def move_right(self):
if self.x<378:
self.x+=25
if self.x>378:
self.x=378
def move_up(self):
if self.y>0:
self.y-=50
if self.y<0:
self.y=0
def move_back(self):
if self.y<574:
self.y+=50
if self.y>574:
self.y=574
class Bullet(object):
def __init__(self,screen_temp):
self.x = -10
self.y = -10
self.damage = 10
self.screen = screen_temp
self.image = pygame.image.load('./bullet1.png')
def display(self):
self.screen.blit(self.image,(self.x,self.y))
def move(hero_temp,enemy1_temp):
for bullet in hero_temp.bullet_list:
bullet.y-=2
for bullet in enemy1_temp.bullet_list:
bullet.y+=2
def judge(self):
if self.y<10 or self.y>690:
return True
else:
return False
def hit(hero_temp,enemy1_temp):
#子弹击打判定
for bullet in hero_temp.bullet_list:
if enemy1_temp.x< bullet.x<enemy1_temp.x+57 and enemy1_temp.y<bullet.y<enemy1_temp.y+43:
enemy1_temp.hp -= bullet.damage
hero_temp.bullet_list.remove(bullet)
print(enemy1_temp.hp)
if enemy1_temp.hp<0:
enemy1_temp.hp=0
print(enemy1_temp.hp)
for bullet in enemy1_temp.bullet_list:
if hero_temp.x< bullet.x<hero_temp.x+102 and hero_temp.y<bullet.y<hero_temp.y+126:
hero_temp.hp -= bullet.damage
enemy1_temp.bullet_list.remove(bullet)
print(hero_temp.hp)
if hero_temp.hp<0:
hero_temp.hp=0
print(hero_temp.hp)
def star(hero_temp):
#开场动画
if hero_temp.star<42:
hero_temp.star+=1
hero_temp.y-=3
def key_control(hero_temp):
#获取事件,比如按键
for event in pygame.event.get():
#判断是否按下退出
if event.type == QUIT:
print('exit')
exit()
#判断是否按下按键,开场动画是否结束
elif event.type == KEYDOWN and hero_temp.star == 42:
if event.key == K_a or event.key == K_LEFT:
hero_temp.move_left()
elif event.key == K_d or event.key == K_RIGHT:
hero_temp.move_right()
elif event.key == K_w or event.key == K_UP:
hero_temp.move_up()
elif event.key == K_s or event.key == K_DOWN:
hero_temp.move_back()
elif event.key == K_SPACE:
hero_temp.fire()
#1.搭建界面,完成主要窗口与背景图的显示。
def main():
#1.创建一个窗口
screen = pygame.display.set_mode((480,700),0,32)
background = pygame.image.load('./background.png')
#2.创建对象
hero = HeroPlane(screen)
enemy1 = EnemyPlane1(screen)
#3.填充背景、飞机图片
while True:
#设定背景图片
screen.blit(background,(0,0))
#加载双方飞机
hero.display()
enemy1.autofire()
enemy1.automove()
enemy1.display()
#开场动画
star(hero)
#更新显示内容
pygame.display.update()
#方位控制
key_control(hero)
#子弹运动
Bullet.move(hero,enemy1)
#子弹碰撞判定
hit(hero,enemy1)
#机体碰撞判定
if enemy1.x<hero.x+51<enemy1.x+57 and enemy1.y<hero.y<enemy1.y+43:
enemy1.hp -= 100
hero.hp -= 20
#延时防止卡顿
time.sleep(0.01)
if __name__ == "__main__":
main()
面向对象开发pygame
最新推荐文章于 2023-08-12 18:00:00 发布