|
楼主 |
发表于 2022-5-31 08:22:59
|
显示全部楼层
这是代码
Determine whether the current mouse position can plant plants.py
def canSeedPlant(self):
x, y = pg.mouse.get_pos()
return self.map.showPlant(x, y)
​
def setupHintImage(self):
pos = self.canSeedPlant()
if pos and self.mouse_image:
if (self.hint_image and pos[0] == self.hint_rect.x and
pos[1] == self.hint_rect.y):
return
width, height = self.mouse_rect.w, self.mouse_rect.h
image = pg.Surface([width, height])
image.blit(self.mouse_image, (0, 0), (0, 0, width, height))
image.set_colorkey(c.BLACK)
image.set_alpha(128)
self.hint_image = image
self.hint_rect = image.get_rect()
self.hint_rect.centerx = pos[0]
self.hint_rect.bottom = pos[1]
self.hint_plant = True
else:
self.hint_plant = False
In which box is the hint.py
MAP_EMPTY = 0
MAP_EXIST = 1
​
​
class Map():
def __init__(self, width, height):
self.width = width
self.height = height
self.map = [[0 for x in range(self.width)] for y in range(self.height)]
​
​
def isValid(self, map_x, map_y):
if (map_x < 0 or map_x >= self.width or
map_y < 0 or map_y >= self.height):
return False
return True
​
def isMovable(self, map_x, map_y):
return (self.map[map_y][map_x] == c.MAP_EMPTY)
​
def getMapIndex(self, x, y):
x -= c.MAP_OFFSET_X
y -= c.MAP_OFFSET_Y
return (x // c.GRID_X_SIZE, y // c.GRID_Y_SIZE)
​
def getMapGridPos(self, map_x, map_y):
return (map_x * c.GRID_X_SIZE + c.GRID_X_SIZE//2 + c.MAP_OFFSET_X,
map_y * c.GRID_Y_SIZE + c.GRID_Y_SIZE//5 * 3 + c.MAP_OFFSET_Y)
​
def setMapGridType(self, map_x, map_y, type):
self.map[map_y][map_x] = type
​
​
def getRandomMapIndex(self):
map_x = random.randint(0, self.width-1)
map_y = random.randint(0, self.height-1)
return (map_x, map_y)
​
​
def showPlant(self, x, y):
pos = None
map_x, map_y = self.getMapIndex(x, y)
if self.isValid(map_x, map_y) and self.isMovable(map_x, map_y):
pos = self.getMapGridPos(map_x, map_y)
return pos
main.py
将 pygame 导入为 pg
from .. 导入工具
from .. 将常量导入为 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_REPEAERPEA,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]
植物名称列表 = [c.SUNFLOWER,c.PEASHOOTER,c.SNOWPEASHOOTER,c.WALLNUT,
c.CHERRYBOMB,c.THREEPEASHOOTER,c.REPEAERPEA,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):
字体 = pg.font.SysFont(无,22)
宽度 = 32
msg_image = font.render(str(sun_value), True, c.NAVYBLUE, c.LIGHTYELLOW)
msg_rect = msg_image.get_rect()
msg_w = msg_rect.width
​​​
图像 = pg.Surface([宽度, 17])
x = 宽度 - 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)
返回图片
​​​
类卡():
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 = 真
​​​
def loadFrame(self, name, scale):
框架 = 工具.GFX [名称]
rect = frame.get_rect()
宽度,高度 = 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 和 y <= self.rect.bottom):
返回真
返回假
​​​
def canClick(self, sun_value, current_time):
如果 self.sun_cost <= sun_value 并且 (current_time - self.frozen_timer) > self.frozen_time:
返回真
返回假
​​​
定义可以选择(自我):
返回 self.select
​​​
def setSelect(self, can_select):
self.select = can_select
如果可以选择:
self.image.set_alpha(255)
别的:
self.image.set_alpha(128)
​​​
def setFrozenTime(self, current_time):
self.frozen_timer = current_time
​​​
def createShowImage(self, sun_value, current_time):
'''创建卡片图像以显示冷却状态
或当太阳值不足时禁用状态'''
时间 = current_time - self.frozen_timer
if time < self.frozen_time: #cool down status
图像 = pg.Surface([self.rect.w, self.rect.h])
freeze_image = self.orig_image.copy()
freeze_image.set_alpha(128)
freeze_height = (self.frozen_time - time)/self.frozen_time * self.rect.h
image.blit(frozen_image, (0,0), (0, 0, self.rect.w, freeze_height))
image.blit(self.orig_image, (0,frozen_height),
(0, freeze_height, self.rect.w, self.rect.h - freeze_height))
elif self.sun_cost > sun_value: #disable 状态
图像 = self.orig_image.copy()
image.set_alpha(192)
别的:
图像 = self.orig_image
返回图片
​​​
def update(self, sun_value, current_time):
如果 (current_time - self.refresh_timer) >= 250:
self.image = self.createShowImage(sun_value, current_time)
self.refresh_timer = current_time
​​​
def 绘制(自我,表面):
surface.blit(self.image,self.rect)
​​​
类菜单栏():
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(自我,名称):
框架 = 工具.GFX [名称]
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)
​​​
定义更新(自我,当前时间):
self.current_time = current_time
对于 self.card_list 中的卡:
card.update(self.sun_value, self.current_time)
​​​
def createImage(self, x, y, num):
如果数 == 1:
返回
img = self.image
rect = self.image.get_rect()
宽度 = rect.w
高度 = rect.h
self.image = pg.Surface((width * num, height)).convert()
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
对于范围内的 i (num):
x = i * 宽度
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
对于 card_list 中的索引:
x += 55
self.card_list.append(卡片(x, y, index))
​​​
def checkCardClick(self, mouse_pos):
结果 = 无
对于 self.card_list 中的卡:
如果 card.checkMouseClick(mouse_pos):
如果 card.canClick(self.sun_value, self.current_time):
结果 = (plant_name_list[card.name_index], card.sun_cost)
休息
返回结果
def checkMenuBarClick(self, mouse_pos):
x, y = mouse_pos
if(x >= self.rect.x and x <= self.rect.right and
y >= self.rect.y 和 y <= self.rect.bottom):
返回真
返回假
​​​
def 减少SunValue(自我,价值):
self.sun_value -= 值
​​​
def 增加SunValue(自我,价值):
self.sun_value += 值
​​​
def setCardFrozenTime(自我,植物名称):
对于 self.card_list 中的卡:
如果植物名称列表[card.name_index] == 植物名称:
card.setFrozenTime(self.current_time)
休息
​​​
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 绘制(自我,表面):
self.drawSunValue()
surface.blit(self.image,self.rect)
对于 self.card_list 中的卡:
card.draw(表面)
​​​
类面板():
def __init__(self, card_list, sun_value):
self.loadImages(sun_value)
self.selected_cards = []
self.selected_num = 0
self.setupCards(card_list)
​​​
def loadFrame(自我,名称):
框架 = 工具.GFX [名称]
rect = frame.get_rect()
frame_rect = (rect.x, rect.y, rect.w, rect.h)
​​​
返回 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
​​​
self.button_image = self.loadFrame(c.START_BUTTON)
self.button_rect = self.button_image.get_rect()
self.button_rect.x = 155
self.button_rect.y = 547
​​​
def setupCards(self, card_list):
self.card_list = []
x = PANEL_X_START - PANEL_X_INTERNAL
y = PANEL_Y_START + 43 - PANEL_Y_INTERNAL
对于 i,枚举中的索引(card_list):
如果我 % 8 == 0:
x = PANEL_X_START - PANEL_X_INTERNAL
y += PANEL_Y_INTERNAL
x += PANEL_X_INTERNAL
self.card_list.append(Card(x, y, index, 0.75))
​​​
def checkCardClick(self, mouse_pos):
delete_card = 无
对于 self.selected_cards 中的卡片:
if delete_card: # 删除卡片时,将右边的卡片向左移动
card.rect.x -= 55
elif card.checkMouseClick(mouse_pos):
self.deleteCard(card.name_index)
delete_card = 卡片
​​​
如果删除卡:
self.selected_cards.remove(delete_card)
self.selected_num -= 1
​​​
如果 self.selected_num == CARD_LIST_NUM:
返回
​​​
对于 self.card_list 中的卡:
如果 card.checkMouseClick(mouse_pos):
如果 card.canSelect():
self.addCard(卡)
休息
​​​
def addCard(自我,卡):
card.setSelect(假)
y = 8
x = 78 + self.selected_num * 55
self.selected_cards.append(卡(x, y, card.name_index))
self.selected_num += 1
​​​
def deleteCard(自我,索引):
self.card_list[index].setSelect(True)
​​​
def checkStartButtonClick(self, mouse_pos):
如果 self.selected_num < CARD_LIST_NUM:
返回假
​​​
x, y = mouse_pos
if (x >= self.button_rect.x and x <= self.button_rect.right and
y >= self.button_rect.y 和 y <= self.button_rect.bottom):
返回真
返回假
​​​
def getSelectedCards(self):
card_index_list = []
对于 self.selected_cards 中的卡片:
card_index_list.append(card.name_index)
返回 card_index_list
​​​
def 绘制(自我,表面):
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)
对于 self.card_list 中的卡:
card.draw(表面)
对于 self.selected_cards 中的卡片:
card.draw(表面)
​​​
如果 self.selected_num == CARD_LIST_NUM:
surface.blit(self.button_image,self.button_rect)
Mouse picture switch.py
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, 0
rect = frame_list[0].get_rect()
width, height = rect.w, rect.h
​
​
if plant_name == c.POTATOMINE or plant_name == c.SQUASH:
color = c.WHITE
else:
color = c.BLACK
self.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 = True
self.plant_name = plant_name
self.plant_cost = plant_cost
​
​
def drawMouseShow(self, surface):
if self.hint_plant:
surface.blit(self.hint_image, self.hint_rect)
x, y = pg.mouse.get_pos()
self.mouse_rect.centerx = x
self.mouse_rect.centery = y
surface.blit(self.mouse_image, self.mouse_rect) |
|