03-14-2024, 03:07 PM
(03-13-2024, 11:18 AM)johnno56 Wrote: .. Like Lord of the rings...Lord of the Rings...mmm... you've just given me an idea to make an old RPG style with the characters from the Lord of the Rings
Thank you for your advice.
Progress Day 4
click the image to zoom in
- Added moving traffic signs on the left side of the road
- Added moving trees on the right side of the road
- Eight-Shape Road track with player's position along the track
- Press S to silent (music and sound effect)
Next
Only 1 remaining day to finish the game
- Collision detection with the traffic signs or trees
- Update Life if collision is detected
Code:
'---------------------------------------
' THE FAST AND THE FURIOUS - DAY 4
'
' Control :
' - movement : LEFT, RIGHT
' - speed : UP, DOWN
' - quit : ESC
' - music,sound off : S
'
' Note :
' This game is just a proof of concept.
' The duration of the game is very short.
' You may extend the length of the road by changing the value of road.length
'
' References :
' 1. CURVED ROAD, texture mapping, N7_24.03.05, by Marcus
' DrawImageTransform() function 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]
green = [0,255,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
free image city 'remove from memory
free image woods
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
barrier = []
barrier[1] = []
barrier[2] = []
barrier[1].img = loadimage("assets/side_sign.png")
barrier[2].img = loadimage("assets/side_tree.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
playsound = 1
'------------
' MAIN LOOP
'------------
while not keydown(KEY_ESCAPE, true)
'Music and Sound control
if keydown(KEY_S,true) then
stop music background.music_
free music background.music_
playsound=0
endif
'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
if playsound=1 then; play sound player.gas,1; endif
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
'Distance, Life, Road Track
set color white ; set font info.myfont
distance = (i*10)
set caret 250,5; wln "Distance: "+ distance +" Km"
set caret 5,5; wln "Speed: "+round(player.speed)
set caret width(primary)-90,5; wln "Life : "+info.life
'calculate the car's position on the road track
'ratio of (current distance/total distance) times 360 degree
cd = distance/1010*360
'Road Track
for d = 0 to 360
t = rad(180-d)
posx = 60*sin(t) + width(primary)/2
posy = 60*sin(t)*cos(t) + 80
set color white; draw pixel posx,posy
'draw car's position on the road track
tcd = rad(180-cd)
posxcd = 60*sin(tcd) + width(primary)/2
posycd = 60*sin(tcd)*cos(tcd) + 80
set color green; draw ellipse posxcd,posycd,5,5,1
next
'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
'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 road
set color white; draw poly image roadImg[i%2], p
'Draw barriers
if z >= 0.5 then
sf = 1/z+0.3 'scale factor
barrier[1].x = x0l-20; barrier[1].y = y0
barrier[2].x = x0r+10; barrier[2].y = y0
if i%10 = 0 then
Draw(barrier[1].img, barrier[1].x, barrier[1].y, sf)
else
barrier[1].y = -10
barrier[1].x = -10
endif
if i%2 = 0 then
Draw(barrier[2].img, barrier[2].x, barrier[2].y, sf)
else
barrier[2].y = -10
barrier[2].y = - 10
endif
endif
endif
next
'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 or info.life <= -1 then
draw image info.finish,(width(primary)-width(info.finish))/2,120;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 = 0; py = height(img)
DrawImageTransformed(img, x,y, s, s, a, px, py)
endfunc