#! bpyimportbgeimporttimeitimportaudimportosdefmain():"""Contains all changes"""# get scenescene=bge.logic.getCurrentScene()# get contollercon=bge.logic.getCurrentController()# get playerplayer=con.owner# get keyboardkeyboard=bge.logic.keyboard# get the text objecttext=scene.objects["Text"]# check if start has already been runif"lives"notinplayer:start(player)# check for collisionsforobjectinscene.objects:# does object collide# the distance is from origin to origin, so we subtract a bitifplayer.getDistanceTo(object)-2<=0:# Is object an enemy ...ifobject.name.startswith("Enemy"):ifplayer["can_collide"]:player["lives"]-=1# play enemy hit soundaud.device().play(player["enemy_sound"])# Set that player can't collideplayer["can_collide"]=False# register time of collisionplayer["last_col"]=timeit.default_timer()# ... or is object a coin?elifobject.name.startswith("Coin"):player["coins"]+=1# play collecting sound with the aud moduleaud.device().play(player["coin_sound"])# remove the coin from the sceneobject.endObject()# player hasn't got lives?ifplayer["lives"]<=0:print("lost")# remove player from sceneplayer.endObject()# stop gamebge.logic.endGame()# player got all coins?ifplayer["coins"]>=10:print("Win")bge.logic.endGame()# player can collide again?ifnotplayer["can_collide"]:if(timeit.default_timer()-player["last_col"])>=1:player["can_collide"]=True# move player# keyboard eventsk_events=bge.logic.KX_INPUT_ACTIVE# up arrow pressed?ifkeyboard.events[bge.events.UPARROWKEY]==k_events:# move player forwards on its local y axisplayer.applyMovement((0,0,.2),True)# down arrow pressedifkeyboard.events[bge.events.DOWNARROWKEY]==k_events:# move player backwards on its local y axisplayer.applyMovement((0,0,-.2),True)# left arrow pressedifkeyboard.events[bge.events.LEFTARROWKEY]==k_events:# rotate to the leftplayer.applyRotation((0,0,.05))# right arrow pressedifkeyboard.events[bge.events.RIGHTARROWKEY]==k_events:# rotate to the rightplayer.applyRotation((0,0,-.05))# Set the texttext.text="Lives: {}; Coins: {}".format(player["lives"],player["coins"])defstart(player):"""Setup the player"""player["lives"]=3player["coins"]=0player["can_collide"]=True# sound with the aud module# coin collect soundpath=bge.logic.expandPath("//snd/coin.wav")player["coin_sound"]=aud.Factory.file(path)# enemy hit soundpath=bge.logic.expandPath("//snd/enemy.wav")player["enemy_sound"]=aud.Factory.file(path)main()

PyMove3D credits

PyMove3D is started and supported by the German Python Software Verband.
It delivers course material for the programming contest and for people
interested in using Python and Blender.