Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


CRDR - CE Car Racing Game (math.random & bounds)

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Extensions
View previous topic :: View next topic  
Author Message
Corroder
Grandmaster Cheater
Reputation: 27

Joined: 10 Apr 2015
Posts: 987

PostPosted: Tue Jul 02, 2019 12:55 am    Post subject: CRDR - CE Car Racing Game (math.random & bounds) Reply with quote

Hi, there. Interested with this topic:

https://forum.cheatengine.org/viewtopic.php?t=610317
https://forum.cheatengine.org/viewtopic.php?t=610419

This is my turn to proofing CE (Lua) logic about using math.random() and bound intersect with() function or collision detector function. I decided to translate one of my C# racing game projects to CE Lua scripting.

in C#, there is a default function to check boundaries/objects collision, example:

Code:
if (player.Bounds.IntersectsWith(AI1.Bounds) || player.Bounds.IntersectsWith(AI2.Bounds))
            {
                gameOver();
            }


Since Lua doesn't have bound. bounce and collision functions by default, then we need to make a custom function for those events.

Generally in Lua to detect intersect/collision we use:

Code:
-- Collision detection function;
-- Returns true if two objects overlap, false if they don't;
-- x1,y1 are the top-left coords of the first object, while w1,h1 are its width and height;
-- x2,y2,w2 & h2 are the same, but for the second object.

function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
  return x1 < x2+w2 and
         x2 < x1+w1 and
         y1 < y2+h2 and
         y2 < y1+h1
end


Anyway, there is another Lua collision detection function present by Lynxz Gaming a CEF member.

Code:
function onCollision(obj1,obj2)
 obj1x = obj1.left
 obj1y = obj1.top
 obj1w = obj1.width
 obj1h = obj1.height
 obj2x = obj2.left
 obj2y = obj2.top
 obj2w = obj2.width
 obj2h = obj2.height
 if obj2x + obj2w >= obj1x and obj2y + obj2h >= obj1y and obj2y <= obj1y + obj1h and obj2x <= obj1x + obj1w then
 return true
 end
end


Not much different, right?.

Next is how to implementing math.random(n,n), which I used this Lua function to set some images position on my car racing games so they appear on a random position trigger by a timer.

Here is the complete project:

Project Details
Project Name: CRDR-CE Racing Game
Release Date: 01-07-2019
Author/Creator: Corroder a.k.a VCL Bro

Requirement
CE 6.8.3
Lua 5.3

Project Resource
Contain:
- Images for the project
- BG Music
- Event Sound

NOTE:
Extract the resources folder using WinRar and place it at the same location as your project.
Give attention with folder name.

Add to CE Table File:

- hit.wav
- race.xm

Download: https://www.mediafire.com/file/3uy8xsca1zz8zzc/CRDR_RacingResource.rar/file


