码迷,mamicode.com
首页 > 其他好文 > 详细

pygame之植物大战僵尸-peter

时间:2019-11-15 12:17:22      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:动画   植物大战僵尸   window   Plan   load   植物大战   event   检测   elf   

# 导入相关库并初始化pygame
import pygame,sys,os
from pygame import *
pygame.init()
# 窗口居中显示
os.environ[‘SDL_VIDEO_WINDOW_POS‘] = ‘%d,%d‘%(50,50)
# 设置游戏窗口、颜色和标题
canvas = pygame.display.set_mode((1200,600))
canvas.fill((255,255,255))
pygame.display.set_caption(‘植物大战僵尸‘)
# 创建关闭窗口方法
def handleEvent():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
# 加载背景、坚果图片
bg = pygame.image.load(‘images/img/bg.jpg‘)
nut1 = pygame.image.load(‘images/plants/TallNut.gif‘)
nut2 = pygame.image.load(‘images/plants/TallnutCracked1.gif‘)
nut3 = pygame.image.load(‘images/plants/TallnutCracked2.gif‘)
nut4 = pygame.image.load(‘images/plants/WallNut.gif‘)
won = pygame.image.load(‘images/img/won.png‘)
# 僵尸移动列表
moveList = []
for x in range(13):
    if x < 9:
        moveList.append(pygame.image.load(‘images/move/0‘+str(x+1)+‘.png‘))
    else:
        moveList.append(pygame.image.load(‘images/move/‘+str(x+1)+‘.png‘))
# 僵尸攻击列表
attackList = []
for x in range(31,42):
    attackList.append(pygame.image.load(‘images/attack/‘+str(x)+‘.png‘))
# 僵尸站立行走列表
standList = []
for x in range(21,27):
    standList.append(pygame.image.load(‘images/stand/‘+str(x)+‘.png‘))
MOVE = 1
ATTACK = 2
STAND = 3
# 创建僵尸类Zombie()
class Zombie():
    def __init__(self,x,y):
        self.x   = x
        self.y   = y
        self.index = 0
        self.state = MOVE
        self.img = moveList[self.index]
    def paint(self):
        canvas.blit(self.img,(self.x,self.y))
    def move(self):
        self.x -= 10
    # 创建僵尸动画播放(图片切换)方法
    def animation(self):
        if self.state == MOVE:
            self.img = moveList[self.index%13]
            self.move()
        elif self.state == ATTACK:
            self.img = attackList[self.index%11]
        elif self.state == STAND:
            self.img = standList[self.index%6]
        self.index += 1
# 创建坚果类Nut()
class Nut():
    def __init__(self,x,y,width,height,img):
        self.x      = x
        self.y      = y
        self.width  = width
        self.height = height
        self.img    = img
        self.life   = 100
    def paint(self):
        canvas.blit(self.img,(self.x,self.y))
nut = Nut(500,300,83,119,nut1)
zombie = Zombie(1000,250)
# 创建check()方法:检测僵尸碰到坚果时,坚果发生变化。
def check(n,z):
    if zombie.x <= nut.x + nut.width/5:
        zombie.state = ATTACK
        if nut.life >= 75:
            nut.img = nut2
        elif nut.life >= 50:
            nut.img = nut3
        elif nut.life >= 25:
            nut.img = nut4
        elif nut.life < 0 and zombie.x > 200:
            zombie.state = MOVE
        elif zombie.x <= 200:
            zombie.state = STAND
            canvas.blit(won,(300,0))
        nut.life -= 1
while True:
    canvas.blit(bg,(0,0))
    zombie.paint()
    zombie.animation()
    if nut.life > 0:
        nut.paint()
    check(nut,zombie)
    pygame.time.delay(100)
    # 更新屏幕并调用handleEvent()方法
    pygame.display.update()
    handleEvent()

pygame之植物大战僵尸-peter

标签:动画   植物大战僵尸   window   Plan   load   植物大战   event   检测   elf   

原文地址:https://www.cnblogs.com/dongks/p/11865241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!