03-13-2024, 09:23 AM
(03-12-2024, 06:29 PM)johnno56 Wrote: Moving scenery? Nice touch...
Small suggestion. Two key presses for changing direction. Left and Right. Rather than pressing the Up key to "straighten" the car, perhaps make it that if the Left or Right key is NOT pressed then return the car "cell" to 3? That way you can reserve the Up and Down keys to control the speed etc...
3 days? You have set a time limit? Interesting.
click the image to zoom in
Progress Day 3
- Add background rock music (looping)
- Add sound effect for car's acceleration
- No more KEY_W or KEY_S for acceleration
- LEFT and RIGHT for changing direction and in 0.5 second would return to normal position (cell 3) or "straighten" automatically
- KEY UP to accelerate the car and deaccelerate automatically .... whaaaat ?
- Add a nice circle Speedometer
Next
- Incoming barriers (fence, dirt etc.) perhaps these are the most challenging to code
- Street / Race Track
Remaining Days to Finish The Game
- 2 days before this weekend .... aaarrrggghhh... I lost a lot of my hairs ....
I have set a time limit for my personal reason only, so that this project would not be a "Never ending story"
Code:
'---------------------------------------
' THE FAST AND THE FURIOUS - DAY 3
'
' Works to do :
' 1. Incoming barrier : fence, road barrier,
' billboard, cliffs, signs, snowman,trees
' if hit barrier Life = Life - 1
' 2. street track
'
' Control :
' - movement : LEFT, RIGHT
' - speed : UP, DOWN
' - quit : ESC
'
' Note :
' This game is just a proof of concept.
' The duration is very short.
' You may extend the length of the road
' Find this variable to change : road.length = 120
'
' References :
' 1. CURVED ROAD, texture mapping, N7_24.03.05, by Marcus
' 2. Sprites https://opengameart.org/content/25d-racing-resources
' 3. Sound Effect and Music https://pixabay.com
'----------------------------------------
'----------------
' INITIALIZATION
'----------------
'include "assets/transform.n7"
set window "The Fast and The Furious", 640, 400, false
set redraw off
'Color definition
blue = [64, 64, 128]
brown = [200, 128, 64]
white = [255, 255, 255]
magenta = [255,0,255]
red = [255,0,0]
'Background city and woods
city = loadimage("assets/back_city.png")
woods = loadimage("assets/back_woods.png")
background = []
background.img = createimage(width(city)*3*20,63)
set image background.img
set color blue;cls;set color white
for i = 0 to 60 step 3
draw image woods,0+i*width(city),0
draw image city,width(city)+i*width(city),0
draw image woods,2*width(city)+i*width(city),0
next
set image primary
background.dx = 0
background.music_ = loadmusic("assets/rock.wav")
' -------------- Road Textures ------------
roadImg = []
' First textures
roadImg[0] = createimage(160, 16)
set image roadImg[0]
for y = 0 to 15 for x = 0 to 159
if rnd(3) = 0 set color 128, 128, 128
else set color 112, 112, 112
set pixel x, y
next
set color white
draw rect 0, 0, 8, 16, true
draw rect 152, 0, 8, 16, true
set image primary
' Second textures
roadImg[1] = createimage(160, 16)
set image roadImg[1]
for y = 0 to 15 for x = 0 to 159
if rnd(3) = 0 set color 96, 96, 96
else set color 80, 80, 80
set pixel x, y
next
set color white
draw rect 76, 0, 8, 16, true
set image primary
' ------------------------------------------
' Curved Road
road = []
road.length = 120 'default value = 120
for z = 0 to road.length
road[z] = sin(z*0.05)*2
next
'Road
p = dim(16)
p[2] = 0; p[3] = 0
p[6] = 160; p[7] = 0
p[10] = 160; p[11] = 16
p[14] = 0; p[15] = 16
'Barrier : fence, road barrier, billboard, cliffs, signs, snowman,trees
barrier = []
barrier[1] = loadimage("assets/barrier_fence.png")
barrier[2] = loadimage("assets/barrier_road.png")
barrier[3] = loadimage("assets/barrier_trash.png")
barrier[4] = loadimage("assets/side_billboard.png")
barrier[5] = loadimage("assets/side_cliff.png")
' Player
player = []
player.img = loadimage("assets/car_player.png",7,1)
player.cell = 3 '0=right, 6=left, 3=normal
player.z = 0.0
player.x = 0
player.speed = 10 '10=slowest
player.gas = loadsound("assets/acceleration.wav")
'Score, Life, Finish Line
info = []
info.myfont = createfont("Arial",30,1,0,0,0)'name,size,bold,italic,underlined,smoothed
info.life = 3
info.finish = loadimage("assets/finish.png")
info.timeStart = 0; info.timeDuration = 0 'timer's variables
play music background.music_,1 'looping
'------------
' MAIN LOOP
'------------
while not keydown(KEY_ESCAPE, true)
'Player's Control
player.z = player.z + 0.075
if keydown(KEY_LEFT,true) then
player.x = player.x - 0.1
player.cell =6
info.timeStart = clock()
elseif keydown(KEY_RIGHT,true) then
player.x = player.x + 0.1
player.cell =0
info.timeStart = clock()
endif
info.timeDuration = (clock() - info.timeStart)/1000
if info.timeDuration > 0.5 then player.cell = 3
if keydown(KEY_UP,true) and player.speed <=80 then
player.speed = player.speed+10
play sound player.gas,1
elseif keydown(KEY_DOWN,true) and player.speed >=20 then
player.speed = player.speed-10
endif
if player.speed > 10 then player.speed = player.speed - 0.2
'Scene : blue sky, brown earth
set color blue; cls
set color brown; draw rect 0, height(primary)/2+0.03*height(primary), width(primary), height(primary)/2, true
'Scene : animated road
set color white
for i = int(player.z) + 15 to int(player.z)
if i < sizeof(road) - 2
z = i + 1 - player.z + 1.1
x0l = width(primary)/2 + (0.75*width(primary)-0.125*width(primary))*(road[i + 1] - 1 - player.x)/z
x0r = width(primary)/2 + (0.75*width(primary)-0.125*width(primary))*(road[i + 1] + 1 - player.x)/z
y0 = height(primary)/2 + (height(primary)/2+0.04*height(primary))/z
z = i - player.z + 1.1
x1l = width(primary)/2 + (0.75*width(primary)-0.125*width(primary))*(road[i] - 1 - player.x)/z
x1r = width(primary)/2 + (0.75*width(primary)-0.125*width(primary))*(road[i] + 1 - player.x)/z
y1 = height(primary)/2 + (height(primary)/2+0.04*height(primary))/z
p[0] = x0l; p[1] = y0;
p[4] = x0r; p[5] = y0
p[8] = x1r; p[9] = y1;
p[12] = x1l; p[13] = y1
draw poly image roadImg[i%2], p
endif
next
'Distance, Life
set color white ; set font info.myfont
distance = (i*10)
set caret 5,5; wln "Distance: "+ distance +" Km";set caret 300,5; wln "Speed: "+round(player.speed)
set caret width(primary)-90,5; wln "Life : "+info.life
'Background : city, woods
if distance < 450 then
background.dx = background.dx + 2
else
background.dx = background.dx - 2
endif
set color white
draw image background.img,(-width(background.img)/2)+background.dx,height(primary)/2-50
'Dashboard : Speedometer
angle = player.speed/10*30
set color white; draw ellipse 50,110-30,40,40 ; draw ellipse 50,110-30,41,41
set color red ; draw ellipse 50,110-30,5,5,1
draw line 49,109-30, 49+30*cos(rad(80+angle)),109-30+30*sin(rad(80+angle))
draw line 50,110-30, 50+30*cos(rad(80+angle)),110-30+30*sin(rad(80+angle))
draw line 51,111-30, 51+30*cos(rad(80+angle)),111-30+30*sin(rad(80+angle))
'Draw player
set color white; draw image player.img,width(primary)/2-width(player.img)/2,height(primary)-110,player.cell
redraw
'Time is Up
if i >= sizeof(road) - 20 then
draw image info.finish,(width(primary)-width(info.finish))/2,80;redraw
while not keydown(KEY_ESCAPE,true);wait 1; wend
end
endif
fwait player.speed
wend
'-----------
' FUNCTIONS
'-----------
'function Draw(img,x,y,s)
' DrawImageTransformed(image, x, y, scale_x, scaleY, angle, pivot_x, pivot_y)
' Parameter :
' image = .png format
' (x,y) = location of the image in the screen coordinates
' scale_x, scale_y = scale the image's size
' angle = in radian.
' (pivot_x, pivot_y) = center point of the image. E.g:(0,0) = top left.
' x = x; y = y ; s = s; a = 0
' px = width(img)/2; py = height(img)/2
' DrawImageTransformed(img, x,y, s, s, a, px, py)
'endfunc