python植物大戰(zhàn)僵尸二十一之僵尸吃太陽花

import pygame
from pygame.locals import *
import sys
import time
import random

from Bullet import Bullet
from FlagZombie import FlagZombie
from Peashooter import Peashooter
from Sun import Sun
from SunFlower import SunFlower
from WallNut import WallNut

# 初始化pygame
from Zombie import Zombie

pygame.init()

# for font in pygame.font.get_fonts():
#     print(font)

size = (1200, 600)

# 設(shè)置屏幕寬高
screen = pygame.display.set_mode(size)
# 設(shè)置屏幕標(biāo)題
pygame.display.set_caption("植物大戰(zhàn)僵尸")
#初始化音樂模塊
pygame.mixer.init()
#加載音樂
pygame.mixer.music.load("material/music/02 - Crazy Dave (Intro Theme).mp3")
# logo = pygame.image.load('material/images/logo.jpg').convert_alpha()
# pygame.display.set_icon(logo)

backgroundImg = pygame.image.load('material/images/background1.jpg').convert_alpha()
sunbackImg = pygame.image.load('material/images/SeedBank.png').convert_alpha()
flower_seed = pygame.image.load("material/images/Sunflower.gif")
wallNut_seed = pygame.image.load("material/images/WallNut.gif")
peashooter_seed = pygame.image.load("material/images/Peashooter.gif")
sunFlowerImg = pygame.image.load('material/images/SunFlower_00.png').convert_alpha()
wallNutImg = pygame.image.load('material/images/WallNut_00.png').convert_alpha()
peashooterImg = pygame.image.load('material/images/Peashooter_00.png').convert_alpha()

score = '500'
myfont = pygame.font.SysFont('arial', 20)
txtImg = myfont.render(score, True, (0, 0, 0))

nameList = ['劉無敵 ', '劉國臣 ', '邸家興 ', '陶倩', '大漢 ', '張新杰', '李彪 ', '紀(jì)勇博', ' 張榕', '楠姐 ', '天昊 ']

peashooterList = pygame.sprite.Group()
sunFlowerList = pygame.sprite.Group()
wallNutList = pygame.sprite.Group()
bulletList = pygame.sprite.Group()

sunList = pygame.sprite.Group()
zombieList = pygame.sprite.Group()
flagZombieList = pygame.sprite.Group()

index = 0
clock = pygame.time.Clock()

GENERATOR_SUN_EVENT = pygame.USEREVENT + 1
pygame.time.set_timer(GENERATOR_SUN_EVENT, 100)

GENERATOR_ZOMBIE_EVENT = pygame.USEREVENT + 2
pygame.time.set_timer(GENERATOR_ZOMBIE_EVENT, 5000)

GENERATOR_PEASHOOTER_EVENT = pygame.USEREVENT + 3
pygame.time.set_timer(GENERATOR_PEASHOOTER_EVENT, 2000)

GENERATOR_FLAGZOMBIE_EVENT = pygame.USEREVENT + 4
pygame.time.set_timer(GENERATOR_FLAGZOMBIE_EVENT, 10000)

choose = 0

