Pygame实战:据说—这是一款还原度超高的植物大战僵尸游戏,你感受下......

news/2025/3/5 2:07:36/

导语

哈喽!大家好,我是木木子!

又到了每日游戏更新系列,看到这么如下.gif是不是让你想起来了童年吖~

植物大战僵尸的人气可谓是经久不衰,晃着脑袋生产阳光的向日葵,突突突吐着子弹的豌豆射手!​

行动迟缓种类丰富的僵尸……印象最深的是“僵尸吃掉了你的脑子!”还有疯狂的戴夫,无一不唤醒着我们的童年记忆

山民们闯到哪一关了?解锁了哪些植物?

对!没错,就是这个大工程今天带大家做一款Python版本的植物大战僵尸游戏!

正文

首先准备好相应的素材如下:【超多仅展示小部分】

定义所有的植物卡片的名称和属性都保存在单独的list中,每个list index都对应一种植物,每个植物卡片是一个单独的Card类,用来显示这个植物,然后每个植设置植物的卡片栏目。

import pygame as pg
from .. import tool
from .. import constants as cPANEL_Y_START = 87
PANEL_X_START = 22
PANEL_Y_INTERNAL = 74
PANEL_X_INTERNAL = 53
CARD_LIST_NUM = 8card_name_list = [c.CARD_SUNFLOWER, c.CARD_PEASHOOTER, c.CARD_SNOWPEASHOOTER, c.CARD_WALLNUT,c.CARD_CHERRYBOMB, c.CARD_THREEPEASHOOTER, c.CARD_REPEATERPEA, c.CARD_CHOMPER,c.CARD_PUFFMUSHROOM, c.CARD_POTATOMINE, c.CARD_SQUASH]
plant_name_list = [c.SUNFLOWER, c.PEASHOOTER, c.SNOWPEASHOOTER, c.WALLNUT,c.CHERRYBOMB, c.THREEPEASHOOTER, c.REPEATERPEA, c.CHOMPER,c.PUFFMUSHROOM, c.POTATOMINE, c.SQUASH]
plant_sun_list = [50, 100, 175, 50, 150, 325, 200, 150, 0, 25, 50]
plant_frozen_time_list = [0, 5000, 5000, 10000, 5000, 5000, 5000, 5000, 8000, 8000, 8000]
all_card_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]def getSunValueImage(sun_value):font = pg.font.SysFont(None, 22)width = 32msg_image = font.render(str(sun_value), True, c.NAVYBLUE, c.LIGHTYELLOW)msg_rect = msg_image.get_rect()msg_w = msg_rect.widthimage = pg.Surface([width, 17])x = width - msg_wimage.fill(c.LIGHTYELLOW)image.blit(msg_image, (x, 0), (0, 0, msg_rect.w, msg_rect.h))image.set_colorkey(c.BLACK)return imageclass Card():def __init__(self, x, y, name_index, scale=0.78):self.loadFrame(card_name_list[name_index], scale)self.rect = self.image.get_rect()self.rect.x = xself.rect.y = yself.name_index = name_indexself.sun_cost = plant_sun_list[name_index]self.frozen_time = plant_frozen_time_list[name_index]self.frozen_timer = -self.frozen_timeself.select = Truedef loadFrame(self, name, scale):frame = tool.GFX[name]rect = frame.get_rect()width, height = rect.w, rect.hself.image = tool.get_image(frame, 0, 0, width, height, c.BLACK, scale)def checkMouseClick(self, mouse_pos):x, y = mouse_posif(x >= self.rect.x and x <= self.rect.right andy >= self.rect.y and y <= self.rect.bottom):return Truereturn Falsedef canClick(self, sun_value, current_time):if self.sun_cost <= sun_value and (current_time - self.frozen_timer) > self.frozen_time:return Truereturn Falsedef canSelect(self):return self.selectdef setSelect(self, can_select):self.select = can_selectif can_select:self.image.set_alpha(255)else:self.image.set_alpha(128)def setFrozenTime(self, current_time):self.frozen_timer = current_timedef update(self, sun_value, current_time):if (self.sun_cost > sun_value or(current_time - self.frozen_timer) <= self.frozen_time):self.image.set_alpha(128)else:self.image.set_alpha(255)def draw(self, surface):surface.blit(self.image, self.rect)class MenuBar():def __init__(self, card_list, sun_value):self.loadFrame(c.MENUBAR_BACKGROUND)self.rect = self.image.get_rect()self.rect.x = 10self.rect.y = 0self.sun_value = sun_valueself.card_offset_x = 32self.setupCards(card_list)def loadFrame(self, name):frame = tool.GFX[name]rect = frame.get_rect()frame_rect = (rect.x, rect.y, rect.w, rect.h)self.image = tool.get_image(tool.GFX[name], *frame_rect, c.WHITE, 1)def update(self, current_time):self.current_time = current_timefor card in self.card_list:card.update(self.sun_value, self.current_time)def createImage(self, x, y, num):if num == 1:returnimg = self.imagerect = self.image.get_rect()width = rect.wheight = rect.hself.image = pg.Surface((width * num, height)).convert()self.rect = self.image.get_rect()self.rect.x = xself.rect.y = yfor i in range(num):x = i * widthself.image.blit(img, (x,0))self.image.set_colorkey(c.BLACK)def setupCards(self, card_list):self.card_list = []x = self.card_offset_xy = 8for index in card_list:x += 55self.card_list.append(Card(x, y, index))def checkCardClick(self, mouse_pos):result = Nonefor card in self.card_list:if card.checkMouseClick(mouse_pos):if card.canClick(self.sun_value, self.current_time):result = (plant_name_list[card.name_index], card.sun_cost)breakreturn resultdef checkMenuBarClick(self, mouse_pos):x, y = mouse_posif(x >= self.rect.x and x <= self.rect.right andy >= self.rect.y and y <= self.rect.bottom):return Truereturn Falsedef decreaseSunValue(self, value):self.sun_value -= valuedef increaseSunValue(self, value):self.sun_value += valuedef setCardFrozenTime(self, plant_name):for card in self.card_list:if plant_name_list[card.name_index] == plant_name:card.setFrozenTime(self.current_time)breakdef drawSunValue(self):self.value_image = getSunValueImage(self.sun_value)self.value_rect = self.value_image.get_rect()self.value_rect.x = 21self.value_rect.y = self.rect.bottom - 21self.image.blit(self.value_image, self.value_rect)def draw(self, surface):self.drawSunValue()surface.blit(self.image, self.rect)for card in self.card_list:card.draw(surface)class Panel():def __init__(self, card_list, sun_value):self.loadImages(sun_value)self.selected_cards = []self.selected_num = 0self.setupCards(card_list)def loadFrame(self, name):frame = tool.GFX[name]rect = frame.get_rect()frame_rect = (rect.x, rect.y, rect.w, rect.h)return tool.get_image(tool.GFX[name], *frame_rect, c.WHITE, 1)def loadImages(self, sun_value):self.menu_image = self.loadFrame(c.MENUBAR_BACKGROUND)self.menu_rect = self.menu_image.get_rect()self.menu_rect.x = 0self.menu_rect.y = 0self.panel_image = self.loadFrame(c.PANEL_BACKGROUND)self.panel_rect = self.panel_image.get_rect()self.panel_rect.x = 0self.panel_rect.y = PANEL_Y_STARTself.value_image = getSunValueImage(sun_value)self.value_rect = self.value_image.get_rect()self.value_rect.x = 21self.value_rect.y = self.menu_rect.bottom - 21self.button_image =  self.loadFrame(c.START_BUTTON)self.button_rect = self.button_image.get_rect()self.button_rect.x = 155self.button_rect.y = 547def setupCards(self, card_list):self.card_list = []x = PANEL_X_START - PANEL_X_INTERNALy = PANEL_Y_START + 43 - PANEL_Y_INTERNALfor i, index in enumerate(card_list):if i % 8 == 0:x = PANEL_X_START - PANEL_X_INTERNALy += PANEL_Y_INTERNALx += PANEL_X_INTERNALself.card_list.append(Card(x, y, index, 0.75))def checkCardClick(self, mouse_pos):delete_card = Nonefor card in self.selected_cards:if delete_card: # when delete a card, move right cards to leftcard.rect.x -= 55elif card.checkMouseClick(mouse_pos):self.deleteCard(card.name_index)delete_card = cardif delete_card:self.selected_cards.remove(delete_card)self.selected_num -= 1if self.selected_num == CARD_LIST_NUM:returnfor card in self.card_list:if card.checkMouseClick(mouse_pos):if card.canSelect():self.addCard(card)breakdef addCard(self, card):card.setSelect(False)y = 8x = 78 + self.selected_num * 55self.selected_cards.append(Card(x, y, card.name_index))self.selected_num += 1def deleteCard(self, index):self.card_list[index].setSelect(True)def checkStartButtonClick(self, mouse_pos):if self.selected_num < CARD_LIST_NUM:return Falsex, y = mouse_posif (x >= self.button_rect.x and x <= self.button_rect.right andy >= self.button_rect.y and y <= self.button_rect.bottom):return Truereturn Falsedef getSelectedCards(self):card_index_list = []for card in self.selected_cards:card_index_list.append(card.name_index)return card_index_listdef draw(self, surface):self.menu_image.blit(self.value_image, self.value_rect)surface.blit(self.menu_image, self.menu_rect)surface.blit(self.panel_image, self.panel_rect)for card in self.card_list:card.draw(surface)for card in self.selected_cards:card.draw(surface)if self.selected_num == CARD_LIST_NUM:surface.blit(self.button_image, self.button_rect)

