-
Notifications
You must be signed in to change notification settings - Fork 2
/
285rushhour.bas
579 lines (485 loc) · 18.8 KB
/
285rushhour.bas
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
' 285 Rush Hour
' by Mike Kopack
' July 2020
' You are free to examine, play with and reuse any of this code as you wish. I provide it as example on how
' to make a simple game for the CMM2 to help others. All I ask is that you give credit in your code for anything
' you use of mine to me (like in a readme or something). Otherwise, have at!
#include "stdsettings.inc"
option base 1
Mode 1,8
' show the intro title screen and play the title music,
'showtitlescreen()
' ask for which controller type
let controllertype=controllersetup()
'stop the music
'play stop
' set up some variables that we need to be global since they are used in different places.
' keeps track of the X position on the left side of each lane for positioning the AI elements in the lanes.
dim integer lanes(10) = (208,246,286,326,363,409,445,486,526,565)
' these are related to the overpass being shown and when
DIM integer overpass_at_score(10) = (2000,6000,9000,13500,18000,25000,35000,55000,75000,90000)
Let overpassvisbile = 0 '0 if the overpass isn't visible, 1 if it is
Let nextoverpass = 1 'which is the next overpass trigger score
LET overpass_yposition = -130 ' starting position for the overpass so it's off the screen
const numcars=10 ' number of AI cars to handle
dim integer aicars(numcars,4) '(lane#, preferredspeed, currentyposition, active=1 inactive=0)
'let carheight=0 'this is how tall y a car is 'this will be changed later when we have different kinds of cars
CONST drag = 1 'how much to slow the player's car down if they don't give it gas
let keeprunning = 1 ' to keep the "game" running. Once this is set to 0 we end the main loop
let currenttime = 0 ' holds the current time at the start of the cycle
LET starttime = 0 'this will hold the clock time at the start of the run
let stopwatch = 0 'this will hold the diff between the current time and the start time to act as a stopwatch for the run
Let delay = 0 ' holds the delay remaining at the end of the cycle that we need to pause for.
dim integer targettimeperframe = 1000/30 'target 30 frames per second
let score = 0 ' this will count how many many vertical lines the player has covered before collision.
let l_count=0 ' used if keyboard control to count how many times in a row we've pressed this key
let r_count=0 ' used in keyboard control to count how many times in a row we've pressed this key
let playerspeed = 0 ' the faster our speed, the farther down we draw the dashed lines each frame
let playerx = lanes(10) ' the starting x position of the player's car
let soundfreq = 15 'this changes with the speed
' Get the game screen set up
setuproad()
' setup all the sprites we need.
loadsprites()
'initialize the start time so we can keep a stopwatch of the run
starttime=time
' main rendering/game loop
do while keeprunning
timer=0
' handle input from the user
handleinput()
'update the road
updateroad()
'update the overpasses
'updateoverpass()
' update obstacles
'updateobstacles()
' update the ai cars
handleai()
' ok, now call the command that makes all the sprites actually move at once to their new positions.
sprite move
' this is all debugging stuff
' blank the area where we're writing text so we don't get leftover artifacts from last frame
box 0,0,160,80,,RGB(0,255,0),1
text 10, 26, "Xpos = "+str$(playerx), LB,,,RGB(255,255,255)
text 10, 36,"Speed = "+str$(playerspeed), LB,,, RGB(255,255,255)
'if we are done before the targettime per frame, wait the difference
' if this comes out negative it'll just immediately cycle to the next
' iteration (but that means we're not keeping up)
delay = targettimeperframe-TIMER
' this is just for debugging so we can see if we're taking too long per frame
if(delay<0) then missedframes = missedframes+1
text 10, 14, str$(delay), LB,,, RGB(255,255,255)
' updatescore()
'updatemap() ' eventually add this in so we can update the overhead map to show progress
' copy the back buffer to the front display
page copy 1 to 0
if(delay>0) then pause delay
loop
endgame()
end
'--------------------------------------------------------------------
' define what to do when there's a collision between sprites
sub collision()
local integer i
if(sprite(S)<>0) then
process_collision(sprite(S))
else
for i=1 to sprite(C,0)
process_collision(sprite(C,0,i))
next i
endif
End sub
'--------------------------------------------------------------------
' handle determining who caused the collision
sub process_collision(S as integer)
local integer i,j
for i = 1 to sprite(C,S)
j=sprite(C,S,i)
' see if it's a collision between player's car and AI car
if(S=1 or j=1) then
'we only stop if the player's car was in a collision with an AI Car (or something else on the road)
keeprunning=0
endif
next i
end sub
'--------------------------------------------------------------------
' figure out what the ai cars should do
sub handleai()
local x=0
local y=0
local carheight=0
local orientation=0
local type=0
local col=0
local sprnum =0
local integer lanefound =0 ' used to indicate if we've found a valid lane with room
local integer templane =0 ' used in AI calcs to hold the lane #
local integer othercarinlane=0 ' flag if we found another car in the lane that we need to slow down for
for i=1 to numcars
sprnum=i+1
' for each car, see if it's active
if(aicars(i,4)=1) then
' it's active so we need to move it,
' it's yposition should move relative to the player's speed and it's own speed
othercarinlane=0
'see if there's another car in this lane ahead of this one and within 1.5 car lengths of this AI car
for car = 1 to numcars
'see if it's a different car, it's active, in the same lane, and it's y+1.5*height of the car is < this car and has a > speed than the car above it
if(i<>car and aicars(car,4)=1 and aicars(car,1) = aicars(i,1)) then
' deal with the cars depending upon which side of the road they're on
if(aicars(i,1) > 5) then
if (aicars(car,3)+(sprite(H,car+1)*1.5)+aicars(i,2))<aicars(i,3) and aicars(car,2)<=aicars(i,2) ) then
'make this car match the ahead car's speed.
aicars(i,2) = aicars(car,2)
othercarinlane=1
endif
else
if(aicars(car,3)<aicars(i,3)+(sprite(H,car+1)*1.5)+aicars(i,2) and aicars(car,2)<aicars(i,2) ) then
'make this car match the ahead car's speed.
aicars(i,2) = aicars(car,2)
othercarinlane=1
endif
endif
endif
next
' if we didn't find any other in the lane, go ahead and move at normal speed
if(othercarinlane=0) then
if(aicars(i,1) > 5) then
aicars(i,3) = -1*aicars(i,2)+playerspeed+aicars(i,3)
else
aicars(i,3) = aicars(i,2)+playerspeed+aicars(i,3) ' left lanes should always come towards you
endif
endif
'update it's position
'if it's offscreen high, don't draw it more than 1 sprite's worth above
templane = aicars(i,1)
'if it's below bottom, hide it and make it inactive
if(aicars(i,3) > MM.VRES-1) then
sprite hide sprnum
sprite close sprnum
aicars(i,4)=0
else if(aicars(i,3) <= -1*SPRITE(H,sprnum)) then
' if it goes off the top of the screen, we have to not move the sprite any farther, but keep track of
' how far up the road it HAS moved.
' sprite height pixels.
sprite next sprnum,lanes(templane),-1*sprite(H,sprnum)+1
else
sprite next sprnum,lanes(templane),aicars(i,3)
endif
else
' if it's inactive, randomly decide if it should show up and where
if((RND)>.9) then
lanefound =1
'pick a lane
templane = int(rnd*10)+1
'see if any other cars are in that lane, active and within 1 car length. If so, retry
for car=1 to numcars
if(aicars(car,4)=1 and aicars(car,1)=templane) then
'see if the car below the spawn area, if so, then this lane is ok
if (aicars(car,3)>60) then
lanefound=1
else
' otherwise it's no good
lanefound=0
exit for
endif
endif
next car
'if we found a valid place to put the new car, place it
if lanefound =1 then
aicars(i,1) = templane
'pick a speed
aicars(i,2) = int(rnd*12)+1
aicars(i,4) = 1 ' make it active
'pick a color and type of AI car
type = int(rnd*5)+1 ' type by row
col = int(rnd*5)+1 ' color by column
carheight=0
orientation=0
' copy the sprite in from the sprite page (page 2)
select case type
case 1
carheight= 65
case 2
carheight= 57
case 3
carheight= 66
case 4
carheight= 66
case 5
carheight= 61
end select
'left lane cars need to be rotated 180
if(aicars(i,1)<6) then
orientation=3
else
orientation=0
endif
'copy the randomly selected graphics from page 2 where we loaded them into the sprite
sprite read sprnum, col*70, type*80, 35,carheight,2
'now set the sprite depending on the lane
templane = aicars(i,1)
aicars(i,3) = (-1*carheight)+1 ' can't set it more than 1 sprite's size off screen
' show the sprite
x=lanes(templane)
y=aicars(i,3)
sprite show sprnum,x,y,2,orientation
endif
endif
endif
next i
end sub
'--------------------------------------------------------------------
sub setuproad()
'set up so we write to the background buffer
page write 1
' First, clear the screen...
cls RGB(0,255,0)
' draw the road
' the street surface will be gray
BOX 200, 0, 400, 600,,rgb(128,128,128),1
' draw the lines on the edges of the road solid white
line 205,0, 205,600, 3, RGB(255,255,255)
Line 595,0,595,600,3,RGB(255,255,255)
' draw the double centerline in yellow
Line 394,0, 394,600,3,RGB(128,128,0)
line 406,0, 406,600,3,RGB(128,128,0)
' the lane lines will be done with sprites so we can make them roll down the screen as the player
' move
' we first draw the dashed lines
for i = 0 to 12 step 2
for j = 1 to 4
Line 200+(j*40), 50*i, 200+(j*40), 50*(i+1), 3, RGB(255,255,255)
line 400+(j*40), 50*i, 400+(j*40), 50*(i+1), 3, RGB(255,255,255)
next
next
end sub
'--------------------------------------------------------------------
SUB loadsprites()
' ok let's load in the sprites. We need to resize them to fit the lanes.
' switch over to a temp page
page write 2
load png "CarBlue.png",0,0,15
image resize 2,2,80,120,200,0,30,45
sprite read 1,200,0,30,45,2
' load png "CarRed.png",0,0,15
' image resize 2,2,80,120,200,0,30,45
' sprite read 2,200,0,30,45,2
' put the player on layer 2 so it doesn't collide with the lane lines or the overpass
' and start it in the rightmost lane
sprite show 1,lanes(10),400,2
'sprite copy 2,3,(numcars-1) ' copy the AI car to sprite 3, numcars-1 copy.
' ok clear that page so we can load in the 25 PNGs for the AI cars
cls rgb(0,0,0)
' load in the 25 different car PNG's onto page 2
local col$ = ""
for i = 1 to 5
for j = 1 to 5
select case j
case 1
col$="black"
case 2
col$="red"
case 3
col$="green"
case 4
col$="blue"
case 5
col$="yellow"
end select
load png "A:Cars/car_"+col$+"_small_"+str$(i), j*70,i*80
next
next
'let's show it so we know they all got loaded ok
page copy 2 to 0
pause 2000
' set up to make the overpass in page 3
page write 3
' ok clear the screen that we loaded the sprites from
cls rgb(0,255,0)
' create the sprite for the overpass
box 0,0,MM.HRES-1,130,,RGB(128,128,128),1
' draw the road edge lines
line 0,3,MM.HRES-1,3,3,RGB(255,255,255)
line 0,127,MM.HRES-1,127,3,RGB(255,255,255)
' draw the centerline
Line 0,63,MM.HRES-1,63,3,RGB(127,127,0)
Line 0,68,MM.HRES-1,68,3,RGB(127,127,0)
'read that all into a sprite on layer 1
sprite read 64,0,0,MM.HRES-1,130,1
cls
' page copy 3 to 0
' switch back to page 1 so we can get things into the drawing page
page write 1
' set up the function to handle collisions
sprite interrupt collision
END SUB
'--------------------------------------------------------------------
sub handleinput()
' read the value of the controllertype so we know what to read and how
local integer leftstickx = 0
local integer rightsticky = 0
local integer changex = 0
select case controllertype
case 1 ' classic controller
' read the joystick and update the game state
' get the left/right
leftstickx = CLASSIC(LX,3)
' get the throttle changes
rightsticky = CLASSIC(RY,3)
' this should adjust the speed
if(rightsticky>140) then playerspeed = playerspeed +1
if(rightsticky<100) then playerspeed = playerspeed -1
if(rightsticky<=140 and rightsticky>=100) then playerspeed =playerspeed - drag
' this SHOULD make it so the farther left/right you push the more it will change
changex = (leftstickx-127) / 64
' see if the player hit the home button to quit
buttons = classic(B,3)
if(buttons and 4) then keeprunning = 0
case 2 'nunchuk
leftstickx = NUNCHUK(JX,3)
rightsticky= NUNCHUK(Z,3) ' use the Z button for gas
buttons = Nunchuk(C,3)
if(buttons) then keeprunning=0
' this should adjust the speed
if(rightsticky=1) then playerspeed = playerspeed +1 else playerspeed = playerspeed -drag
' this SHOULD make it so the farther left/right you push the more it will change
changex = (leftstickx-127) / 64
case 3 'keyboard
local numkeys=keydown(0)
if (numkeys <> 0) then
' something pressed,figure out what
for i=1 to numkeys
select case keydown(i)
case 128
'speed up
playerspeed=playerspeed+1
case 130
'left
l_count=l_count+1
if l_count>4 then l_count=4
r_count=0
changex=-1*l_count
case 131
'right
l_count=0
r_count=r_count+1
if r_count>4 then r_count=4
changex=r_count
end select
next
else
'nothing pressed so slow down
playerspeed=playerspeed-drag
endif
end select
if(playerspeed < 0) then playerspeed = 0
if(playerspeed > 15) then playerspeed = 15
'adjust the engine sound based on the speed
if (playerspeed+20) <> soundfreq then
soundfreq=playerspeed+20
play sound 1, b, w, soundfreq,25
endif
' make it so you can't steer unless moving
if(playerspeed=0) then changex =0
playerx = playerx + changex
' make sure it stays within the limits while still allowing off-sides of the road a bit.
if(playerx < 100) then playerx = 100
if(playerx > 700) then playerx = 700
' update the positions of the player's sprite based on the player's movement
sprite next 1, playerx,400
END SUB
'--------------------------------------------------------------------
SUB updateroad()
' scroll the layer lines according to the players' speed
for i =0 to 3
sprite scrollr 238+(i*40),0,5,MM.VRes,0,-1*playerspeed
sprite scrollr 438+(i*40),0,5,MM.VRes,0,-1*playerspeed
next
End sub
'--------------------------------------------------------------------
SUB endgame()
'end game
' close out all the sprites
sprite close all
' change to the 0 page
page write 0
' stop all sound
play stop
' if using the Wii classic or nunchuk, close it out
if(controllertype=1) then wii classic close 3
if(controllertype=2) then wii nunchuk close 3
' give the system a chance to catch up
pause 100
'clear the screen
'cls RGB(0,0,0)
' output diag info and final score...
print "We missed :"+str$(missedframes)+" frames during run..."
print "Final score: "+STR$(score)
end sub
'--------------------------------------------------------------------
' figure out which type of control the player is using
function controllersetup() as integer
local returnvalue
' Prompt for which controller type to use
input "Which input type (1=Classic, 2=Nunchuk, 3=Keyboard)"; value
' if they select classic, set controllersetup = 1
' if they select nunchuk set controllersetup = 2
' if they select keyboard set controllersetup = 3
' for now we'll hard code for classic, but later we'll read what they select
returnvalue =value
if(returnvalue = 1) then
'Open the Wii Classic Controller
wii classic open 3
else if(returnvalue=2) then
'open the wii nunchuk
wii nunchuk open 3
else
returnvalue=3
endif
controllersetup=returnvalue
end FUNCTION
'--------------------------------------------------------------------
sub updatescore()
'update the score based on how far the player moved
score = score + playerspeed
text 10, 48, "Score = "+STR$(score), LB,,, RGB(255,255,255)
stopwatch = time-starttime
text 10, 60, "Time = "+STR$(stopwatch)+" ms", LB,,,RGB(255,255,255)
end sub
'--------------------------------------------------------------------
sub showtitlescreen()
'load the graphic
'start the music
'pause for 5 seconds and then we're done
end sub
'--------------------------------------------------------------------
'update the overpasses
sub updateoverpass()
' first see if the overpass isn't visible. If not, see if it should be because we tripped the next score trigger
if(overpassvisible=0) then
'see if the score is now over the threshold of triggering the next overpass to show up
if(score>=overpass_at_score(nextoverpass)) then
overpass_yposition = -129
'show the overpass, put it on layer 0
sprite show 64,0,overpass_yposition,0
overpassvisible=1
nextoverpass=nextoverpass+1
endif
else 'is visible
' update it's position
overpass_yposition=overpass_yposition+playerspeed
' see if the player has moved enough to move the overpass off the bottom of the screen.
if(overpass_yposition>=MM.VRES) then
sprite hide 64
overpassvisible=0
else
sprite next 64,0,overpass_yposition
endif
endif
end sub
'--------------------------------------------------------------------
' update obstacles
sub updateobstacles()
end sub