Jump to content
  • entries
    2
  • comments
    4
  • views
    4,456

[BallGame] - Easy game development with LeadWerks


GlshchnkLx

3,866 views

 Share

Hello everybody. Today I probably will start the first lesson dedicated to developing games with LeadWerks Game Engine 4.1.

 

Table of contents:

  • Introduction
  • Requirements
  • Project preparation
  • Setting up the project
  • Level creation (Prototype)
  • Ball Script

Introduction

 

This lesson is to create a game where the main character is a ball (or other primitive). By the way, there is such a lesson ( "Marble Game") in the official documentation.

 

Requirements

 

Before we begin, I would say the main requirements for the correct execution of the lesson:

  • You must dial the code themselves. This will allow you to understand the basic design principles.
  • Carefully read the text of the lesson and follow all the steps. It is very important.
  • Treat with understanding the author. I am a simple man, and I can make mistakes.

Well. Now you can start a lesson.

 

Project preparation

 

Start LeadWerks 4.1 and open the "Project Manager" (how?)

 

 

 

YbhQozgjjzw.jpg

 

 

 

For the tutorial, I'll use an empty project ( "Blank Project"). The name you can choose any, I chose "BallGame".

 

 

 

RxY7WhuI6kk.jpg

 

 

 

After the final creation, we make some modifications to the workspace. We need to add some starter content. For the tutorial, I'll take it from a template "Advanced First-Person Shooter". We need a folder with materials.

 

Open this folder ({Steam Folder}/steamapps/common/Leadwerks/Templates/Advanced First-Person Shooter/) (If you change the directory LW then open it)

 

And copy the folder "Materials" folder in your workspace.

 

Well done if you have completed all the stages of preparation. It remains only to make a game )))

 

Setting up the project

 

Open your workspace (project) in LW.

 

 

AV6RCQvMk3g.jpg

 

 

Now we'll do a few simple actions to configure our project. We will make a simple splash screen and other simple decorations.

 

Open the file "Main.lua" ( "Scripts / Main.lua") in the script editor (how?)

 

 

 

s5e6shB6TEE.jpg

 

00SddZWnrOg.jpg

 

 

 

Now we have to modify the "Main.lua". I will not talk about work with Lua principles in LW (You can read about this in the Tutorial). I'll just write code and specify where to insert it (I'll refer to a reading material about this)

 

All functions that are available to us, you can find in the API.

 

To implement a simple splash screen, we need the following themes:

A lot of material had accumulated. But not everything is so terrible.

 

Will definitely start writing screen)

 

Go to the 20th line of the code. It is especially this place? It's simple. All code up to this initialisere LW, and already runs code after the first level and run all in normal mode.

 

Now I will write all of the code that you have to understand yourself to write, and later I will comment on it.

 


--simple splash screen (only example)
tempFont = context:GetFont() --Current Font/ We save for restore after display
local font = Font:Load("Fonts/Arial.ttf",36) --Font for display
context:SetFont(font)
font:Release()

local X = window:GetWidth() * 0.45 --the definition of the center of Width
local Y = window:GetHeight() *0.5 --the definition of the center of Height(
local A = 0 --Alpha
local AStep = 0.005 --increment step
local TimeScreen = 2.5 --Time in sec for display
TimeScreen = TimeScreen * 1000
context:SetBlendMode(Blend.Alpha)
context:SetColor(1,1,1,0) --Start Color
while window:KeyDown(Key.Space)==false do --Space for skip
Time:Step()
context:DrawText(title .." with Leadwerks",X,Y) --Text
context:SetColor(A,A,A,1)
Time:Delay(TimeScreen / (1 / AStep))
A = A + AStep
context:Sync(false) --Out on the screen

if (A >= 1) then
break
end
end
context:SetColor(1,1,1,1)
context:SetScale(1,1)
context:SetFont(tempFont)
tempFont:Release()

 

 

 

mfhtmNiW6vM.jpg

 

 

 

The result is bad. But this is only a simple example. At desire it is possible to complicate the code and will be all good)

 

 

NtQcGafDVi0.jpg

 

 

Now let's do the automatic detection of screen resolution (run full screen)

 

Go to line 10. There will be a similar piece of code

 

window=Window:Create(title,0,0,System:GetProperty("screenwidth","1024"),System:GetProperty("screenheight","768"),windowstyle)

 

The values of the parameters screenwidth and screenheight define the starting desktop resolution. We can ask the system to know the current desktop resolution.

 

insert before line 10 the following code:

 

screen = System:GetGraphicsMode(System:CountGraphicsModes()-1)
screenwidth = screen.x
screenheight = screen.y

 

Now modify the line

 

window=Window:Create(title,0,0,System:GetProperty("screenwidth","1024"),System:GetProperty("screenheight","768"),windowstyle)

 

in the following line

 

window=Window:Create(title,0,0,System:GetProperty("screenwidth",screenwidth),System:GetProperty("screenheight",screenheight),windowstyle)

 