while True:

    # print(time.time())
    clock.tick(15)
    ##如果沒有音樂流則選擇播放
    if not pygame.mixer.music.get_busy():
        pygame.mixer.music.play()
    # 啟動(dòng)消息隊(duì)列,獲取消息并處理
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type == GENERATOR_SUN_EVENT:
            # 當(dāng)前是否有太陽花對象,有幾個(gè)太陽花對象辐宾,就生成幾個(gè)太陽
            if len(sunFlowerList) > 0:
                timeNow = time.time()
                for sunFlower in sunFlowerList:
                    if timeNow - sunFlower.lasttime >= 5:
                        sunFlower.lasttime = timeNow
                        sun = Sun(sunFlower.rect)
                        sunList.add(sun)

        if event.type == GENERATOR_PEASHOOTER_EVENT:
            # 當(dāng)前是否有太陽花對象怜俐,有幾個(gè)太陽花對象,就生成幾個(gè)太陽
            if len(peashooterList) > 0:
                for peashooter in peashooterList:
                    bullet = Bullet(peashooter.rect, size)
                    bulletList.add(bullet)

        if event.type == GENERATOR_ZOMBIE_EVENT:
            randomIndex = random.randrange(0, len(nameList))
            nameOfZombie = nameList[randomIndex]
            zombie = Zombie(nameOfZombie)
            zombieList.add(zombie)

        if event.type == GENERATOR_FLAGZOMBIE_EVENT:
            flagZombie = FlagZombie()
            flagZombieList.add(flagZombie)

        if event.type == MOUSEBUTTONDOWN:
            mouse_pressed = pygame.mouse.get_pressed()
            # 判斷是否按下的事鼠標(biāo)左鍵
            if mouse_pressed[0]:
                (x, y) = pygame.mouse.get_pos()
                # print(x, y)

                # 判斷鼠標(biāo)是否點(diǎn)中了某個(gè)卡片
                if 330 <= x <= 380 and 10 <= y <= 80 and int(score) >= 50:
                    choose = 1
                elif 380 < x <= 430 and 10 <= y <= 80 and int(score) >= 50:
                    choose = 2
                elif 430 < x <= 480 and 10 <= y <= 80 and int(score) >= 100:
                    choose = 3
                elif 250 < x < 1200 and 70 < y < 600:
                    if choose == 1:
                        sunFlower = SunFlower(time.time())
                        sunFlower.rect.top = y
                        sunFlower.rect.left = x
                        sunFlowerList.add(sunFlower)
                        choose = 0

                        # 扣去太陽花相應(yīng)的分?jǐn)?shù)
                        score = int(score)
                        score -= 50
                        myfont = pygame.font.SysFont('arial', 20)
                        txtImg = myfont.render(str(score), True, (0, 0, 0))
                    if choose == 2:
                        wallNut = WallNut()
                        wallNut.rect.top = y
                        wallNut.rect.left = x
                        wallNutList.add(wallNut)
                        choose = 0

                        # 扣去太陽花相應(yīng)的分?jǐn)?shù)
                        score = int(score)
                        score -= 50
                        myfont = pygame.font.SysFont('arial', 20)
                        txtImg = myfont.render(str(score), True, (0, 0, 0))
                    if choose == 3:
                        peashooter = Peashooter()
                        peashooter.rect.top = y
                        peashooter.rect.left = x
                        peashooterList.add(peashooter)
                        choose = 0

                        # 扣去太陽花相應(yīng)的分?jǐn)?shù)
                        score = int(score)
                        score -= 100
                        myfont = pygame.font.SysFont('arial', 20)
                        txtImg = myfont.render(str(score), True, (0, 0, 0))

                for sun in sunList:
                    if sun.rect.collidepoint((x, y)):
                        # sunList.remove(sun)
                        sun.is_click = True
                        score = int(score) + 50
                        myfont = pygame.font.SysFont('arial', 20)
                        txtImg = myfont.render(str(score), True, (0, 0, 0))

    # 如果堅(jiān)果和僵尸沖突:
    # 1.僵尸要停住,動(dòng)畫要變成吃堅(jiān)果
    # 2.堅(jiān)果要掉血(n個(gè)僵尸同時(shí)吃拧揽,要掉n個(gè)血)
    # 3.堅(jiān)果血沒后慰照,要kill掉自己
    # 4.堅(jiān)果吃完后软免,僵尸要繼續(xù)向前走
    # 5.僵尸死后,堅(jiān)果的僵尸集合中要移除該僵尸
    for zombie in zombieList:
        for wallNut in wallNutList:
            if pygame.sprite.collide_mask(zombie, wallNut):
                zombie.isMeetWallNut = True
                wallNut.zombies.add(zombie)

    for zombie in flagZombieList:
        for wallNut in wallNutList:
            if pygame.sprite.collide_mask(zombie, wallNut):
                zombie.isMeetWallNut = True
                wallNut.zombies.add(zombie)

    for zombie in zombieList:
        for peashooter in peashooterList:
            if pygame.sprite.collide_mask(zombie, peashooter):
                zombie.isMeetWallNut = True
                peashooter.zombies.add(zombie)

    for zombie in flagZombieList:
        for peashooter in peashooterList:
            if pygame.sprite.collide_mask(zombie, peashooter):
                zombie.isMeetWallNut = True
                peashooter.zombies.add(zombie)

    for zombie in zombieList:
        for sunFlower in sunFlowerList:
            if pygame.sprite.collide_mask(zombie, sunFlower):
                zombie.isMeetWallNut = True
                sunFlower.zombies.add(zombie)

    for zombie in flagZombieList:
        for sunFlower in sunFlowerList:
            if pygame.sprite.collide_mask(zombie, sunFlower):
                zombie.isMeetWallNut = True
                sunFlower.zombies.add(zombie)


    for bullet in bulletList:
        for zombie in zombieList:
            if pygame.sprite.collide_mask(bullet, zombie):
                zombie.energy -= 1
                bulletList.remove(bullet)

    for bullet in bulletList:
        for flagZombie in flagZombieList:
            if pygame.sprite.collide_mask(bullet, flagZombie):
                flagZombie.energy -= 1
                bulletList.remove(bullet)


    screen.blit(backgroundImg, (0, 0))
    screen.blit(sunbackImg, (250, 0))
    screen.blit(txtImg, (270, 60))

    screen.blit(flower_seed, (330, 10))
    screen.blit(wallNut_seed, (380, 10))
    screen.blit(peashooter_seed, (430, 10))

    # 根據(jù)選中的卡片焚挠,將對應(yīng)的植物圖片膏萧,顯示在當(dāng)前鼠標(biāo)的右下角,跟隨鼠標(biāo)移動(dòng)
    (x, y) = pygame.mouse.get_pos()

    if choose == 1:
        screen.blit(sunFlowerImg, (x, y))
    if choose == 2:
        screen.blit(wallNutImg, (x, y))
    if choose == 3:
        screen.blit(peashooterImg, (x, y))

    # if index % 10 == 0:
    #     bullet = Bullet(peashooter.rect, size)
    #     spriteList.add(bullet)

    sunFlowerList.update(index)
    sunFlowerList.draw(screen)
    sunList.update(index)
    sunList.draw(screen)
    zombieList.update(index)
    zombieList.draw(screen)
    wallNutList.update(index)
    wallNutList.draw(screen)
    peashooterList.update(index)
    peashooterList.draw(screen)
    bulletList.update(index)
    bulletList.draw(screen)
    flagZombieList.update(index)
    flagZombieList.draw(screen)

    for zombie in zombieList:
        yourfont = pygame.font.SysFont('simsunnsimsun', 30)
        headpic = yourfont.render(zombie.name, True, (255, 0, 0))
        screen.blit(headpic, (zombie.rect.left + 60, zombie.rect.top - 20))

    index += 1

    pygame.display.update()