Demo Video (C# project version able to download on video description)

https://youtu.be/NDNt6iLJHn8


Complete CE Lua script:

Code:
path = TrainerOrigin or getMainForm()
resourcepath = path..'//CRDR_RacingResource//'

-------------------------------------------------------------------- Objects
frm = createForm()
frm.setSize(405,580)
frm.borderStyle='bsSingle'
frm.Position='poScreenCenter'
frm.Caption='CRDR - CE Racing Game'
frm.Color ='1262987'

panel1=createPanel(frm)
panel1.setPosition(12.5,12.5)
panel1.setSize(380,425)
panel1.borderStyle='bsNone'
panel1.Color='0x000000'

lblDist = createLabel(frm)
lblDist.setPosition(90,panel1.Top+panel1.Height+5)
lblDist.Font.Name='OCR A Std'
lblDist.Font.Size=14
lblDist.Font.Color='61046'
lblDist.Caption='Distance :'

lblDistValue = createLabel(frm)
lblDistValue.setPosition(lblDist.Left+lblDist.Width+5,lblDist.Top)
lblDistValue.Font.Name='OCR A Std'
lblDistValue.Font.Size=14
lblDistValue.Font.Color='61046'
lblDistValue.Caption=' 0000'

btnStart=createButton(frm)
btnStart.setSize(160,40)
btnStart.setPosition(lblDist.Left+30,lblDist.Top+lblDist.Height+10)
btnStart.Font.Size=20
btnStart.Font.Name='OCR A Std'
btnStart.Caption='START'

lblRule1 = createLabel(frm)
lblRule1.setPosition(65,btnStart.Top+btnStart.Height+20)
lblRule1.Caption='Avoid other cars and get point'
lblRule1.Font.Name='OCR A Std'
lblRule1.Font.Size=10
lblRule1.Font.Color='1219071'

lblRule2 = createLabel(frm)
lblRule2.setPosition(45,lblRule1.Top+lblRule1.Height+3)
lblRule2.Caption='Use LEFT and RIGHT key to MOVE car'
lblRule2.Font.Name='OCR A Std'
lblRule2.Font.Size=10
lblRule2.Font.Color='1219071'

roadTrack1=createImage(panel1)
roadTrack1.setSize(385,682)
roadTrack1.setPosition(-2,-632)
roadTrack1.Stretch=true
roadTrack1.loadImageFromFile(resourcepath..'roadTrack.jpg')

roadTrack2=createImage(panel1)
roadTrack2.setSize(385,682)
roadTrack2.setPosition(-3,-250)
roadTrack2.Stretch=true
roadTrack2.loadImageFromFile(resourcepath..'roadTrack.jpg')

Al1=createImage(panel1)
Al1.setSize(50,101)
Al1.setPosition(66,19)
Al1.Stretch=true
Al1.loadImageFromFile(resourcepath..'carGrey.png')

Al2=createImage(panel1)
Al2.setSize(50,101)
Al2.setPosition(294,85)
Al2.Stretch=true
Al2.loadImageFromFile(resourcepath..'carGreen.png')

explosion=createImage(panel1)
explosion.setSize(200,200)
explosion.setPosition(153,234)
explosion.Stretch=true
explosion.loadImageFromFile(resourcepath..'explosion.gif')
explosion.Visible = false

player=createImage(panel1)
player.setSize(50,101)
player.setPosition(161,286)
player.Stretch=true
player.loadImageFromFile(resourcepath..'carYellow.png')

trophy=createImage(panel1)
trophy.setSize(250,100)
trophy.setPosition(66,157)
trophy.Stretch=true
trophy.loadImageFromFile(resourcepath..'bronze.png')
trophy.Visible=false

-------------------------------------------------------------------- Handlers
frm.DoubleBuffered = true

function onCollision(obj1,obj2)
 obj1x = obj1.left
 obj1y = obj1.top
 obj1w = obj1.width
 obj1h = obj1.height
 obj2x = obj2.left
 obj2y = obj2.top
 obj2w = obj2.width
 obj2h = obj2.height
 if obj2x + obj2w >= obj1x and obj2y + obj2h >= obj1y and obj2y <= obj1y + obj1h and obj2x <= obj1x + obj1w then
 return true
 end
end

-- set as global variables
carSpeed=5
roadSpeed=5
carLeft=false
carRight=false
trafficSpeed=5
Score=0

function Reset()
 trophy.Visible=false
 btnStart.Enabled=false
 explosion.Visible=false
 trafficSpeed=5
 roadSpeed=5
 Score=0
 player.Left=161
 player.Top=286
 carLeft=false
 carRight=false
 Al1.Left=66
 Al1.Top=120
 Al2.Left=294
 Al2.Top=185
 roadTrack2.Left=-3
 roadTrack2.Top=-250
 roadTrack1.Left=-2
 roadTrack1.Top=-632
 timer1.Enabled=true
-- timer2.Enabled=true
end

function moveCar(sender, Key)
 frm.setFocus()
 if Key==VK_LEFT and player.Left>0 then carLeft=true end
 if Key==VK_RIGHT and
     player.Left+player.Width<panel1.Width then carRight=true end
end
frm.onKeyDown = moveCar

function stopCar(sender,Key)
 frm.setFocus()
 if Key==VK_LEFT then carLeft=false end
 if Key==VK_RIGHT then carRight=false end
end
frm.onKeyUp = stopCar

function changeAl1()
-- change Al1 car images by random
 local num = math.random(1,8)
 if num==1 then Al1.loadImageFromFile(resourcepath..'carGreen.png')
 elseif num==2 then Al1.loadImageFromFile(resourcepath..'carGrey.png')
 elseif num==3 then Al1.loadImageFromFile(resourcepath..'carOrange.png')
 elseif num==4 then Al1.loadImageFromFile(resourcepath..'carPink.png')
 elseif num==5 then Al1.loadImageFromFile(resourcepath..'carRed.png')
 elseif num==6 then Al1.loadImageFromFile(resourcepath..'TruckBlue.png')
 elseif num==7 then Al1.loadImageFromFile(resourcepath..'TruckWhite.png')
 elseif num==8 then Al1.loadImageFromFile(resourcepath..'Ambulance.png') end
end

function changeAl2()
-- change Al2 car images by random
 local num = math.random(1,8)
 if num==1 then Al2.loadImageFromFile(resourcepath..'carGreen.png')
 elseif num==2 then Al2.loadImageFromFile(resourcepath..'carGrey.png')
 elseif num==3 then Al2.loadImageFromFile(resourcepath..'carOrange.png')
 elseif num==4 then Al2.loadImageFromFile(resourcepath..'carPink.png')
 elseif num==5 then Al2.loadImageFromFile(resourcepath..'carRed.png')
 elseif num==6 then Al2.loadImageFromFile(resourcepath..'TruckBlue.png')
 elseif num==7 then Al2.loadImageFromFile(resourcepath..'TruckWhite.png')
 elseif num==8 then Al2.loadImageFromFile(resourcepath..'Ambulance.png') end
end

function gameOver()
 trophy.Visible=true
 timer1.Enabled=false
 btnStart.Enabled=true
 explosion.Visible=true
 explosion.setPosition(player.Left-80,player.Top-60)
 explosion.BringToFront()
 if (Score<1000) then trophy.loadImageFromFile(resourcepath..'bronze.png') end
 if (Score>2000) then trophy.loadImageFromFile(resourcepath..'silver.png') end
 if (Score>3500) then trophy.loadImageFromFile(resourcepath..'gold.png') end
 playSound(findTableFile('hit.wav'))
-- timer2.enabled = false
 timer1.enabled = false
 xmplayer_stop()
end

function timer1_Tick(sender)
-- increase the score and show on the label
 Score = Score + 1
 lblDistValue.Caption=' '..Score
-- move/animating the road track down
 roadTrack1.Top=roadTrack1.Top+roadSpeed
 roadTrack2.Top=roadTrack2.Top+roadSpeed
-- if the road track has gone past -630 then set it back
 if (roadTrack1.Top>630) then roadTrack1.Top=-630 end
 if (roadTrack2.Top>630) then roadTrack2.Top=-630 end
-- move the player car by LEFT and RIGHT keys
 if (carLeft) then player.Left = player.Left-carSpeed end
 if (carRight) then player.Left = player.Left+carSpeed end
-- bounce the car off the boundaries of the panel
 if (player.Left<1) then carLeft=false
 elseif (player.Left+player.Width>380) then carRight=false end
-- move the Al cars
 Al1.Top=Al1.Top+trafficSpeed
 Al2.Top=Al2.Top+trafficSpeed
-- respawn the Als and change their images
 if (Al1.Top>panel1.Height) then
 changeAl1()
 Al1.Left=math.random(2,160)  -- appear on left random number
 Al1.Top=math.random(100,200)*-1 end -- appear on top random number
 if (Al2.Top>panel1.Height) then
 changeAl2()
 Al2.Left=math.random(185,327)  -- appear on left random number
 Al2.Top=math.random(100,200)*-1 end -- appear on top random number
-- check collision between player with Al1 or Al2
 if onCollision(player,Al1) or onCollision(player,Al2) then gameOver() end
-- check score and increase difficulty level
 if (Score > 100 and Score <= 500) then
    trafficSpeed=6
    roadSpeed=7
 elseif (Score > 500 and Score <= 1000) then
    trafficSpeed=7
    roadSpeed=8
 elseif (Score > 1000 and Score <= 1200) then
    trafficSpeed=9
    roadSpeed=10 end
end

timer1 = createTimer(frm)
timer1.Interval=10
timer1.Enabled=false
timer1.onTimer=timer1_Tick

function racingSound()
 playSound(findTableFile('racing01.wav'))
end

timer2 = createTimer(frm)
timer2.Interval=5000
timer2.Enabled=false
timer2.onTimer=racingSound

function start()
 xmplayer_stop()
 xmplayer.playXM( findTableFile('race.xm') )
-- playSound(findTableFile('racing01.wav'))
 Reset()
end

function exiter()
 xmplayer_stop()
 timer1.destroy()
 closeCE()
 return caFree
end

frm.show()
xmplayer.playXM(findTableFile('race.xm'))
btnStart.onClick=start
frm.onClose=exiter


Term and Condition
Feel free to edit, use and share.
No hidden code / No encoded script = 100% free

Your Challenge
1. Try to make better GUI with better music/background
2. Try to beat the game with getting Gold Medal (you need to finish the distance as 3500)

Enjoy...



CRDR Racing Game.JPG
 Description:
 Filesize:  43.01 KB
 Viewed:  1437 Time(s)

CRDR Racing Game.JPG



_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Aylin
Master Cheater
Reputation: 7

Joined: 16 Feb 2017
Posts: 393

PostPosted: Tue Jul 02, 2019 1:53 pm    Post subject: Reply with quote

This is unfair, @Corroder. Smile
Your coding skills are better.
I think the next version,
Can be for different games. Wink
Nice job, congratulations, man.



14.jpg
 Description:
 Filesize:  60.21 KB
 Viewed:  1399 Time(s)

14.jpg



_________________
Hi Hitler Make a Youtube Web for Trainer
https://forum.cheatengine.org/viewtopic.php?t=609146
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message MSN Messenger
Corroder
Grandmaster Cheater
Reputation: 27

Joined: 10 Apr 2015
Posts: 987

PostPosted: Tue Jul 02, 2019 7:58 pm    Post subject: Reply with quote

@Aylin..., No no, my coding skill just ordinary and I still learning for coding.
Btw, Bronze medal with 1069 distance is not bad. However, be careful when driving or your driver's licence will be busted... hahaha Very Happy

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Extensions All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites