diff --git a/resources/graphics/Cards/card_cherrybomb_move.png b/resources/graphics/Cards/card_cherrybomb_move.png new file mode 100644 index 0000000..03561e0 Binary files /dev/null and b/resources/graphics/Cards/card_cherrybomb_move.png differ diff --git a/resources/graphics/Cards/card_chomper_move.png b/resources/graphics/Cards/card_chomper_move.png new file mode 100644 index 0000000..8a30884 Binary files /dev/null and b/resources/graphics/Cards/card_chomper_move.png differ diff --git a/resources/graphics/Cards/card_peashooter_move.png b/resources/graphics/Cards/card_peashooter_move.png new file mode 100644 index 0000000..d842730 Binary files /dev/null and b/resources/graphics/Cards/card_peashooter_move.png differ diff --git a/resources/graphics/Cards/card_potatomine_move.png b/resources/graphics/Cards/card_potatomine_move.png new file mode 100644 index 0000000..ab9a7e6 Binary files /dev/null and b/resources/graphics/Cards/card_potatomine_move.png differ diff --git a/resources/graphics/Cards/card_redwallnut_move.png b/resources/graphics/Cards/card_redwallnut_move.png new file mode 100644 index 0000000..11a6a1f Binary files /dev/null and b/resources/graphics/Cards/card_redwallnut_move.png differ diff --git a/resources/graphics/Cards/card_repeaterpea_move.png b/resources/graphics/Cards/card_repeaterpea_move.png new file mode 100644 index 0000000..d6ead86 Binary files /dev/null and b/resources/graphics/Cards/card_repeaterpea_move.png differ diff --git a/resources/graphics/Cards/card_snowpea_move.png b/resources/graphics/Cards/card_snowpea_move.png new file mode 100644 index 0000000..31f716e Binary files /dev/null and b/resources/graphics/Cards/card_snowpea_move.png differ diff --git a/resources/graphics/Cards/card_wallnut_move.png b/resources/graphics/Cards/card_wallnut_move.png new file mode 100644 index 0000000..f9bc6e8 Binary files /dev/null and b/resources/graphics/Cards/card_wallnut_move.png differ diff --git a/resources/graphics/Plants/WallNut/WallNutBowling/WallNutBowling_0.png b/resources/graphics/Plants/WallNut/WallNutBowling/WallNutBowling_0.png new file mode 100644 index 0000000..673f40f Binary files /dev/null and b/resources/graphics/Plants/WallNut/WallNutBowling/WallNutBowling_0.png differ diff --git a/resources/graphics/Screen/MoveBackground.png b/resources/graphics/Screen/MoveBackground.png new file mode 100644 index 0000000..d6dd1c2 Binary files /dev/null and b/resources/graphics/Screen/MoveBackground.png differ diff --git a/source/component/menubar.py b/source/component/menubar.py index a9335da..a8f40e0 100644 --- a/source/component/menubar.py +++ b/source/component/menubar.py @@ -1,5 +1,6 @@ __author__ = 'marble_xu' +import random import pygame as pg from .. import tool from .. import constants as c @@ -40,6 +41,16 @@ def getSunValueImage(sun_value): image.set_colorkey(c.BLACK) return image +def getCardPool(data): + card_pool = [] + for card in data: + tmp = card['name'] + for i,name in enumerate(plant_name_list): + if name == tmp: + card_pool.append(i) + break + return card_pool + class Card(): def __init__(self, x, y, name_index, scale=0.78): self.loadFrame(card_name_list[name_index], scale) @@ -167,7 +178,7 @@ class MenuBar(): 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) + result = (plant_name_list[card.name_index], card) break return result @@ -309,4 +320,119 @@ class Panel(): card.draw(surface) if self.selected_num == CARD_LIST_NUM: - surface.blit(self.button_image, self.button_rect) \ No newline at end of file + surface.blit(self.button_image, self.button_rect) + +class MoveCard(): + def __init__(self, x, y, name_index, scale=0.78): + name = card_name_list[name_index] + '_move' + self.loadFrame(name, scale) + self.rect = self.orig_image.get_rect() + self.rect.x = x + self.rect.y = y + self.rect.w = 1 + self.image = self.createShowImage() + + self.name_index = name_index + self.move_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.orig_rect = self.orig_image.get_rect() + 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 createShowImage(self): + '''create a part card image when card appears from left''' + if self.rect.w < self.orig_rect.w: #create a part card image + image = pg.Surface([self.rect.w, self.rect.h]) + image.blit(self.orig_image, (0, 0), (0, 0, self.rect.w, self.rect.h)) + self.rect.w += 1 + else: + image = self.orig_image + return image + + def update(self, left_x, current_time): + if self.move_timer == 0: + self.move_timer = current_time + elif (current_time - self.move_timer) >= c.CARD_MOVE_TIME: + if self.rect.x > left_x: + self.rect.x -= 1 + self.image = self.createShowImage() + self.move_timer += c.CARD_MOVE_TIME + + def draw(self, surface): + surface.blit(self.image, self.rect) + +class MoveBar(): + def __init__(self, card_pool): + self.loadFrame(c.MOVEBAR_BACKGROUND) + self.rect = self.image.get_rect() + self.rect.x = 90 + self.rect.y = 0 + + self.card_start_x = self.rect.x + 8 + self.card_end_x = self.rect.right - 5 + self.card_pool = card_pool + self.card_list = [] + self.create_timer = -c.MOVEBAR_CARD_FRESH_TIME + + 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 createCard(self): + if len(self.card_list) > 0 and self.card_list[-1].rect.right > self.card_end_x: + return False + x = self.card_end_x + y = 6 + index = random.randint(0, len(self.card_pool) - 1) + self.card_list.append(MoveCard(x, y, self.card_pool[index])) + return True + + def update(self, current_time): + self.current_time = current_time + left_x = self.card_start_x + for card in self.card_list: + card.update(left_x, self.current_time) + left_x = card.rect.right + 1 + + if(self.current_time - self.create_timer) > c.MOVEBAR_CARD_FRESH_TIME: + if self.createCard(): + self.create_timer = self.current_time + + def checkCardClick(self, mouse_pos): + result = None + for index, card in enumerate(self.card_list): + if card.checkMouseClick(mouse_pos): + result = (plant_name_list[card.name_index], card) + 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 deleateCard(self, card): + self.card_list.remove(card) + + def draw(self, surface): + surface.blit(self.image, self.rect) + for card in self.card_list: + card.draw(surface) \ No newline at end of file diff --git a/source/constants.py b/source/constants.py index 3c37bb6..066d9f1 100644 --- a/source/constants.py +++ b/source/constants.py @@ -56,9 +56,17 @@ MAP_OFFSET_X = 35 MAP_OFFSET_Y = 100 #MENUBAR +CHOOSEBAR_TYPE = 'choosebar_type' +CHOOSEBAR_STATIC = 0 +CHOOSEBAR_MOVE = 1 MENUBAR_BACKGROUND = 'ChooserBackground' +MOVEBAR_BACKGROUND = 'MoveBackground' PANEL_BACKGROUND = 'PanelBackground' START_BUTTON = 'StartButton' +CARD_POOL = 'card_pool' + +MOVEBAR_CARD_FRESH_TIME = 6000 +CARD_MOVE_TIME = 60 #PLANT INFO PLANT_IMAGE_RECT = 'plant_image_rect' diff --git a/source/data/map/level_4.json b/source/data/map/level_4.json new file mode 100644 index 0000000..aaa525b --- /dev/null +++ b/source/data/map/level_4.json @@ -0,0 +1,41 @@ +{ + "background_type":0, + "choosebar_type":1, + "card_pool":[ + {"name":"Peashooter"}, + {"name":"SnowPea"}, + {"name":"WallNut"}, + {"name":"CherryBomb"}, + {"name":"RepeaterPea"}, + {"name":"Chomper"}, + {"name":"PotatoMine"} + ], + "zombie_list":[ + {"time": 1000, "map_y":1, "name":"Zombie"}, + {"time": 6000, "map_y":3, "name":"FlagZombie"}, + {"time":10000, "map_y":0, "name":"ConeheadZombie"}, + {"time":14000, "map_y":2, "name":"NewspaperZombie"}, + {"time":18000, "map_y":4, "name":"BucketheadZombie"}, + {"time":22000, "map_y":0, "name":"Zombie"}, + {"time":26000, "map_y":3, "name":"BucketheadZombie"}, + {"time":30000, "map_y":4, "name":"Zombie"}, + {"time":32000, "map_y":3, "name":"NewspaperZombie"}, + {"time":34000, "map_y":1, "name":"FlagZombie"}, + {"time":36000, "map_y":2, "name":"ConeheadZombie"}, + {"time":38000, "map_y":0, "name":"BucketheadZombie"}, + {"time":40000, "map_y":1, "name":"ConeheadZombie"}, + {"time":42000, "map_y":1, "name":"NewspaperZombie"}, + {"time":60000, "map_y":4, "name":"Zombie"}, + {"time":61000, "map_y":3, "name":"NewspaperZombie"}, + {"time":62000, "map_y":1, "name":"FlagZombie"}, + {"time":63000, "map_y":2, "name":"Zombie"}, + {"time":64000, "map_y":0, "name":"BucketheadZombie"}, + {"time":65000, "map_y":1, "name":"ConeheadZombie"}, + {"time":66000, "map_y":2, "name":"Zombie"}, + {"time":67000, "map_y":4, "name":"BucketheadZombie"}, + {"time":68000, "map_y":3, "name":"NewspaperZombie"}, + {"time":69000, "map_y":1, "name":"FlagZombie"}, + {"time":70000, "map_y":4, "name":"BucketheadZombie"}, + {"time":71000, "map_y":0, "name":"FlagZombie"} + ] +} \ No newline at end of file diff --git a/source/state/level.py b/source/state/level.py index c30e9de..25ba8db 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -19,9 +19,8 @@ class Level(tool.State): self.map = map.Map(c.GRID_X_LEN, self.map_y_len) self.loadMap() - self.state = c.CHOOSE - self.initChoose() self.setupBackground() + self.initState() def loadMap(self): map_file = 'level_' + str(self.game_info[c.LEVEL_NUM]) + '.json' @@ -79,22 +78,38 @@ class Level(tool.State): self.draw(surface) + def initState(self): + if c.CHOOSEBAR_TYPE in self.map_data: + self.bar_type = self.map_data[c.CHOOSEBAR_TYPE] + else: + self.bar_type = c.CHOOSEBAR_STATIC + + if self.bar_type == c.CHOOSEBAR_STATIC: + self.initChoose() + else: + card_pool = menubar.getCardPool(self.map_data[c.CARD_POOL]) + self.initPlay(card_pool) + def initChoose(self): + self.state = c.CHOOSE self.panel = menubar.Panel(menubar.all_card_list, self.map_data[c.INIT_SUN_NAME]) def choose(self, mouse_pos, mouse_click): if mouse_pos and mouse_click[0]: self.panel.checkCardClick(mouse_pos) if self.panel.checkStartButtonClick(mouse_pos): - self.state = c.PLAY self.initPlay(self.panel.getSelectedCards()) def initPlay(self, card_list): - self.menubar = menubar.MenuBar(card_list, self.map_data[c.INIT_SUN_NAME]) + self.state = c.PLAY + if self.bar_type == c.CHOOSEBAR_STATIC: + self.menubar = menubar.MenuBar(card_list, self.map_data[c.INIT_SUN_NAME]) + else: + self.menubar = menubar.MoveBar(card_list) self.drag_plant = False self.hint_image = None self.hint_plant = False - if self.background_type == c.BACKGROUND_DAY: + if self.background_type == c.BACKGROUND_DAY and self.bar_type == c.CHOOSEBAR_STATIC: self.produce_sun = True else: self.produce_sun = False @@ -227,8 +242,12 @@ class Level(tool.State): if new_plant.can_sleep and self.background_type == c.BACKGROUND_DAY: new_plant.setSleep() self.plant_groups[map_y].add(new_plant) - self.menubar.decreaseSunValue(self.plant_cost) - self.menubar.setCardFrozenTime(self.plant_name) + if self.bar_type == c.CHOOSEBAR_STATIC: + self.menubar.decreaseSunValue(self.select_plant.sun_cost) + self.menubar.setCardFrozenTime(self.plant_name) + else: + self.menubar.deleateCard(self.select_plant) + self.map.setMapGridType(map_x, map_y, c.MAP_EXIST) self.removeMouseImage() #print('addPlant map[%d,%d], grid pos[%d, %d] pos[%d, %d]' % (map_x, map_y, x, y, pos[0], pos[1])) @@ -252,7 +271,7 @@ class Level(tool.State): else: self.hint_plant = False - def setupMouseImage(self, plant_name, plant_cost): + def setupMouseImage(self, plant_name, select_plant): frame_list = tool.GFX[plant_name] if plant_name in tool.PLANT_RECT: data = tool.PLANT_RECT[plant_name] @@ -274,7 +293,7 @@ class Level(tool.State): pg.mouse.set_visible(False) self.drag_plant = True self.plant_name = plant_name - self.plant_cost = plant_cost + self.select_plant = select_plant def removeMouseImage(self): pg.mouse.set_visible(True)