收集整理了一份《2024年最新Python全套学习资料》免费送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Python知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来
如果你需要这些资料,可以添加V无偿获取:hxbc188 (备注666)
正文
作者: marble_xu
GitHub:https://github.com/marblexu/PythonPlantsVsZombies
PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取
[python免费学习资料以及群交流解答点击即可加入
]( )
功能介绍
最近一直在给这个植物大战僵尸游戏添加新的植物和僵尸, 因为网上的图片资源有限,能加的植物和僵尸比较少, 目前进展如下。
功能实现如下:
-
支持的植物类型:太阳花,豌豆射手,寒冰射手,坚果,樱桃炸弹。新增加植物:双重豌豆射手,三重豌豆射手,食人花 ,小喷菇,土豆地雷,倭瓜。
-
支持的僵尸类型:普通僵尸,棋子僵尸,路障僵尸,铁桶僵尸。新增加读报僵尸。
-
使用json文件保存关卡信息,设置僵尸出现的时间和位置。
-
增加每关开始时选择上场植物。
-
增加除草机。
下面是游戏的截图:
如图所示,游戏中可以种植物的方格一共有45个(有5行,每行9列)。
这篇文章要介绍的是:
-
上方植物卡片栏的实现。
-
点击植物卡片,鼠标切换为植物图片。
-
鼠标移动时,判断当前在哪个方格中,并显示半透明的植物作为提示。
所有的植物卡片的名称和属性都保存在单独的list中,每个list index都对应一种植物。
比如list index 0 就是太阳花:
-
card_name_list[0] 是太阳花卡片的名字,用来获取太阳花卡片的图片。
-
plant_name_list[0] 是太阳花的名字,用来获取太阳花卡片的图片。
-
plant_sun_list[0] 是种植太阳花需要花费的太阳点数。
-
plant_frozen_time_list[0] 是太阳花的冷却时间。
每个植物卡片是一个单独的Card类,用来显示这个植物。
-
checkMouseClick函数:判断鼠标是否点击到这个卡片;
-
canClick:判断这个卡片是否能种植(有没有足够的点数,是否还在冷却时间内);
-
update 函数:通过设置图片的透明度来表示这个卡片是否能选择。
MenuBar类显示图3中的植物卡片栏:
-
self.sun_value:当前采集的太阳点数;
-
self.card_list: 植物卡片的list;
-
setupCards函数:遍历初始化__init__函数中传入这个关卡选好的植物卡片list,依次创建Card类,设置每个卡片的显示位置;
-
checkCardClick函数:检查鼠标是否点击了卡片栏上的某个植物卡片,如果选择了一个可种植的卡片,返回结果。
代码:
import pygame as pg
from … import tool
from … import constants as c
PANEL_Y_START = 87
PANEL_X_START = 22
PANEL_Y_INTERNAL = 74
PANEL_X_INTERNAL = 53
CARD_LIST_NUM = 8
card_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_PUFFSHROOM, c.CARD_POTATOMINE, c.CARD_SQUASH, c.CARD_SPIKEWEED,
c.CARD_JALAPENO, c.CARD_SCAREDYSHROOM, c.CARD_SUNSHROOM, c.CARD_ICESHROOM]
plant_name_list = [c.SUNFLOWER, c.PEASHOOTER, c.SNOWPEASHOOTER, c.WALLNUT,
c.CHERRYBOMB, c.THREEPEASHOOTER, c.REPEATERPEA, c.CHOMPER,
c.PUFFSHROOM, c.POTATOMINE, c.SQUASH, c.SPIKEWEED,
c.JALAPENO, c.SCAREDYSHROOM, c.SUNSHROOM, c.ICESHROOM]
plant_sun_list = [50, 100, 175, 50, 150, 325, 200, 150, 0, 25, 50, 100, 125, 25, 25, 75]
plant_frozen_time_list = [7500, 7500, 7500, 30000, 50000, 7500, 7500, 7500, 7500, 30000,
30000, 7500, 50000, 7500, 7500, 50000]
all_card_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
def getSunValueImage(sun_value):
font = pg.font.SysFont(None, 22)
width = 32
msg_image = font.render(str(sun_value), True, c.NAVYBLUE, c.LIGHTYELLOW)
msg_rect = msg_image.get_rect()
msg_w = msg_rect.width
image = pg.Surface([width, 17])
x = width - msg_w
image.fill(c.LIGHTYELLOW)
image.blit(msg_image, (x, 0), (0, 0, msg_rect.w, msg_rect.h))
image.set_colorkey(c.BLACK)
return image
class Card():
def init(self, x, y, name_index, scale=0.78):
self.loadFrame(card_name_list[name_index], scale)
self.rect = self.orig_image.get_rect()
self.rect.x = x
self.rect.y = y
self.name_index = name_index
self.sun_cost = plant_sun_list[name_index]
self.frozen_time = plant_frozen_time_list[name_index]
self.frozen_timer = -self.frozen_time
self.refresh_timer = 0
self.select = True
def loadFrame(self, name, scale):
frame = tool.GFX[name]
rect = frame.get_rect()
width, height = rect.w, rect.h
self.orig_image = tool.get_image(frame, 0, 0, width, height, c.BLACK, scale)
self.image = self.orig_image
def checkMouseClick(self, mouse_pos):
x, y = mouse_pos
if(x >= self.rect.x and x <= self.rect.right and
y >= self.rect.y and y <= self.rect.bottom):
return True
return False
def canClick(self, sun_value, current_time):
if self.sun_cost <= sun_value and (current_time - self.frozen_timer) > self.frozen_time:
return True
return False
def canSelect(self):
return self.select
def setSelect(self, can_select):
self.select = can_select
if can_select:
self.image.set_alpha(255)
else:
self.image.set_alpha(128)
def setFrozenTime(self, current_time):
self.frozen_timer = current_time
def createShowImage(self, sun_value, current_time):
‘’'create a card image to show cool down status
or disable status when have not enough sun value’‘’
time = current_time - self.frozen_timer
if time < self.frozen_time: #cool down status
image = pg.Surface([self.rect.w, self.rect.h])
frozen_image = self.orig_image.copy()
frozen_image.set_alpha(128)
frozen_height = (self.frozen_time - time)/self.frozen_time * self.rect.h
image.blit(frozen_image, (0,0), (0, 0, self.rect.w, frozen_height))
image.blit(self.orig_image, (0,frozen_height),
(0, frozen_height, self.rect.w, self.rect.h - frozen_height))
elif self.sun_cost > sun_value: #disable status
image = self.orig_image.copy()
image.set_alpha(192)
else:
image = self.orig_image
return image
def update(self, sun_value, current_time):
if (current_time - self.refresh_timer) >= 250:
self.image = self.createShowImage(sun_value, current_time)
self.refresh_timer = current_time
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 = 10
self.rect.y = 0
self.sun_value = sun_value
self.card_offset_x = 32
self.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_time
for card in self.card_list:
card.update(self.sun_value, self.current_time)
def createImage(self, x, y, num):
if num == 1:
return
img = self.image
rect = self.image.get_rect()
width = rect.w
height = rect.h
self.image = pg.Surface((width * num, height)).convert()
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
for i in range(num):
x = i * width
self.image.blit(img, (x,0))
self.image.set_colorkey(c.BLACK)
def setupCards(self, card_list):
self.card_list = []
x = self.card_offset_x
y = 8
for index in card_list:
x += 55
self.card_list.append(Card(x, y, index))
def checkCardClick(self, mouse_pos):
result = None
for 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)
break
return result
def checkMenuBarClick(self, mouse_pos):
x, y = mouse_pos
if(x >= self.rect.x and x <= self.rect.right and
y >= self.rect.y and y <= self.rect.bottom):
return True
return False
def decreaseSunValue(self, value):
self.sun_value -= value
def increaseSunValue(self, value):
self.sun_value += value
def setCardFrozenTime(self, plant_name):
for card in self.card_list:
if plant_name_list[card.name_index] == plant_name:
card.setFrozenTime(self.current_time)
break
def drawSunValue(self):
self.value_image = getSunValueImage(self.sun_value)
self.value_rect = self.value_image.get_rect()
self.value_rect.x = 21
self.value_rect.y = self.rect.bottom - 21
self.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 = 0
self.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 = 0
self.menu_rect.y = 0
self.panel_image = self.loadFrame(c.PANEL_BACKGROUND)
self.panel_rect = self.panel_image.get_rect()
self.panel_rect.x = 0
self.panel_rect.y = PANEL_Y_START
self.value_image = getSunValueImage(sun_value)
self.value_rect = self.value_image.get_rect()
self.value_rect.x = 21
self.value_rect.y = self.menu_rect.bottom - 21
一、Python所有方向的学习路线
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照下面的知识点去找对应的学习资源,保证自己学得较为全面。
二、Python必备开发工具
工具都帮大家整理好了,安装就可直接上手!
三、最新Python学习笔记
当我学到一定基础,有自己的理解能力的时候,会去阅读一些前辈整理的书籍或者手写的笔记资料,这些笔记详细记载了他们对一些技术点的理解,这些理解是比较独到,可以学到不一样的思路。
四、Python视频合集
观看全面零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
五、实战案例
纸上得来终觉浅,要学会跟着视频一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
六、面试宝典
简历模板
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
如果你需要这些资料,可以添加V无偿获取:hxbc188 (备注666)
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
g)
五、实战案例
纸上得来终觉浅,要学会跟着视频一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
六、面试宝典
简历模板
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
如果你需要这些资料,可以添加V无偿获取:hxbc188 (备注666)
[外链图片转存中…(img-v0CLzlS6-1713813069316)]
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!