植物卡片栏目截图如下:

设置的几种植物截图如下:

然后使用setupMouseImage 函数实现鼠标图片切换为选中的植物。

    def setupMouseImage(self, plant_name, plant_cost):frame_list = tool.GFX[plant_name]if plant_name in tool.PLANT_RECT:data = tool.PLANT_RECT[plant_name]x, y, width, height = data['x'], data['y'], data['width'], data['height']else:x, y = 0, 0rect = frame_list[0].get_rect()width, height = rect.w, rect.hif plant_name == c.POTATOMINE or plant_name == c.SQUASH:color = c.WHITEelse:color = c.BLACKself.mouse_image = tool.get_image(frame_list[0], x, y, width, height, color, 1)self.mouse_rect = self.mouse_image.get_rect()pg.mouse.set_visible(False)self.drag_plant = Trueself.plant_name = plant_nameself.plant_cost = plant_costdef removeMouseImage(self):pg.mouse.set_visible(True)self.drag_plant = Falseself.mouse_image = Noneself.hint_image = Noneself.hint_plant = False

效果图如下:开始界面只有一个关卡,but一个关卡有不同的关。

​嗯哼~好啦其实后面关卡有惊喜的啦~这里就不透露了~

总结

这款植物大战僵尸游戏界面还是蛮不错的啦 还原度很高~

