修复水中可以直接种普通植物的bug

This commit is contained in:
星外之神 2022-04-17 19:34:26 +08:00
parent 2532f8e325
commit fdc86eb0c3
3 changed files with 24 additions and 17 deletions

View File

@ -2,6 +2,7 @@ import random
import pygame as pg
from .. import tool
from .. import constants as c
from copy import deepcopy
class Map():
def __init__(self, background_type):
@ -9,15 +10,15 @@ class Map():
if self.background_type in {c.BACKGROUND_POOL, c.BACKGROUND_FOG}:
self.width = c.GRID_POOL_X_LEN
self.height = c.GRID_POOL_Y_LEN
self.map = [[(deepcopy(c.MAP_STATE_EMPTY), deepcopy(c.MAP_STATE_WATER))[y in {2, 3}] for x in range(self.width)] for y in range(self.height)]
elif self.background_type in {c.BACKGROUND_ROOF, c.BACKGROUND_ROOFNIGHT}:
self.width = c.GRID_ROOF_X_LEN
self.height = c.GRID_ROOF_Y_LEN
self.map = [[deepcopy(c.MAP_STATE_TILE) for x in range(self.width)] for y in range(self.height)]
else:
self.width = c.GRID_X_LEN
self.height = c.GRID_Y_LEN
# 要把记录信息改成元组的话这里又得改
# 而且不同场地还不一样
self.map = [[c.MAP_STATE_EMPTY for x in range(self.width)] for y in range(self.height)]
self.map = [[deepcopy(c.MAP_STATE_EMPTY) 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
@ -33,6 +34,7 @@ class Map():
# 当然,不用元组的话字符串也行,但是得把判断植物写在母函数中,并且需要更多参数
# 这样返回的就是一个具体信息而非bool值了
# 到时候还要改一下变量名还叫isMovable不合适
#if self.map[map_y][map_x][c.]
return (self.map[map_y][map_x] == c.MAP_STATE_EMPTY)
def getMapIndex(self, x, y):
@ -69,6 +71,12 @@ class Map():
def setMapGridType(self, map_x, map_y, type):
self.map[map_y][map_x] = type
def addMapPlant(self, map_x, map_y, plantName):
self.map[map_y][map_x][c.MAP_PLANT].add(plantName)
def removeMapPlant(self, map_x, map_y, plantName):
self.map[map_y][map_x][c.MAP_PLANT].remove(plantName)
def getRandomMapIndex(self):
map_x = random.randint(0, self.width-1)
map_y = random.randint(0, self.height-1)

View File

@ -1,4 +1,4 @@
START_LEVEL_NUM = 4
START_LEVEL_NUM = 1
START_LITTLE_GAME_NUM = 1
ORIGINAL_CAPTION = 'pypvz'
@ -92,16 +92,15 @@ BACKGROUND_WALLNUTBOWLING = 6
BACKGROUND_SINGLE = 7
BACKGROUND_TRIPLE = 8
MAP_STATE_EMPTY = 0
MAP_COMMON_PLANT = 1
# 只有南瓜头
MAP_PUMPKIN_ONLY = 2
# 有南瓜头和其他植物
MAP_PUMPKIN_WITH = 3
# 可能还需要给咖啡豆定义一个状态,但是这个最好是在种植的时候判断那里是否有睡眠的蘑菇
# 睡莲与花盆更为特殊,可能需要更复杂的定义
# 可能可以用元组或者字符串
MAP_PLANT = 'plantnames'
MAP_SLEEP = 'sleep' # 有没有休眠的蘑菇,作是否能种植咖啡豆的判断
MAP_PLOT_TYPE = 'plotType'
MAP_GRASS = 'grass'
MAP_WATER = 'water'
MAP_TILE = 'tile' # 指屋顶上的瓦片
MAP_STATE_EMPTY = {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_GRASS} # 由于同一格显然不可能种两个相同的植物,所以用集合
MAP_STATE_WATER = {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_WATER}
MAP_STATE_TILE = {MAP_PLANT:set(), MAP_SLEEP:False, MAP_PLOT_TYPE:MAP_TILE}
BACKGROUND_OFFSET_X = 220
MAP_OFFSET_X = 35

View File

@ -146,7 +146,7 @@ class Level(tool.State):
print('initBowlingMap')
for x in range(3, self.map.width):
for y in range(self.map.height):
self.map.setMapGridType(x, y, c.MAP_COMMON_PLANT)
self.map.setMapGridType(x, y, c.MAP_STATE_TILE) # 将坚果保龄球红线右侧视为屋顶的瓦片以达到不能直接种植植物的效果
def initState(self):
# 小游戏才有CHOOSEBAR_TYPE
@ -530,7 +530,7 @@ class Level(tool.State):
self.menubar.deleateCard(self.select_plant)
if self.bar_type != c.CHOSSEBAR_BOWLING:
self.map.setMapGridType(map_x, map_y, c.MAP_COMMON_PLANT)
self.map.addMapPlant(map_x, map_y, self.plant_name)
self.removeMouseImage()
#print('addPlant map[%d,%d], grid pos[%d, %d] pos[%d, %d]' % (map_x, map_y, x, y, pos[0], pos[1]))
@ -671,7 +671,7 @@ class Level(tool.State):
map_x, map_y = self.map.getMapIndex(x, y)
if self.bar_type != c.CHOSSEBAR_BOWLING:
# 更改地图类型、添加南瓜头、睡莲、花盆后可能也需要改这里
self.map.setMapGridType(map_x, map_y, c.MAP_STATE_EMPTY)
self.map.removeMapPlant(map_x, map_y, plant.name)
# 用铲子铲不用触发植物功能
if not shovel:
if (plant.name == c.CHERRYBOMB or plant.name == c.JALAPENO or