-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
205 lines (172 loc) · 4.51 KB
/
main.lua
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
cart_id="picoracerv01"
-- init
function _init()
cartdata(cart_id)
titlescreen:init()
end
-- update
function _update60()
update()
end
-- draw
function _draw()
draw()
end
game = {
horizon=70,
stripeoffsets={0,0,0}, -- 1 for grass, 2 for verge, 3 forstripe
speed=0,
bend=0
}
function game:init()
-- set state functions
update=function ()
game:update()
end
draw=function ()
game:draw()
end
xm=128
ym=32
cls(0)
end
function game:update()
if btn(0)
then
self.bend+=0.01
self.bend=self.bend>=1 and 1 or self.bend
end
if btn(1)
then
self.bend-=0.01
self.bend=self.bend<=-1 and -1 or self.bend
end
if btnp(2)
then
self.speed=self.speed>=10 and 10 or self.speed+1
end
if btnp(3)
then
self.speed=self.speed<=0 and 0 or self.speed-1
end
end
function game:draw()
-- self:draw_sin()
printh("draw")
self:draw_sky()
self:draw_road()
print("speed: "..self.speed,4,4,0)
print("bend: "..self.bend,4,11,0)
end
p8cos = cos function cos(angle) return p8cos(angle/(3.1415*2)) end
p8sin = sin function sin(angle) return -p8sin(angle/(3.1415*2)) end
function game:draw_sin()
cls(0)
if btn(0)
then
--xm+=1
ym-=1
end
if btn(1)
then
--xm-=1
ym+=1
end
for x=1,128,1 do
y=y*20
printh(x0)
pset(x,y+64,7)
pset(x,64)
end
end
-- Screen elements
function game:draw_sky()
rectfill(0,0,127,self.horizon-1,12)
end
function game:get_color(value, position, length, c1, c2)
local x0 = abs(value/length)
local x1 = 2*((1-1.5*x0)^2+0.01*position)
local y = p8sin (x1)
return y<=0 and c2 or c1
end
-- start at the bottom of the screen
-- draw one line, consisting of: grass, verge, road, verge, grass
-- move up
function game:draw_road()
local roadwidthhalf,vergewidth,stripewidth,roadcentre,grassstripe,vergestripe,centrestripe = 40,10,4,64,self.stripeoffsets[1],self.stripeoffsets[2],self.stripeoffsets[3]
local vergelength,stripelength,grasslength = 100,100,100
-- recalculate initial offsets for next frame
self.stripeoffsets[1]=self.stripeoffsets[1]>=grasslength and 0 or self.stripeoffsets[1]+self.speed
self.stripeoffsets[2]=self.stripeoffsets[2]>=vergelength and 0 or self.stripeoffsets[2]+self.speed
self.stripeoffsets[3]=self.stripeoffsets[3]>=stripelength and 0 or self.stripeoffsets[3]+self.speed
local pos = -1 * (128-self.horizon)
for y=128,self.horizon,-1 do
-- work out bend offset for this row - at bottom of screen, will be zero
-- left is negative, right is positive
local mult = (128-y)/(128-self.horizon)
mult = mult^3
local bendoffset = mult * self.bend * 100
-- 0 - -(128-self.horizon)
printh(pos)
local grasscolour = self:get_color(pos,grassstripe, grasslength, 3, 11)
local vergecolour = self:get_color(pos,vergestripe, vergelength, 7, 8)
local stripecolor = self:get_color(pos,centrestripe, stripelength, 5, 7)
pos += 1
for x=0,127 do
local colour
if x < roadcentre-roadwidthhalf-vergewidth+bendoffset
then
colour = grasscolour
elseif x < roadcentre-roadwidthhalf+bendoffset
then
colour = vergecolour
elseif x < roadcentre+roadwidthhalf+bendoffset
then
if x < roadcentre+stripewidth/2+bendoffset and x >= roadcentre-stripewidth/2+bendoffset
then
colour = stripecolor
else
colour = 5 --road
end
elseif x < roadcentre+roadwidthhalf+vergewidth+bendoffset
then
colour = vergecolour
else
colour = grasscolour
end
pset(x,y,colour)
end
-- decrease width for perspective
roadwidthhalf-=0.35
vergewidth-=0.1
end
end
player = {
speed = 0
}
function player:init()
end
function player:update()
end
function player:draw()
end
titlescreen = {
showfor=300,
timer=0
}
function titlescreen:init()
-- set state functions
update=function ()
titlescreen:update()
end
draw=function ()
titlescreen:draw()
end
end
function titlescreen:update()
if (btnp(5)) game:init()
end
function titlescreen:draw()
cls(0)
print("press ❎ to start",30,120,7)
end