'''太陽花'''
import pygame


class SunFlower(pygame.sprite.Sprite):
    def __init__(self, lasttime):
        super(SunFlower, self).__init__()
        self.image = pygame.image.load('material/images/SunFlower_00.png').convert_alpha()
        self.images = [pygame.image.load('material/images/SunFlower_{:02d}.png'.format(i)).convert_alpha() for i in
                       range(0, 13)]
        self.rect = self.images[0].get_rect()
        self.lasttime = lasttime
        # self.rect.top = 380
        # self.rect.left = 250
        self.zombies = set()
        self.energy = 3 * 15

    def update(self, *args):
        for zoobie in self.zombies:
            if not zoobie.isAlive:
                continue
            self.energy -= 1
            if self.energy <= 0:
                for zoobie in self.zombies:
                    zoobie.isMeetWallNut = False
                self.kill()

        self.image = self.images[args[0] % len(self.images)]


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市榛泛,隨后出現(xiàn)的幾起案子蝌蹂,更是在濱河造成了極大的恐慌,老刑警劉巖曹锨,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件孤个,死亡現(xiàn)場離奇詭異,居然都是意外死亡沛简,警方通過查閱死者的電腦和手機(jī)齐鲤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來椒楣,“玉大人给郊,你說我怎么就攤上這事∨趸遥” “怎么了淆九?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長毛俏。 經(jīng)常有香客問我炭庙,道長,這世上最難降的妖魔是什么煌寇? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任焕蹄,我火速辦了婚禮,結(jié)果婚禮上阀溶,老公的妹妹穿的比我還像新娘腻脏。我一直安慰自己,他們只是感情好淌哟,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布迹卢。 她就那樣靜靜地躺著,像睡著了一般徒仓。 火紅的嫁衣襯著肌膚如雪腐碱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天掉弛,我揣著相機(jī)與錄音症见,去河邊找鬼。 笑死殃饿,一個(gè)胖子當(dāng)著我的面吹牛谋作,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播乎芳,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼遵蚜,長吁一口氣:“原來是場噩夢啊……” “哼帖池!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起吭净,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤睡汹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后寂殉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體囚巴,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年友扰,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了彤叉。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,981評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡村怪,死狀恐怖秽浇,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情实愚,我是刑警寧澤兼呵,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布兔辅,位于F島的核電站腊敲,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏维苔。R本人自食惡果不足惜碰辅,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望介时。 院中可真熱鬧没宾,春花似錦、人聲如沸沸柔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽褐澎。三九已至会钝,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間工三,已是汗流浹背迁酸。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留俭正,地道東北人奸鬓。 一個(gè)月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像掸读,于是被迫代替她去往敵國和親串远。 傳聞我的和親對象是個(gè)殘疾皇子宏多,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容