以为比较喜欢玩游戏 所以一直对游戏有很深的情怀 之前在网上无意间看见有游戏的教程 然后就跟着学习了几天 最后跟着做了一个游戏的demo 感觉还不错,这是游戏内图片:
下面是代码:这个是主类:
import pygame as pg
import Plane_sprites as Ps
pg.mixer.init()
class PlaneGame(object):
def __init__(self):
print("游戏初始化")
self.screen = pg.display.set_mode(Ps.SCREEN_RECT.size)
self.clock = pg.time.Clock()
self.__creat_sprites()
pg.time.set_timer(Ps.CREATE_ENEMY_EVENT,1000)
pg.time.set_timer(Ps.HERO_FIRE_EVENT,500)
pg.time.set_timer(Ps.BOSS_MOVE_EVENT,1500)
pg.time.set_timer(Ps.BOSS_FIRE_EVENT,4000)
pg.time.set_timer(Ps.BOSS_UP,10000)
# pg.time.set_timer(Ps.BOSS_TRACK_EVENT,500)
self.Boss_blood = 20
self.fenshu = 0
self.boss_come = False
self.bullets_sound = pg.mixer.Sound("./sound/bullet.wav")
self.bullets_sound.set_volume(0.3)
self.bgm_01 = pg.mixer.Sound("./sound/bgm_zhandou1.wav")
self.bgm_01.set_volume(0.3)
self.Boss_bullets = pg.mixer.Sound("./sound/ssz_03.wav")
self.Boss_bullets.set_volume(1)
self.enemy_Sum = 1
def __event_handler(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.__gameover()
if event.type == Ps.CREATE_ENEMY_EVENT:
# print("敌机出场")
self.enemy = Ps.Enemy()
# pg.time.set_timer(Ps.ENEMY_FIRE_EVENT+self.enemy_Sum,3000)
self.enemy_Sum += 1
self.enemy_Group.add(self.enemy)
if event.type == Ps.HERO_FIRE_EVENT:
self.hero.fire()
# self.bullets_sound.play()
if event.type == Ps.ENEMY_FIRE_EVENT:
self.enemy.fire()
if event.type == Ps.BOSS_UP:
if self.boss_come == False:
self.boss_come = True
print("Boss登场!!")
self.boss = Ps.Boss()
self.Boss_Group.add(self.boss)
# if event.type == Ps.BOSS_TRACK_EVENT:
# if self.boss_come:
# self.boss.track(self.hero.rect.centerx,self.hero.rect.y)
if event.type == Ps.BOSS_FIRE_EVENT and self.boss_come:
self.boss.fire()
self.Boss_bullets.play()
if event.type == Ps.BOSS_MOVE_EVENT and self.boss_come:
self.boss.move()
keys_pressed = pg.key.get_pressed()
if keys_pressed[pg.K_RIGHT]:
self.hero.speed_x = Ps.HERO_SPEED
elif keys_pressed[pg.K_LEFT]:
self.hero.speed_x = -Ps.HERO_SPEED
elif keys_pressed[pg.K_UP]:
self.hero.speed_y = -Ps.HERO_SPEED
elif keys_pressed[pg.K_DOWN]:
self.hero.speed_y = Ps.HERO_SPEED
else:
if not keys_pressed[pg.K_LEFT] or keys_pressed[pg.K_RIGHT]:
self.hero.speed_x = 0
if not keys_pressed[pg.K_UP] or keys_pressed[pg.K_DOWN]:
self.hero.speed_y = 0
pass
def __check_collide(self):
bo = pg.sprite.groupcollide(self.Boss_Group,self.hero.bullets,False,True)
if bo:
self.Boss_blood -= 1
print("Boss剩余血量 %d" % self.Boss_blood)
if self.Boss_blood == 0:
print("boss Killed!")
self.boss.kill()
self.Boss_blood = 20
self.boss_come = False
self.fenshu += 10
# try:
if self.boss_come:
f = pg.sprite.groupcollide(self.hero_Group,self.boss.bullets,True,True)
if f:
print("被击中!")
self.__gameover()
# except:
# if d:
# self.hero.kill()
# self.__gameover()
# except:
# print("boss没有出现")
# finally:
a = pg.sprite.groupcollide(self.hero.bullets,self.enemy_Group,True,True)
if a:
self.fenshu += 1
b = pg.sprite.spritecollide(self.hero,self.enemy_Group,True)
if b:
self.hero.kill()
self.__gameover()
def __update_sprites(self):
self.bg_Group.update()
self.bg_Group.draw(self.screen)
self.enemy_Group.update()
self.enemy_Group.draw(self.screen)
self.hero_Group.update()
self.hero_Group.draw(self.screen)
self.hero.bullets.update()
self.hero.bullets.draw(self.screen)
self.Boss_Group.update()
self.Boss_Group.draw(self.screen)
try:
if self.boss_come:
self.boss.bullets.update()
self.boss.bullets.draw(self.screen)
except:
pass
#@staticmethod
def __gameover(self):
pg.quit()
print(self.fenshu)
exit()
def start_game(self):
print("游戏开始")
while True:
self.clock.tick(Ps.GAME_FPS)
self.__event_handler()
self.__check_collide()
self.__update_sprites()
pg.display.update()
def __creat_sprites(self):
bg1 = Ps.Background()
bg2 = Ps.Background(True)
# bg2.rect.y = -bg2.rect.height
self.bg_Group = pg.sprite.Group(bg1,bg2)
self.enemy_Group = pg.sprite.Group()
self.hero = Ps.Hero()
self.hero_Group = pg.sprite.Group(self.hero)
self.Boss_Group = pg.sprite.Group()
if __name__ == '__main__':
game = PlaneGame()
game.bgm_01.play()
game.start_game()
下面这个是包含敌人和主角的类的定义 还有子弹 小兵 各种计时器和操作控制定义:
import random
from pygame.sprite import Sprite
import pygame as pg
SCREEN_RECT = pg.Rect(0,0,480,700)
GAME_FPS = 60
HERO_SPEED = 3
#创建敌人数量
CREATE_ENEMY_EVENT = pg.USEREVENT
HERO_FIRE_EVENT = pg.USEREVENT + 1
BOSS_FIRE_EVENT = pg.USEREVENT + 2
BOSS_MOVE_EVENT = pg.USEREVENT + 3
BOSS_UP = pg.USEREVENT + 4
ENEMY_FIRE_EVENT = pg.USEREVENT + 5
BOSS_TRACK_EVENT = pg.USEREVENT + 6
class GameSprite(Sprite):
def __init__(self,image_name,speed=1):
super().__init__()
self.image = pg.image.load(image_name)
self.rect = self.image.get_rect()
self.speed = speed
def update(self):
self.rect.y += self.speed
def check(self):
if self.rect.x <= 0:
self.rect.x = 0
if self.rect.right >= SCREEN_RECT.right:
self.rect.x = SCREEN_RECT.right
if self.rect.bottom >= SCREEN_RECT.height:
self.rect.bottom = SCREEN_RECT.height
if self.rect.y <= -100:
self.rect.y = 0
class Background(GameSprite):
#创建背景
def __init__(self,is_alt=False):
super().__init__("./images/background.png")
if is_alt:
self.rect.y = self.rect.height
def update(self):
super().update()
if self.rect.y >= SCREEN_RECT.height:
self.rect.y = -SCREEN_RECT.height
class Enemy(GameSprite):
def __init__(self):
super().__init__("./images/enemy1.png")
self.speed = random.randint(1,3)
self.rect.bottom = 0
max_x = SCREEN_RECT.width - self.rect.width
self.rect.x = random.randint(0,max_x)
self.bullet = pg.sprite.Group()
def update(self):
super().update()
if self.rect.y >= SCREEN_RECT.height:
# print("敌机删除")
self.kill()
def __del__(self):
# print("敌机挂了 %s" % self.rect)
pass
def fire(self):
bullet = Bullet_enemy()
bullet.rect.x = self.rect.bottom
bullet.rect.centerx = self.rect.centerx
self.bullet.add(bullet)
class Hero(GameSprite):
def __init__(self):
super().__init__("./images/me1.png")
self.speed_x = 0
self.speed_y = 0
self.rect.centerx = SCREEN_RECT.centerx
self.rect.bottom = SCREEN_RECT.bottom - 30
self.bullets = pg.sprite.Group()
def update(self):
self.rect.x += self.speed_x
self.rect.y += self.speed_y
if self.rect.x < 0:
self.rect.x = 0
elif self.rect.right > SCREEN_RECT.right:
self.rect.right = SCREEN_RECT.right
def fire(self):
for i in (-1,0,1):
bullet = Bullet()
bullet.speed_x = i
bullet.rect.bottom = self.rect.y
bullet.rect.centerx = self.rect.centerx
self.bullets.add(bullet)
pass
class Bullet(GameSprite):
def __init__(self):
super().__init__("./images/bullet1.png",-3)
self.speed_x = 0
def update(self):
super().update()
self.rect.x += self.speed_x
if self.rect.y <= 0:
self.kill()
def __del__(self):
# print("子弹被销毁")
pass
class Bullet_enemy(GameSprite):
def __init__(self):
super().__init__("./images/bullet2.png",2)
self.speed_x = 0
def update(self):
super().update()
self.rect.x += self.speed_x
if self.rect.y <= 0 or self.rect.x <= 0 or self.rect.right >= SCREEN_RECT.right :
self.kill()
def __del__(self):
# print("子弹被销毁")
pass
class Boss(GameSprite):
def __init__(self):
super().__init__("./images/enemy3_n1.png")
self.speed_y = 0
self.rect.bottom = 0
self.speed_x = 0
self.bullets = pg.sprite.Group()
def update(self):
self.check()
self.rect.x += self.speed_x
self.rect.y += self.speed_y
if self.rect.x <= 0:
self.rect.x = 0
if self.rect.right >= SCREEN_RECT.right:
self.rect.right = SCREEN_RECT.right
if self.rect.bottom >= (SCREEN_RECT.height / 2):
self.rect.bottom = SCREEN_RECT.height/2
if self.rect.y <= 0:
self.rect.y = 0
def fire(self):
for i in (-1,0,1):
bullet = Bullet_enemy()
bullet.speed_x = i
bullet.rect.y = self.rect.bottom
bullet.rect.centerx = self.rect.centerx
self.bullets.add(bullet)
pass
def track(self,x,y):
x_speed = abs(self.rect.centerx - x) / abs(self.rect.bottom - y)
self.bullet.speed_x = x_speed * 2
def move(self):
self.speed_x = random.randint(-2,2)
self.speed_y = random.randint(-2,2)
这两个是主要代码 其他的东西就不贴出来了