基于python的植物大战僵尸游戏的论文,python植物大战僵尸代码示例

  基于python的植物大战僵尸游戏的论文,python植物大战僵尸代码示例

  这篇文章主要介绍了如何利用大蟒实现植物大战僵尸游戏,文中有非常详细的代码示例,对正在学习大蟒的小伙伴们有非常好的帮助,需要的朋友可以参考下

  

目录
游戏截图动态演示源码分享状态/工具。py状态/常数py状态/主。巴拉圭主执行文件main.py

  

游戏截图

  

动态演示

  

源码分享

  

state/tool.py

  导入操作系统

  导入数据

  从字母表导入抽象方法

  将简介及安装导入为程序制导

  从。将常量作为c导入

  类别状态():

  def __init__(self):

  self.start_time=0.0

  自电流时间=0.0

  self.done=False

  self.next=无

  self.persist={}

  @抽象方法

  定义启动(自身、当前时间、持续):

  抽象方法

  极好的清理(自我):

  self.done=False

  回归自我,坚持

  @抽象方法

  定义更新(自身,表面,关键点,当前时间):

  抽象方法

  类别控制():

  def __init__(self):

  自我。屏幕=pg。展示。get _ surface()

  self.done=False

  self.clock=pg.time.Clock()

  self.fps=60

  self.keys=pg.key.get_pressed()

  self.mouse_pos=None

  self.mouse_click=[False,False]# value :[鼠标左键单击,鼠标右键单击]

  自电流时间=0.0

  self.state_dict={}

  self.state_name=无

  自我状态=无

  自我。游戏_信息={ c .当前_时间:0.0,

  碳(碳的缩写)LEVEL_NUM:c .START_LEVEL_NUM}

  def setup_states(self,state_dict,start_state):

  自我状态_字典=状态_字典

  self.state_name=起始状态

  自我。状态=自我。state _ dict[自我。状态_名称]

  自我。状态。启动(自我。当前时间,自我.游戏信息)

  定义更新(自身):

  自我。当前时间=pg。时间。get _ ticks()

  if self.state.done:

  self.flip_state()

  self.state.update(self.screen,self.current_time,self.m

  ouse_pos, self.mouse_click)

   self.mouse_pos = None

   self.mouse_click[0] = False

   self.mouse_click[1] = False

   def flip_state(self):

   previous, self.state_name = self.state_name, self.state.next

   persist = self.state.cleanup()

   self.state = self.state_dict[self.state_name]

   self.state.startup(self.current_time, persist)

   def event_loop(self):

   for event in pg.event.get():

   if event.type == pg.QUIT:

   self.done = True

   elif event.type == pg.KEYDOWN:

   self.keys = pg.key.get_pressed()

   elif event.type == pg.KEYUP:

   self.keys = pg.key.get_pressed()

   elif event.type == pg.MOUSEBUTTONDOWN:

   self.mouse_pos = pg.mouse.get_pos()

   self.mouse_click[0], _, self.mouse_click[1] = pg.mouse.get_pressed()

   print(pos:, self.mouse_pos, mouse:, self.mouse_click)

   def main(self):

   while not self.done:

   self.event_loop()

   self.update()

   pg.display.update()

   self.clock.tick(self.fps)

   print(game over)

  def get_image(sheet, x, y, width, height, colorkey=c.BLACK, scale=1):

   image = pg.Surface([width, height])

   rect = image.get_rect()

   image.blit(sheet, (0, 0), (x, y, width, height))

   image.set_colorkey(colorkey)

   image = pg.transform.scale(image,

   (int(rect.width*scale),

   int(rect.height*scale)))

   return image

  def load_image_frames(directory, image_name, colorkey, accept):

   frame_list = []

   tmp = {}

   # image_name is "Peashooter", pic name is Peashooter_1, get the index 1

   index_start = len(image_name) + 1

   frame_num = 0;

   for pic in os.listdir(directory):

   name, ext = os.path.splitext(pic)

   if ext.lower() in accept:

   index = int(name[index_start:])

   img = pg.image.load(os.path.join(directory, pic))

   if img.get_alpha():

   img = img.convert_alpha()

   else:

   img = img.convert()

   img.set_colorkey(colorkey)

   tmp[index]= img

   frame_num += 1

   for i in range(frame_num):

   frame_list.append(tmp[i])

   return frame_list

  def load_all_gfx(directory, colorkey=c.WHITE, accept=(.png, .jpg, .bmp, .gif)):

   graphics = {}

   for name1 in os.listdir(directory):

   # subfolders under the folder resources\graphics

   dir1 = os.path.join(directory, name1)

   if os.path.isdir(dir1):

   for name2 in os.listdir(dir1):

   dir2 = os.path.join(dir1, name2)

   if os.path.isdir(dir2):

   # e.g. subfolders under the folder resources\graphics\Zombies

   for name3 in os.listdir(dir2):

   dir3 = os.path.join(dir2, name3)

   # e.g. subfolders or pics under the folder resources\graphics\Zombies\ConeheadZombie

   if os.path.isdir(dir3):

   # e.g. its the folder resources\graphics\Zombies\ConeheadZombie\ConeheadZombieAttack

   image_name, _ = os.path.splitext(name3)

   graphics[image_name] = load_image_frames(dir3, image_name, colorkey, accept)

   else:

   # e.g. pics under the folder resources\graphics\Plants\Peashooter

   image_name, _ = os.path.splitext(name2)

   graphics[image_name] = load_image_frames(dir2, image_name, colorkey, accept)

   break

   else:

   # e.g. pics under the folder resources\graphics\Screen

   name, ext = os.path.splitext(name2)

   if ext.lower() in accept:

   img = pg.image.load(dir2)

   if img.get_alpha():

   img = img.convert_alpha()

   else:

   img = img.convert()

   img.set_colorkey(colorkey)

   graphics[name] = img

   return graphics

  def loadZombieImageRect():

   file_path = os.path.join(source, data, entity, zombie.json)

   f = open(file_path)

   data = json.load(f)

   f.close()

   return data[c.ZOMBIE_IMAGE_RECT]

  def loadPlantImageRect():

   file_path = os.path.join(source, data, entity, plant.json)

   f = open(file_path)

   data = json.load(f)

   f.close()

   return data[c.PLANT_IMAGE_RECT]

  pg.init()

  pg.display.set_caption(c.ORIGINAL_CAPTION)

  SCREEN = pg.display.set_mode(c.SCREEN_SIZE)

  GFX = load_all_gfx(os.path.join("resources","graphics"))

  ZOMBIE_RECT = loadZombieImageRect()

  PLANT_RECT = loadPlantImageRect()

  

  

  

