-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalk
More file actions
53 lines (42 loc) · 1.88 KB
/
walk
File metadata and controls
53 lines (42 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from ggame import App
from ggame import ImageAsset, Sprite, Frame
import time
### create sprite from spritesheet
#
#VIEW IMAGE - https://github.com/tiffinschool/pythonChallenges/blob/master/characterSheet96x96.png
#REFERENCE URL - https://ggame.readthedocs.io/en/latest/ggameapi.html#sprite
#USAGE - ImageAsset(url, frame=None, qty=1, direction='horizontal', margin=0)
manFrame = Frame(0,0,96,96)
manImage = ImageAsset("characterSheet96x96.png",manFrame,16,'horizontal',0)
manSprite = Sprite(manImage,(100,100))
runningRightIndexes = [2,3]
lastTimeMovedRight = 0
### bind event listeners
#
#REFERENCE URL - https://ggame.readthedocs.io/en/latest/ggameapi.html#ggame.asset.Frame
#USAGE - listenKeyEvent(eventtype, key, callback)
#moves man right including animation
def moveManRight(e):
global manSprite, lastTimeMovedRight
manSprite.x += 2
if (time.process_time() > lastTimeMovedRight + 0.1): #only update the appearance every 0.1 seconds
lastTimeMovedRight = time.process_time()
manSprite.index = nextElement(runningRightIndexes, manSprite.index)
#helper function - returns next element within array relative to specified element
def nextElement(arr,elem):
if (elem in arr):
return arr[(arr.index(elem)+1)%len(arr)]
return arr[0]
#resets appearance to standing
def standStraightMan(e):
global manSprite
manSprite.index = 0 #index = 0 is the standing appearance
App().listenKeyEvent("keydown","right arrow",moveManRight)
App().listenKeyEvent("keyup","right arrow",standStraightMan)
### Challenges
#
# 1. add an event so that the man can also run left when you press the left key
# 2. when the man runs left the appearance needs to change so he is facing left etc.
# 3. replace the man spritesheet with the ninja cat spritesheet which can be found here:
# https://github.com/tiffinschool/pythonChallenges/blob/master/catSpriteSheet64x64.png
App().run()