最简单之打飞机游戏编程

大家好。小编通过这段时间学习做了一个超级简单的打飞机。现在贡献给大家。

我们先要建两个.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)

小编就能写到这了、希望对你们有所帮助!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,279评论 0 10
  • 各位禾美的家人大家晚上好!首先非常感谢公司提供的发展平台,以及同事们对我的帮助。关于我的上半年总结我想从两个方面来...
    简书米阅读 573评论 0 1
  • 带着孙女学学挖土 文/我心飞翔 午后的阳光,渐渐退掉了它的灼热,小孙女一觉醒来,吃点东西,喝足了水,我们准备下楼。...
    我心飞翔XFJ阅读 166评论 0 0
  • 今天得知自己大学广播台的师兄得了恶性淋巴瘤。 照片上的他非常非常瘦,完全不是大学时候结结实实的样子。而且脸色很差,...
    千秋的布丁酱阅读 318评论 0 1
  • 平台:Windows 类型:虚拟机镜像 软件包:.net coreneo application server...
    ITknight阅读 432评论 0 0