AI科技大本营|玩出花儿了!一文教你用 Python 制作吃豆人游戏! | 附代码,Python( 四 )
hero_sprites,ghost_sprites=level.setupPlayers(HEROPATH,[BlinkyPATH,ClydePATH,InkyPATH,PinkyPATH])
food_sprites=level.setupFood(YELLOW,WHITE)
is_clearance=False
whileTrue:
foreventinpygame.event.get:
ifevent.type==pygame.QUIT:
sys.exit(-1)
pygame.quit
ifevent.type==pygame.KEYDOWN:
ifevent.key==pygame.K_LEFT:
forheroinhero_sprites:
hero.changeSpeed([-1,0])
hero.is_move=True
elifevent.key==pygame.K_RIGHT:
forheroinhero_sprites:
hero.changeSpeed([1,0])
hero.is_move=True
elifevent.key==pygame.K_UP:
forheroinhero_sprites:
hero.changeSpeed([0,-1])
hero.is_move=True
elifevent.key==pygame.K_DOWN:
forheroinhero_sprites:
hero.changeSpeed([0,1])
hero.is_move=True
ifevent.type==pygame.KEYUP:
if(event.key==pygame.K_LEFT)or(event.key==pygame.K_RIGHT)or(event.key==pygame.K_UP)or(event.key==pygame.K_DOWN):
hero.is_move=False
screen.fill(BLACK)
forheroinhero_sprites:
hero.update(wall_sprites,gate_sprites)
hero_sprites.draw(screen)
forheroinhero_sprites:
food_eaten=pygame.sprite.spritecollide(hero,food_sprites,True)
SCORE+=len(food_eaten)
wall_sprites.draw(screen)
gate_sprites.draw(screen)
food_sprites.draw(screen)
forghostinghost_sprites:
#幽灵随机运动(效果不好且有BUG)
'''
res=ghost.update(wall_sprites,None)
whilenotres:
ghost.changeSpeed(ghost.randomDirection)
res=ghost.update(wall_sprites,None)
'''
#指定幽灵运动路径
ifghost.tracks_loc[1]<ghost.tracks[ghost.tracks_loc[0]][2]:
ghost.changeSpeed(ghost.tracks[ghost.tracks_loc[0]][0:2])
ghost.tracks_loc[1]+=1
else:
ifghost.tracks_loc[0]<len(ghost.tracks)-1:
ghost.tracks_loc[0]+=1
elifghost.role_name=='Clyde':
ghost.tracks_loc[0]=2
else:
ghost.tracks_loc[0]=0
ghost.changeSpeed(ghost.tracks[ghost.tracks_loc[0]][0:2])
ghost.tracks_loc[1]=0
ifghost.tracks_loc[1]<ghost.tracks[ghost.tracks_loc[0]][2]:
ghost.changeSpeed(ghost.tracks[ghost.tracks_loc[0]][0:2])
else:
ifghost.tracks_loc[0]<len(ghost.tracks)-1:
loc0=ghost.tracks_loc[0]+1
elifghost.role_name=='Clyde':
loc0=2
else:
loc0=0
ghost.changeSpeed(ghost.tracks[loc0][0:2])
ghost.update(wall_sprites,None)
ghost_sprites.draw(screen)
score_text=font.render("Score:%s"%SCORE,True,RED)
screen.blit(score_text,[10,10])
iflen(food_sprites)==0:
is_clearance=True
break
ifpygame.sprite.groupcollide(hero_sprites,ghost_sprites,False,False):
is_clearance=False
break
pygame.display.flip
clock.tick(10)
returnis_clearance
'''显示文字'''
defshowText(screen,font,is_clearance,flag=False):
clock=pygame.time.Clock
msg='GameOver!'ifnotis_clearanceelse'Congratulations,youwon!'
positions=[[235,233],[65,303],[170,333]]ifnotis_clearanceelse[[145,233],[65,303],[170,333]]
surface=pygame.Surface((400,200))
surface.set_alpha(10)
推荐阅读
- 科技观察站|被错付了,腾讯与老干妈堪比喜剧电影的“商战”情节:风味豆豉鹅
- 暖日欣欣科技立伟|随便哪一件都值得我们去怀念,热血传奇:盘点盛大经典的装备系列
- 爱辉科技|荣耀赵明点赞,网友:赵总也想去小米?,雷军官宣杨柘加盟小米
- 高科技元素|广东欢太科技有限公司这样建议,小孩沉迷游戏造成误充值
- 心急科技|压力有点大啊,明日之后玩家首次当策划多结局新玩法
- 无边科技|710点券留给钟馗,杨戬改动方案公布司马懿新皮肤被爆料
- 风口科技|中路抢血包,打野我想静静,最怕打野做什么边路抢河蟹
- 云顶之弈▲云顶之弈上分黑科技,只用这一套阵容,轻松上钻2!简直不要太强!
- 电科技|以投屏为主打卖点,索尼这款电视收获数万人好评,携手乐播
- ITheat热点科技|FE12-24mm f2.8已在路上,索尼A7S3全幅微单EVF规格曝光