main.lua 5.12 KB
Newer Older
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import 'CoreLibs/graphics.lua'

print( "Example ~ Pickin' Sticks ~ RWSS" )

local gfx = playdate.graphics
local snd = playdate.sound

playdate.display.setRefreshRate( 50 )

screenSize = { width = 400, height = 240, gridWidth = 20, hudHeight = 20 }

images = {}
images["ground"]  = gfx.image.new( "images/ground.png" )

sheetDirection = {}
sheetDirection["left"] = 0
sheetDirection["up"] = 1
sheetDirection["right"] = 2
sheetDirection["down"] = 3

soundCollect = snd.sampleplayer.new( "audio/sound1.wav" )
soundCollect:setVolume( 0.5 )

gfx.setBackgroundColor( gfx.kColorWhite )

function DrawGround()  
  for y = 0, screenSize["height"], screenSize["gridWidth"] do
    for x = 0, screenSize["width"], screenSize["gridWidth"] do
      images["ground"]:draw( x, y )
    end
  end
end

function GetRandomX()
  return math.random( 0, screenSize["width"] - screenSize["gridWidth"] )
end

function GetRandomY()
  return math.random( screenSize["hudHeight"], screenSize["height"] - screenSize["gridWidth"] )
end

function KeepInBounds( obj )
  if ( obj["x"] < 0 ) then obj["x"] = 0 end
  
  if ( obj["x"] > screenSize["width"] - screenSize["gridWidth"] ) then
    obj["x"] = screenSize["width"] - screenSize["gridWidth"]
  end
  
  if ( obj["y"] < screenSize["hudHeight"] ) then obj["y"] = screenSize["hudHeight"] end
  
  if ( obj["y"] > screenSize["height"] - screenSize["gridWidth"] ) then
    obj["y"] = screenSize["height"] - screenSize["gridWidth"]
  end
end

function UpdatePlayer()
  playerObject["frame"] = playerObject["frame"] + playerObject["frameSpeed"]
  if ( playerObject["frame"] >= playerObject["frameMax"] ) then
    playerObject["frame"] = 0
  end
  
  playerObject["x"] = playerObject["x"] + (playerObject["velocityX"]*playerObject["walkSpeed"])
  playerObject["y"] = playerObject["y"] + (playerObject["velocityY"]*playerObject["walkSpeed"])
end

function DrawPlayer()
  playerObject["image"]:draw( playerObject["x"], playerObject["y"], gfx.kImageUnflipped, math.floor( playerObject["frame"] ) * playerObject["width"], playerObject["direction"] * playerObject["height"], playerObject["width"], playerObject["height"] )
end

function PlayerAccelerate( direction )
  playerObject["direction"] = sheetDirection[ direction ]
  if ( direction == "up" ) then
    playerObject["velocityY"] = -1
  elseif ( direction == "down" ) then
    playerObject["velocityY"] = 1
  elseif ( direction == "left" ) then
    playerObject["velocityX"] = -1
  elseif ( direction == "right" ) then
    playerObject["velocityX"] = 1
  end
end

function PlayerDeaccelerate( direction )
  if ( direction == "up" or direction == "down" ) then
    playerObject["velocityY"] = 0
  elseif ( direction == "left" or direction == "right" ) then
    playerObject["velocityX"] = 0
  end
end

function IsCollision( objA, objB )
  return (  objA["x"]                   <= objB["x"] + objB["width"] and
            objA["x"] + objA["width"]   >= objB["x"] + objB["width"] and
            objA["y"]                   <= objB["y"] + objB["height"] and
            objA["y"] + objA["height"]  >= objB["y"] + objB["height"] )
end

stickObject = {}
stickObject["x"] = GetRandomX()
stickObject["y"] = GetRandomY()
stickObject["width"] = 20
stickObject["height"] = 20
stickObject["image"] = gfx.image.new( "images/stick.png" )

playerObject = {}
playerObject["x"] = GetRandomX()
playerObject["y"] = GetRandomY()
playerObject["width"] = 20
playerObject["height"] = 20
playerObject["image"] = gfx.image.new( "images/girl.png" )
playerObject["score"] = 0
playerObject["walkSpeed"] = 2
playerObject["direction"] = 0
playerObject["frame"] = 0
playerObject["frameMax"] = 2
playerObject["frameSpeed"] = 0.1
playerObject["velocityX"] = 0
playerObject["velocityY"] = 0

function playdate.update()
  -- Updates
  UpdatePlayer()
  KeepInBounds( playerObject )
  KeepInBounds( stickObject )
    
  if ( IsCollision( playerObject, stickObject ) ) then
    stickObject["x"] = GetRandomX()
    stickObject["y"] = GetRandomY()
    playerObject["score"] = playerObject["score"] + 1
    soundCollect:play()
  end

  
  -- Drawing
  gfx.clear( gfx.kColorWhite )
  gfx.setColor( gfx.kColorBlack )
  DrawGround()
  stickObject["image"]:draw( stickObject["x"], stickObject["y"] )
  DrawPlayer()
  
  gfx.setColor( gfx.kColorWhite )
  gfx.fillRect( 0, 0, screenSize["width"], screenSize["hudHeight"] )
  gfx.setColor( gfx.kColorBlack )
  gfx.drawLine( 0, screenSize["hudHeight"], screenSize["width"], screenSize["hudHeight"] )
  gfx.setColor( gfx.kColorBlack )
  gfx.drawText( "Score: " .. playerObject["score"], 2, 2 )
  gfx.drawText( "Pickin' Sticks", 300, 2 )
end

function playdate.leftButtonDown()    PlayerAccelerate( "left" )  end
function playdate.leftButtonUp()      PlayerDeaccelerate( "left" )  end
                 
function playdate.rightButtonDown()   PlayerAccelerate( "right" )  end
function playdate.rightButtonUp()     PlayerDeaccelerate( "right" )  end
                      
function playdate.upButtonDown()      PlayerAccelerate( "up" )  end
function playdate.upButtonUp()        PlayerDeaccelerate( "up" )  end
                            
function playdate.downButtonDown()    PlayerAccelerate( "down" )  end
function playdate.downButtonUp()      PlayerDeaccelerate( "down" )  end