state/constants.py

  

  START_LEVEL_NUM = 1

  ORIGINAL_CAPTION = Plant VS Zombies Game

  SCREEN_WIDTH = 800

  SCREEN_HEIGHT = 600

  SCREEN_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT)

  GRID_X_LEN = 9

  GRID_Y_LEN = 5

  GRID_X_SIZE = 80

  GRID_Y_SIZE = 100

  WHITE = (255, 255, 255)

  NAVYBLUE = ( 60, 60, 100)

  SKY_BLUE = ( 39, 145, 251)

  BLACK = ( 0, 0, 0)

  LIGHTYELLOW = (234, 233, 171)

  RED = (255, 0, 0)

  PURPLE = (255, 0, 255)

  GOLD = (255, 215, 0)

  GREEN = ( 0, 255, 0)

  SIZE_MULTIPLIER = 1.3

  #GAME INFO DICTIONARY KEYS

  CURRENT_TIME = current time

  LEVEL_NUM = level num

  #STATES FOR ENTIRE GAME

  MAIN_MENU = main menu

  LOAD_SCREEN = load screen

  GAME_LOSE = game los

  GAME_VICTORY = game victory

  LEVEL = level

  MAIN_MENU_IMAGE = MainMenu

  OPTION_ADVENTURE = Adventure

  GAME_LOOSE_IMAGE = GameLoose

  GAME_VICTORY_IMAGE = GameVictory

  #MAP COMPONENTS

  BACKGROUND_NAME = Background

  BACKGROUND_TYPE = background_type

  INIT_SUN_NAME = init_sun_value

  ZOMBIE_LIST = zombie_list

  MAP_EMPTY = 0

  MAP_EXIST = 1

  BACKGROUND_OFFSET_X = 220

  MAP_OFFSET_X = 35

  MAP_OFFSET_Y = 100

  #MENUBAR

  CHOOSEBAR_TYPE = choosebar_type

  CHOOSEBAR_STATIC = 0

  CHOOSEBAR_MOVE = 1

  CHOSSEBAR_BOWLING = 2

  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

  CAR = car

  SUN = Sun

  SUNFLOWER = SunFlower

  PEASHOOTER = Peashooter

  SNOWPEASHOOTER = SnowPea

  WALLNUT = WallNut

  CHERRYBOMB = CherryBomb

  THREEPEASHOOTER = Threepeater

  REPEATERPEA = RepeaterPea

  CHOMPER = Chomper

  CHERRY_BOOM_IMAGE = Boom

  PUFFSHROOM = PuffShroom

  POTATOMINE = PotatoMine

  SQUASH = Squash

  SPIKEWEED = Spikeweed

  JALAPENO = Jalapeno

  SCAREDYSHROOM = ScaredyShroom

  SUNSHROOM = SunShroom

  ICESHROOM = IceShroom

  HYPNOSHROOM = HypnoShroom

  WALLNUTBOWLING = WallNutBowling

  REDWALLNUTBOWLING = RedWallNutBowling

  PLANT_HEALTH = 5

  WALLNUT_HEALTH = 30

  WALLNUT_CRACKED1_HEALTH = 20

  WALLNUT_CRACKED2_HEALTH = 10

  WALLNUT_BOWLING_DAMAGE = 10

  PRODUCE_SUN_INTERVAL = 7000

  FLOWER_SUN_INTERVAL = 22000

  SUN_LIVE_TIME = 7000

  SUN_VALUE = 25

  ICE_SLOW_TIME = 2000

  FREEZE_TIME = 7500

  ICETRAP = IceTrap

  #PLANT CARD INFO

  CARD_SUNFLOWER = card_sunflower

  CARD_PEASHOOTER = card_peashooter

  CARD_SNOWPEASHOOTER = card_snowpea

  CARD_WALLNUT = card_wallnut

  CARD_CHERRYBOMB = card_cherrybomb

  CARD_THREEPEASHOOTER = card_threepeashooter

  CARD_REPEATERPEA = card_repeaterpea

  CARD_CHOMPER = card_chomper

  CARD_PUFFSHROOM = card_puffshroom

  CARD_POTATOMINE = card_potatomine

  CARD_SQUASH = card_squash

  CARD_SPIKEWEED = card_spikeweed

  CARD_JALAPENO = card_jalapeno

  CARD_SCAREDYSHROOM = card_scaredyshroom

  CARD_SUNSHROOM = card_sunshroom

  CARD_ICESHROOM = card_iceshroom

  CARD_HYPNOSHROOM = card_hypnoshroom

  CARD_REDWALLNUT = card_redwallnut

  #BULLET INFO

  BULLET_PEA = PeaNormal

  BULLET_PEA_ICE = PeaIce

  BULLET_MUSHROOM = BulletMushRoom

  BULLET_DAMAGE_NORMAL = 1

  #ZOMBIE INFO

  ZOMBIE_IMAGE_RECT = zombie_image_rect

  ZOMBIE_HEAD = ZombieHead

  NORMAL_ZOMBIE = Zombie

  CONEHEAD_ZOMBIE = ConeheadZombie

  BUCKETHEAD_ZOMBIE = BucketheadZombie

  FLAG_ZOMBIE = FlagZombie

  NEWSPAPER_ZOMBIE = NewspaperZombie

  BOOMDIE = BoomDie

  LOSTHEAD_HEALTH = 5

  NORMAL_HEALTH = 10

  FLAG_HEALTH = 15

  CONEHEAD_HEALTH = 20

  BUCKETHEAD_HEALTH = 30

  NEWSPAPER_HEALTH = 15

  ATTACK_INTERVAL = 1000

  ZOMBIE_WALK_INTERVAL = 70

  ZOMBIE_START_X = SCREEN_WIDTH + 50

  #STATE

  IDLE = idle

  FLY = fly

  EXPLODE = explode

  ATTACK = attack

  ATTACKED = attacked

  DIGEST = digest

  WALK = walk

  DIE = die

  CRY = cry

  FREEZE = freeze

  SLEEP = sleep

  #LEVEL STATE

  CHOOSE = choose

  PLAY = play

  #BACKGROUND

  BACKGROUND_DAY = 0

  BACKGROUND_NIGHT = 1

  

  

  

state/main.py

  

from . import tool

  from . import constants as c

  from .state import mainmenu, screen, level

  def main():

   game = tool.Control()

   state_dict = {c.MAIN_MENU: mainmenu.Menu(),

   c.GAME_VICTORY: screen.GameVictoryScreen(),

   c.GAME_LOSE: screen.GameLoseScreen(),

   c.LEVEL: level.Level()}

   game.setup_states(state_dict, c.MAIN_MENU)

   game.main()

  

  

  

主执行文件main.py

  

import pygame as pg

  from source.main import main

  if __name__==__main__:

   main()

   pg.quit()

  到此这篇关于基于Python实现经典植物大战僵尸游戏的文章就介绍到这了,更多相关Python植物大战僵尸内容请搜索盛行IT软件开发工作室以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT软件开发工作室!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: