AI科技大本营|玩出花儿了!一文教你用 Python 制作吃豆人游戏! | 附代码,Python( 三 )


[-0.5,0,3],[0,-0.5,7],[-0.5,0,3],[0,0.5,7],[-0.5,0,11],[0,-0.5,7],[0.5,0,5]]
self.ghost_sprites.add(player)
elifrole_name=='Clyde':
player=Player(319,259,each)
player.is_move=True
player.tracks=[[-1,0,2],[0,-0.5,4],[0.5,0,5],[0,0.5,7],[-0.5,0,11],[0,-0.5,7],
[-0.5,0,3],[0,0.5,7],[-0.5,0,7],[0,0.5,15],[0.5,0,15],[0,-0.5,3],
[-0.5,0,11],[0,-0.5,7],[0.5,0,3],[0,-0.5,11],[0.5,0,9]]
self.ghost_sprites.add(player)
elifrole_name=='Inky':
player=Player(255,259,each)
player.is_move=True
player.tracks=[[1,0,2],[0,-0.5,4],[0.5,0,10],[0,0.5,7],[0.5,0,3],[0,-0.5,3],
[0.5,0,3],[0,-0.5,15],[-0.5,0,15],[0,0.5,3],[0.5,0,15],[0,0.5,11],
[-0.5,0,3],[0,-0.5,7],[-0.5,0,11],[0,0.5,3],[-0.5,0,11],[0,0.5,7],
[-0.5,0,3],[0,-0.5,3],[-0.5,0,3],[0,-0.5,15],[0.5,0,15],[0,0.5,3],
[-0.5,0,15],[0,0.5,11],[0.5,0,3],[0,-0.5,11],[0.5,0,11],[0,0.5,3],[0.5,0,1]]
self.ghost_sprites.add(player)
elifrole_name=='Pinky':
player=Player(287,259,each)
player.is_move=True
player.tracks=[[0,-1,4],[0.5,0,9],[0,0.5,11],[-0.5,0,23],[0,0.5,7],[0.5,0,3],
[0,-0.5,3],[0.5,0,19],[0,0.5,3],[0.5,0,3],[0,0.5,3],[0.5,0,3],
[0,-0.5,15],[-0.5,0,7],[0,0.5,3],[-0.5,0,19],[0,-0.5,11],[0.5,0,9]]
self.ghost_sprites.add(player)
returnself.hero_sprites,self.ghost_sprites
'''创建食物'''
defsetupFood(self,food_color,bg_color):
self.food_sprites=pygame.sprite.Group
forrowinrange(19):
forcolinrange(19):
if(row==7orrow==8)and(col==8orcol==9orcol==10):
continue
else:
food=Food(30*col+32,30*row+32,4,4,food_color,bg_color)
is_collide=pygame.sprite.spritecollide(food,self.wall_sprites,False)
ifis_collide:
continue
is_collide=pygame.sprite.spritecollide(food,self.hero_sprites,False)
ifis_collide:
continue
self.food_sprites.add(food)
returnself.food_sprites
3、游戏创建:
在通过关卡定义墙等位置后以及精灵自身属性怪物运动、食物定义等后 , 通过调用已经创建好的类达到搭建游戏的目的 。
具体如下可见:
importos
importsys
importpygame
importLevels
'''定义一些必要的参数'''
BLACK=(0,0,0)
WHITE=(255,255,255)
BLUE=(0,0,255)
GREEN=(0,255,0)
RED=(255,0,0)
YELLOW=(255,255,0)
PURPLE=(255,0,255)
SKYBLUE=(0,191,255)
BGMPATH=os.path.join(os.getcwd,'resources/sounds/bg.mp3')
ICONPATH=os.path.join(os.getcwd,'resources/images/icon.png')
FONTPATH=os.path.join(os.getcwd,'resources/font/ALGER.TTF')
HEROPATH=os.path.join(os.getcwd,'resources/images/pacman.png')
BlinkyPATH=os.path.join(os.getcwd,'resources/images/Blinky.png')
ClydePATH=os.path.join(os.getcwd,'resources/images/Clyde.png')
InkyPATH=os.path.join(os.getcwd,'resources/images/Inky.png')
PinkyPATH=os.path.join(os.getcwd,'resources/images/Pinky.png')
'''开始某一关游戏'''
defstartLevelGame(level,screen,font):
clock=pygame.time.Clock
SCORE=0
wall_sprites=level.setupWalls(SKYBLUE)
gate_sprites=level.setupGate(WHITE)


推荐阅读