💖关注小编——获取更多精彩内容哦!

记得三连哦~mua 你们的支持是我最大的动力!!


http://www.ppmy.cn/news/713272.html

相关文章

Python 开发植物大战僵尸游戏

点击上方“码农突围”&#xff0c;马上关注&#xff0c;每天早上8:50准时推送 真爱&#xff0c;请置顶或星标 作者&#xff1a;楷楷 链接&#xff1a;https://segmentfault.com/a/1190000019418065 开发思路 完整项目地址&#xff1a; https://github.com/371854496/pygame 觉得…

计算机科学与技术班徽图片,学校运动会入场广播词

导语&#xff1a;特色创新谋发展&#xff0c;春风化雨育英才&#xff0c;学校运动会到了&#xff0c;下面是由小编整理的关于。欢迎阅读&#xff01; 篇一&#xff1a;运动会入场广播词 现在向我们走来的是信息管理学院代表队&#xff01;学院现拥有四个本科专业&#xff0c;在…

cocos2dx 植物大战僵尸 7 向日葵和双生向日葵

这个是向日葵的贴图&#xff0c;由于只找到了这样的资源图片&#xff0c;也就这样用了&#xff0c;不过这样做好处还是有的。使用遮罩让这两个向日葵只显示一个&#xff0c;当处于正常动画时显示上方的图片&#xff0c;当要生成太阳时就显示下方的图片。&#xff08;同双生向日…

针对“扫雷“和“植物大战僵尸“游戏,分析,扫描,阳光值,植物,金币,僵尸的分析逆向

《软件逆向分析》 2022年9月 目录 {#目录 .TOC-Heading} [一、实验工具介绍 3](#一实验工具介绍) [二、针对"扫雷"游戏 3](#二针对扫雷游戏) [2.1分析"初级"、"中级"和"高级"的棋盘内存地址范围 3](#分析初级中级和高级的棋盘内存…

python将视频分解为图片+将图片合成为视频

系列文章目录 文章目录 系列文章目录前言一、python视频拆分图片合成(源码一)1.python视频拆分1.python图片合成 二、python视频拆分图片合成(源码二)三、python视频拆分(源码三)总结 前言 一、python视频拆分图片合成(源码一) 1.python视频拆分 import cv2def video2frame(v…

Linux向日葵同步剪贴板,Windows远程桌面可双向复制粘贴图片了,向日葵客户端9.0.3更新...

无人值守远程桌面调取文档时&#xff0c;有时可能只是一段文字&#xff0c;我们都需要通过邮箱或两端都登录社交应用来传输&#xff0c;然而首先你得有两个社交账户&#xff0c;或者另一端得有人接收文件。懒癌症晚期者和高效人士要问了&#xff0c;有没有更方便的办法&#xf…

Linux向日葵同步剪贴板,远程桌面可双向复制粘贴图片 向日葵客户端9.0.3更新

即使是通过ctrlc、ctrlv的快捷方式亦可将被控电脑中的文本复制粘贴到本地。向日葵是一款阳光的控制手机或电脑的远程软件&#xff0c;主要功能是远程桌面、远程文件、远程摄像头、远程开机等。支持Windows、Mac、Linux、Android、iOS等主流系统相互穿越控制。 (手机通过向日葵控…

微服务架构介绍及SpringCloudAlibaba组件介绍

单体架构vs微服务架构 单机架构 什么是单体架构 一个归档包&#xff08;例如war格式&#xff09;包含了应用所有功能的应用程序&#xff0c;我们通常称之为单体应用。架构单体应用的方法论&#xff0c;我们称之为单体应用架构。&#xff08;就是一个war包打天下&#xff09;…