網(wǎng)上有很多關(guān)于工商pos機代碼,用 Python 實現(xiàn)植物大戰(zhàn)僵尸代碼的知識,也有很多人為大家解答關(guān)于工商pos機代碼的問題,今天pos機之家(www.rcqwhg.com)為大家整理了關(guān)于這方面的知識,讓我們一起來看下吧!
本文目錄一覽:
1、工商pos機代碼
工商pos機代碼
前言
本文的文字及圖片來源于網(wǎng)絡(luò),僅供學(xué)習(xí)、交流使用,不具有任何商業(yè)用途,版權(quán)歸原作者所有,如有問題請及時聯(lián)系我們以作處理。
作者: marble_xu
PS:如有需要Python學(xué)習(xí)資料的小伙伴可以加點擊下方鏈接自行獲取
http://note.youdao.com/noteshare?id=3054cce4add8a909e784ad934f956cef
功能介紹
最近一直在給這個植物大戰(zhàn)僵尸游戲添加新的植物和僵尸, 因為網(wǎng)上的圖片資源有限,能加的植物和僵尸比較少, 目前進展如下。
功能實現(xiàn)如下:
支持的植物類型:太陽花,豌豆射手,寒冰射手,堅果,櫻桃炸彈。新增加植物:雙重豌豆射手,三重豌豆射手,食人花 ,小噴菇,土豆地雷,倭瓜。支持的僵尸類型:普通僵尸,棋子僵尸,路障僵尸,鐵桶僵尸。新增加讀報僵尸。使用json文件保存關(guān)卡信息,設(shè)置僵尸出現(xiàn)的時間和位置。增加每關(guān)開始時選擇上場植物。增加除草機。下面是游戲的截圖:
植物卡片選擇和種植
如圖所示,游戲中可以種植物的方格一共有45個(有5行,每行9列)。
這篇文章要介紹的是:
上方植物卡片欄的實現(xiàn)。點擊植物卡片,鼠標切換為植物圖片。鼠標移動時,判斷當前在哪個方格中,并顯示半透明的植物作為提示。代碼實現(xiàn)
所有的植物卡片的名稱和屬性都保存在單獨的list中,每個list index都對應(yīng)一種植物。
比如list index 0 就是太陽花:
Card_name_list[0] 是太陽花卡片的名字,用來獲取太陽花卡片的圖片。plant_name_list[0] 是太陽花的名字,用來獲取太陽花卡片的圖片。plant_sun_list[0] 是種植太陽花需要花費的太陽點數(shù)。plant_frozen_time_list[0] 是太陽花的冷卻時間。植物卡片類
每個植物卡片是一個單獨的Card類,用來顯示這個植物。
checkMouseClick函數(shù):判斷鼠標是否點擊到這個卡片;canClick:判斷這個卡片是否能種植(有沒有足夠的點數(shù),是否還在冷卻時間內(nèi));update 函數(shù):通過設(shè)置圖片的透明度來表示這個卡片是否能選擇。卡片欄類
MenuBar類顯示圖3中的植物卡片欄:
self.sun_value:當前采集的太陽點數(shù);self.card_list: 植物卡片的list;setupCards函數(shù):遍歷初始化init函數(shù)中傳入這個關(guān)卡選好的植物卡片list,依次創(chuàng)建Card類,設(shè)置每個卡片的顯示位置;checkCardClick函數(shù):檢查鼠標是否點擊了卡片欄上的某個植物卡片,如果選擇了一個可種植的卡片,返回結(jié)果。代碼:
import pygame as pgfrom .. import toolfrom .. import constants as cPANEL_Y_START = 87PANEL_X_START = 22PANEL_Y_INTERNAL = 74PANEL_X_INTERNAL = 53CARD_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_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="360px",height="auto" />
rect = msg_image.get_rect() msg_w = msg_rect.width="360px",height="auto" />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.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="360px",height="auto" />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_STAR 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 for i, index in enumerate(card_list): if i % 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 = None for card in self.selected_cards: if delete_card: # when delete a card, move right cards to left card.rect.x -= 55 elif card.checkMouseClick(mouse_pos): self.deleteCard(card.name_index) delete_card = card if delete_card: self.selected_cards.remove(delete_card) self.selected_num -= 1 if self.selected_num == CARD_LIST_NUM: return for card in self.card_list: if card.checkMouseClick(mouse_pos): if card.canSelect(): self.addCard(card) break def addCard(self, card): card.setSelect(False) y = 8 x = 78 + self.selected_num * 55 self.selected_cards.append(Card(x, y, card.name_index)) self.selected_num += 1 def deleteCard(self, index): self.card_list[index].setSelect(True) def checkStartButtonClick(self, mouse_pos): if self.selected_num < CARD_LIST_NUM: return False x, y = mouse_pos if (x >= self.button_rect.x and x <= self.button_rect.right and y >= self.button_rect.y and y <= self.button_rect.bottom): return True return False def getSelectedCards(self): card_index_list = [] for card in self.selected_cards: card_index_list.append(card.name_index) return card_index_list def 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 函數(shù)實現(xiàn)鼠標圖片切換為選中的植物:
self.mouse_image :根據(jù) plant_name 獲取選中的植物圖片;self.mouse_rect:選中植物圖片的位置,在drawMouseShow函數(shù)中,需要將植物圖片的位置設(shè)置成當前鼠標的位置;pg.mouse.set_visible(False):隱藏默認的鼠標顯示,這樣效果就是鼠標圖片切換為選中的植物了。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="360px",height="auto" />
提示種在哪個方格中
先看下map類,代碼在source\\component\\map.py 中:
self.map:二維list,用來保存每個方格的狀態(tài)。每個entry初始化為 0, 表示可以種植物,值為1時表示這個方格已經(jīng)種了植物。getMapIndex 函數(shù):傳入?yún)?shù)是游戲中的坐標位置(比如當前鼠標的位置),返回該位置在地圖的哪個方格中。getMapGridPos 函數(shù):傳入一個方格的index,返回在該方格中種植物的坐標位置。showPlant 函數(shù):根據(jù)傳入的坐標位置,判斷該位置所在的方格是否能種植物,如果能種,就返回返回在該方格中種植物的坐標位置。MAP_EMPTY = 0MAP_EXIST = 1class Map(): def __init__(self, width="360px",height="auto" />
代碼在source\\state\\level.py中:
canSeedPlant 函數(shù):判斷當前鼠標位置能否種植物;setupHintImage 函數(shù):如果當前鼠標位置能種植物,且有選擇了一個植物卡片,則設(shè)置self.hint_image 顯示當前會在哪一個方格中種植物,self.hint_rect 是植物種的坐標位置。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="360px",height="auto" />
以上就是關(guān)于工商pos機代碼,用 Python 實現(xiàn)植物大戰(zhàn)僵尸代碼的知識,后面我們會繼續(xù)為大家整理關(guān)于工商pos機代碼的知識,希望能夠幫助到大家!