And 9 line write "System:GetProperty("fullscreen",1)" instead of "System:GetProperty("fullscreen")"

 

 

 

ZfFDbNfpZa8.jpg

 

 

 

I know that experienced developers will tell you what to do, but we're just learning. In normal projects you can't do that)

 

How much we have done. But even more lies ahead.

 

Level creation (Prototype)

 

In this part of the lesson, I'm not going to tell you anything. A lot of articles on this subject is on the Internet. I'll give a couple of links and it will show a prototype)

 

The appropriate lessons LeadWerks you can find here

 

To build a level, I will use primitives. Character is a ball, the levels are composed of platforms.

 

Here's what I got (Show me what you have left)

 

 

 

G44MbSpDAYg.jpgPUw1PtuIi7U.jpgCvxHkV8AooM.jpg

-aDmO9CRrts.jpgqq-BiFXQrJU.jpg

 

 

 

Are you pleased with the result? If not, then understand that you do not like your level and correct.

 

Level ready. Make the character (ball)

 

Ball Script

 

Read the tutorial

 

Now you have an idea how to do it. And I'll show you my implementation.

 

Create a script of the player (Similar to that of a tutorial) ("Scripts/Player/BallPlayer.lua")

 

 

 

zjMINKevuLc.jpg

 

 

 

It will be the main file for us. Through him we will make the control of the character/ camera / other.

 

I will do as always. Write all the code and tell you how it works. So I'll give links to topics which will include (But will not give references to the functions themselves. You have to find yourself)

 

--Script Settings
Script.Mass = 10 --int (MassPlayer)
Script.Speed = 25.0 --float (Speed Mul)
Script.EnergyToJump = 1500.0 --float (It is for jump)
function Script:Start()
self.Force = self.Mass * self.Speed --Created Force Multiple
self.Direction = Vec3(0,0,0) --Helpfull (Determines where we are going)
self.entity:SetMass(self.Mass) --Created Mass
--Camera
self.camera = Camera:Create()
self.camera:SetPosition(self.entity:GetPosition())
self.camera:SetRotation(45, 0, 0)
self.camera:Move(0,0, -4)
--Light
self.light = PointLight:Create()
self.light:SetPosition(self.entity:GetPosition())
self.light:SetRange(2)
--For Jump
self.ETJ = self.EnergyToJump
end
function Script:UpdateWorld()
--The cursor displacement (rotation camera)
self.offsetx = window:GetMousePosition().x - window:GetWidth()/2
self.offsety = window:GetMousePosition().y - window:GetHeight()/2
--Camera Rotation with offset
self.camera:SetRotation(self.offsety/10 + self.camera:GetRotation().x, self.offsetx/10 + self.camera:GetRotation().y, 0, false)
--Generate points for a jump
if self.ETJ < self.EnergyToJump then
self.ETJ = self.ETJ + self.EnergyToJump * 0.005
end
--Camera
self.camera:SetPosition(self.entity:GetPosition())
self.camera:Move(0,0, window:GetMousePosition().z - 4)
--Light
self.light:SetPosition(self.entity:GetPosition())
--Direction for move
self.Direction.x = -Math:Sin(self.camera:GetRotation(true).y)
self.Direction.z = Math:Cos(self.camera:GetRotation(true).y)
--Move
if window:KeyDown(Key.W) then self.entity:AddForce(self.Direction * self.Force,true) end
if window:KeyDown(Key.A) then self.entity:AddForce(Vec3(self.Direction.z, 0, self.Direction.x) * self.Force * -1 ,true) end
if window:KeyDown(Key.D) then self.entity:AddForce(Vec3(self.Direction.z, 0, self.Direction.x) * self.Force,true) end
if window:KeyDown(Key.S) then self.entity:AddForce(self.Direction * self.Force * -1 ,true) end
--Jump
if (self.ETJ - 100) > 0 then
if window:KeyDown(Key.Space) then
self.ETJ = self.ETJ - 100
self.entity:AddForce(Vec3(0, self.Mass, 0) * self.Force * 0.7 ,true)
end
end
window:SetMousePosition(window:GetWidth()/2, window:GetHeight()/2, 0)
end

function Script:PostRender(context)
context:SetBlendMode(Blend.Alpha)
context:SetColor(1,1,1,1)
context:DrawText("Energy to jump" .. self.ETJ,2,2)
end

 

Apply this script to the ball physics and play.

 

The lesson was over. You made a simple game. Now you can finish the leveling/ points/ achievements to sell on Steam.

 

Here are screenshots of my result:

 

 

 

FYIBYCMj79w.jpgbrzALHVWpyg.jpgzdpb_68YKJQ.jpgWaxenaWywHo.jpg

 

 

 

I hope you will find useful this tutorial. In the future, I can tell you other things)

  • Upvote 6
 Share

2 Comments


Recommended Comments

Thank you. Now, I haven't yet looked at the tutorial but I think any effort to build teaching materials should be applauded.

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...