大家好。小编通过这段时间学习做了一个超级简单的打飞机。现在贡献给大家。
我们先要建两个.py文件。小编在这里是建的是主程序文件:pplane_main.py 和工具文件: pplane_sprites.py
现在我先看主程序规划pplane_main.py:
#主程序
import pygame
from pplane_sprites import *
#子弹的
ZIDAN_DE = pygame.USEREVENT+1
pygame.init()
pygame.mixer.music.load('儿童歌曲-黑猫警长1(1).mp3')
pygame.mixer.music.play()
#主程序类
class PlaneGame(object):
#初始化
def __init__(self):
#游戏窗口
self.screen = pygame.display.set_mode((SCEREN_RECT.width,SCEREN_RECT.height))
#游戏标题
pygame.display.set_caption('小蜜蜂')
#创建游戏时钟
self.clock = pygame.time.Clock()
#创建精灵和精灵组
self.__cretar_sprite()
pygame.time.set_timer(DIJI_BA,1000)
pygame.time.set_timer(ZIDAN_DE,200)
self.life1 = 1
self.life2 = 1
#游戏开始
def start_game(self):
print('游戏开始')
while True:
self.clock.tick(60)
#监听事件
self.__event_handler()
#碰撞检测
self.__check_collide()
#精灵组更新绘制
self.__update_sprites()
#刷新
pygame.display.update()
#精灵精灵组
def __cretar_sprite(self):
bg1 = Background('./images/background.png')
bg2 = Background('./images/background.png')
bg2.rect.x = bg2.rect.width
self.back_group = pygame.sprite.Group(bg1,bg2)
self.hero = Hero('./images/me1.png')
self.hero_group = pygame.sprite.Group(self.hero)
self.enemy = Enemy('./images/enemy1.png')
self.enemy_group = pygame.sprite.Group(self.enemy)
self.hero2 = Hero2('./images/life.png')
self.hero2_group = pygame.sprite.Group(self.hero2)
self.hero3 = Hero3('./images/life.png')
self.hero3_group = pygame.sprite.Group(self.hero3)
# #事件监听
def __event_handler(self):
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
self.__game_over()
elif event.type == DIJI_BA:
self.enemy_group.add(Enemy('./images/enemy1.png'))
elif event.type == ZIDAN_DE:
self.hero.fort()
self.hero2.fort()
self.hero3.fort()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.hero.panduan1 = True
self.hero2.panduan5 = True
self.hero3.panduana = True
elif event.key == pygame.K_DOWN:
self.hero.panduan2 = True
self.hero2.panduan6 = True
self.hero3.panduanb = True
elif event.key == pygame.K_LEFT:
self.hero.panduan3 = True
self.hero2.panduan7 = True
self.hero3.panduanc = True
elif event.key == pygame.K_RIGHT:
self.hero.panduan4 = True
self.hero2.panduan8 = True
self.hero3.panduand = True
elif event.type ==pygame.KEYUP:
if event.key == pygame.K_UP:
self.hero.panduan1 = False
self.hero2.panduan5 = False
self.hero3.panduana = False
elif event.key == pygame.K_DOWN:
self.hero.panduan2 =False
self.hero2.panduan6 = False
self.hero3.panduanb =False
elif event.key == pygame.K_LEFT:
self.hero.panduan3 = False
self.hero2.panduan7 =False
self.hero3.panduanc =False
elif event.key == pygame.K_RIGHT:
self.hero.panduan4 = False
self.hero2.panduan8 =False
self.hero3.panduand =False
#碰撞检测
def __check_collide(self):
pygame.sprite.groupcollide(self.hero.bullte_group,self.enemy_group,True,True)
eneims = pygame.sprite.spritecollide(self.hero,self.enemy_group,True)
pygame.sprite.groupcollide(self.hero2.bulltea_group,self.enemy_group,True,True)
eneime = pygame.sprite.spritecollide(self.hero2,self.enemy_group,True)
pygame.sprite.groupcollide(self.hero3.bulltes_group,self.enemy_group,True,True)
eneima = pygame.sprite.spritecollide(self.hero3,self.enemy_group,True)
#主机碰撞
if len(eneims)>0:
self.hero.kill()
self.hero2.kill()
self.hero3.kill()
self.__game_over()
elif len(eneime)>0:
self.life1 -= 1
if self.life1 == 0:
#主机移动
self.hero.diaoyong1=False
self.hero.diaoyong2 = True
self.hero.diaoyong3 = False
self.hero.diaoyong4 = False
#僚机1移动
self.hero3.liaojiyidong3 = False
self.hero3.liaojiyidong4 = True
#僚机2移动
self.hero2.liaojiyidong1 = False
self.hero2.liaojiyidong2 = False
self.hero2.rect.y = -10000
self.hero2.rect.x = -10000
self.hero2.kill()
elif len(eneima)>0:
self.life2 -= 1
if self.life2 == 0:
#主机移动
self.hero.diaoyong1=False
self.hero.diaoyong2 = False
self.hero.diaoyong3 = True
self.hero.diaoyong4 = False
#僚机1移动
self.hero2.liaojiyidong1 = False
self.hero2.liaojiyidong2 = True
#僚机2移动
self.hero3.liaojiyidong3 = False
self.hero3.liaojiyidong4 = False
self.hero3.rect.y = -10000
self.hero3.rect.x = -10000
self.hero3.kill()
elif self.life2 == 1 and self.life1 == 1:
#主机移动
self.hero.diaoyong1=False
self.hero.diaoyong2 = False
self.hero.diaoyong3 = False
self.hero.diaoyong4 = True
#僚机1移动
self.hero2.liaojiyidong1 = True
self.hero2.liaojiyidong2 = False
#僚机2移动
self.hero3.liaojiyidong3 = True
self.hero3.liaojiyidong4 = False
elif self.life2 == 0 and self.life1 == 0:
#主机移动
self.hero.diaoyong1=True
self.hero.diaoyong2 = False
self.hero.diaoyong3 = False
self.hero.diaoyong4 = False
#僚机1移动
self.hero2.liaojiyidong1 = False
self.hero2.liaojiyidong2 = False
#僚机2移动
self.hero3.liaojiyidong3 = False
self.hero3.liaojiyidong4 = False
#精灵组更新和绘制
def __update_sprites(self):
#绘制背景
self.back_group.update()
self.back_group.draw(self.screen)
#绘制英雄1
self.hero_group.update()
self.hero_group.draw(self.screen)
#绘制僚机1
self.hero2_group.update()
self.hero2_group.draw(self.screen)
#绘制僚机2
self.hero3_group.update()
self.hero3_group.draw(self.screen)
#敌机
self.enemy_group.update()
self.enemy_group.draw(self.screen)
#英雄子弹
self.hero.bullte_group.update()
self.hero.bullte_group.draw(self.screen)
#僚机1子弹
self.hero2.bulltea_group.update()
self.hero2.bulltea_group.draw(self.screen)
#僚机2子弹
self.hero3.bulltes_group.update()
self.hero3.bulltes_group.draw(self.screen)
#游戏结束
def __game_over(self):
print('游戏结束')
pygame.quit()
exit()
if __name__ == '__main__':
game = PlaneGame()
game.start_game()
现在是工具文件pplane_sprites.py:
import pygame
import random
#设置窗口常量
SCEREN_RECT = pygame.Rect(0,0,700,480)
DIJI_BA = pygame.USEREVENT
class GameSprite(pygame.sprite.Sprite):
def __init__(self,image_name,speed = 1):
super().__init__()
self.image = pygame.image.load(image_name)
self.rect = self.image.get_rect()
self.speed = speed
def update(self):
self.rect.x -= self.speed
#背景类
class Background(GameSprite):
def __init__(self,image_name):
super().__init__(image_name)
def update(self):
super().update()
if self.rect.right== 0:
self.rect.x= SCEREN_RECT.width
#英雄1
class Hero(GameSprite):
def __init__(self,image_name):
super().__init__(image_name)
self.rect.centery = SCEREN_RECT.centery
self.rect.left = SCEREN_RECT.left +20
self.panduan1 = False
self.panduan2 =False
self.panduan3 =False
self.panduan4 =False
self.diaoyong1 =False
self.diaoyong2 = False
self.diaoyong3 = False
self.diaoyong4 = False
self.bullte_group = pygame.sprite.Group()
def update(self):
if self.diaoyong1 == True:
if self.panduan1 == True and self.rect.y >= 0:
self.rect.y -= 5
elif self.panduan2 == True and self.rect.y <= SCEREN_RECT.height-self.rect.height:
self.rect.y += 5
elif self.panduan3 == True and self.rect.x >= 0:
self.rect.x -= 5
elif self.panduan4 == True and self.rect.x <= SCEREN_RECT.width-self.rect.width:
self.rect.x += 5
elif self.diaoyong2 == True:
if self.panduan1 == True and self.rect.y >= 0:
self.rect.y -= 5
elif self.panduan2 == True and self.rect.bottom <= 418 :
self.rect.y += 5
elif self.panduan3 == True and self.rect.x >= 0:
self.rect.x -= 5
elif self.panduan4 == True and self.rect.x <= SCEREN_RECT.width-self.rect.width:
self.rect.x += 5
elif self.diaoyong3 == True:
if self.panduan1 == True and self.rect.y >=62 :
self.rect.y -= 5
elif self.panduan2 == True and self.rect.y <= SCEREN_RECT.height-self.rect.height:
self.rect.y += 5
elif self.panduan3 == True and self.rect.x >= 0:
self.rect.x -= 5
elif self.panduan4 == True and self.rect.x <= SCEREN_RECT.width-self.rect.width:
self.rect.x += 5
elif self.diaoyong4 == True:
if self.panduan1 == True and self.rect.y >= 55:
self.rect.y -= 5
elif self.panduan2 == True and self.rect.bottom <= 430:
self.rect.y += 5
elif self.panduan3 == True and self.rect.x >= 0:
self.rect.x -= 5
elif self.panduan4 == True and self.rect.x <= SCEREN_RECT.width-self.rect.width:
self.rect.x += 5
def fort(self):
for i in (1,2,3):
self.bullte = Bullte('./images/bullet1.png')
self.bullte.rect.left = self.rect.right + i*20
self.bullte.rect.centery = self.rect.centery
self.bullte_group.add(self.bullte)
#敌机
class Enemy(GameSprite):
def __init__(self,image_name):
super().__init__(image_name)
self.rect.left = SCEREN_RECT.width
self.speed = random.randint(1,3)
ma_y = SCEREN_RECT.height-self.rect.height
self.rect.y = random.randint(0,ma_y)
def update(self):
super().update()
if self.rect.right <= 0:
self.kill()
#子弹
class Bullte(GameSprite):
def __init__(self,image_name):
super().__init__(image_name,-10)
def update(self):
super().update()
if self.rect.left > SCEREN_RECT.width:
self.kill()
#僚机1
class Hero2(GameSprite):
def __init__(self,image_name):
super().__init__(image_name)
self.hero1=Hero('./images/me1.png')
self.rect.centery = self.hero1.rect.centery -90
self.rect.left = self.hero1.rect.left-10
self.panduan5 = False
self.panduan6 = False
self.panduan7 =False
self.panduan8 =False
self.liaojiyidong1 = False
self.liaojiyidong2 = False
self.bulltea_group = pygame.sprite.Group()
def update(self):
if self.liaojiyidong1 == True:
if self.panduan5 == True and self.rect.y >=0:
self.rect.y -= 5
elif self.panduan6 == True and self.rect.bottom <= 321:
self.rect.y += 5
elif self.panduan7 == True and self.rect.left >= 10:
self.rect.x -= 5
elif self.panduan8 == True and self.rect.right <=621:
self.rect.x += 5
elif self.liaojiyidong2 == True:
if self.panduan5 == True and self.rect.y >=0:
self.rect.y -= 5
elif self.panduan6 == True and self.rect.bottom <= 362:
self.rect.y += 5
elif self.panduan7 == True and self.rect.left >= 10:
self.rect.x -= 5
elif self.panduan8 == True and self.rect.right <=621:
self.rect.x += 5
def fort(self):
for i in (1,2,3):
self.bulltea = Bullte('./images/bullet1.png')
self.bulltea.rect.left = self.rect.right + i*20
self.bulltea.rect.centery = self.rect.centery
self.bulltea_group.add(self.bulltea)
#僚机2
class Hero3(GameSprite):
def __init__(self,image_name):
super().__init__(image_name)
self.hero1=Hero('./images/me1.png')
self.rect.centery = self.hero1.rect.centery +90
self.rect.left = self.hero1.rect.left-10
self.panduana = False
self.panduanb = False
self.panduanc =False
self.panduand =False
self.liaojiyidong3 = False
self.liaojiyidong4 = False
self.bulltes_group = pygame.sprite.Group()
def update(self):
if self.liaojiyidong3 == True:
if self.panduana == True and self.rect.top >= 159:
self.rect.y -= 5
elif self.panduanb == True and self.rect.bottom <= SCEREN_RECT.bottom:
self.rect.y += 5
elif self.panduanc == True and self.rect.left >= 10:
self.rect.x -= 5
elif self.panduand == True and self.rect.right <=621:
self.rect.x += 5
elif self.liaojiyidong4 == True:
if self.panduana == True and self.rect.top >=118:
self.rect.y -= 5
elif self.panduanb == True and self.rect.bottom <= SCEREN_RECT.bottom:
self.rect.y += 5
elif self.panduanc == True and self.rect.left >= 10:
self.rect.x -= 5
elif self.panduand == True and self.rect.right <=621:
self.rect.x += 5
def fort(self):
for i in (1,2,3):
self.bulltes = Bullte('./images/bullet1.png')
self.bulltes.rect.left = self.rect.right + i*40
self.bulltes.rect.centery = self.rect.centery
self.bulltes_group.add(self.bulltes)
小编就能写到这了、希望对你们有所帮助!