SlideShare a Scribd company logo
Ring Documentation, Release 1.2
Screen Shot:
45.26 Using the Game Engine - Object and Drawing
We can use the Object keyword (defined by the game engine) to create objects from the GameObject class.
Example:
Load "gameengine.ring" # Give control to the game engine
func main # Called by the Game Engine
oGame = New Game # Create the Game Object
{
title = "My First Game"
Object {
x = 0 y=300 width = 200 height=200
draw = func oGame,oSelf {
oSelf {
for t = 1 to 210
gl_draw_circle(x,y,t,
gl_map_rgb(t*random(255),
t*2,t*3),1)
45.26. Using the Game Engine - Object and Drawing 350
Ring Documentation, Release 1.2
next
}
}
state = func oGame,oSelf {
oSelf {
if x <= 800
x+= 3
else
x=0
ok
}
}
keypress = func oGame,oSelf,nKey {
oSelf {
Switch nKey
on KEY_LEFT
x -= 10
on KEY_RIGHT
x += 10
on KEY_UP
y -= 10
on KEY_DOWN
y += 10
off
}
}
}
} # Start the Events Loop
Screen Shot:
45.26. Using the Game Engine - Object and Drawing 351
Ring Documentation, Release 1.2
Example:
Load "gameengine.ring" # Give control to the game engine
func main # Called by the Game Engine
oGame = New Game # Create the Game Object
{
title = "My First Game"
Object {
x = 400 y=300 width = 200 height=200
draw = func oGame,oSelf {
oSelf {
for t = 1 to 210
gl_draw_rectangle(x+t,y+t,
x+t*2,y+t*2,
gl_map_rgb(t*random(255),
t*2,t*3),1)
gl_draw_rectangle(x+t*2,y+t*2,
x-t*2,y-t*2,
gl_map_rgb(t*random(255),
t*2,t*3),1)
next
}
}
45.26. Using the Game Engine - Object and Drawing 352
Ring Documentation, Release 1.2
keypress = func oGame,oSelf,nKey {
oSelf {
Switch nKey
on KEY_LEFT
x -= 10
on KEY_RIGHT
x += 10
on KEY_UP
y -= 10
on KEY_DOWN
y += 10
off
}
}
}
} # Start the Events Loop
Screen Shot:
45.27 Stars Fighter Game
The Stars Fighter source code
45.27. Stars Fighter Game 353
Ring Documentation, Release 1.2
# The Ring Standard Library
# Game Engine for 2D Games
# 2016, Mahmoud Fayed <msfclipper@yahoo.com>
oGameState = NULL
load "gameengine.ring"
func main
oGame = New Game
while true
oGameState = new GameState
oGame {
title = "Stars Fighter!"
sprite
{
file = "images/menu1.jpg"
x = 0 y=0 width=800 height = 600 scaled = true animate = false
keypress = func ogame,oself,nKey {
if nkey = key_esc or nKey = GE_AC_BACK
ogame.shutdown()
but nKey = key_space
oGameState.startplay=true
ogame.shutdown=true
ok
}
mouse = func ogame,oself,nType,aMouseList {
if nType = GE_MOUSE_UP
oGameState.startplay=true
ogame.shutdown=true
ok
}
}
text {
animate = false
size = 35
file = "fonts/pirulen.ttf"
text = "Stars Fighter"
x = 10 y=50
}
text {
animate = false
size = 25
file = "fonts/pirulen.ttf"
text = "Version 1.0"
x = 80 y=100
}
text {
animate = false
size = 16
file = "fonts/pirulen.ttf"
text = "(C) 2016, Mahmoud Fayed"
x = 45 y=140
}
45.27. Stars Fighter Game 354
Ring Documentation, Release 1.2
text {
animate = false
size = 25
file = "fonts/pirulen.ttf"
text = "Press Space to start"
x = 190 y=470
}
text {
animate = false
size = 20
file = "fonts/pirulen.ttf"
text = "Press Esc to Exit"
x = 260 y=510
}
Sound {
file = "sound/music1.wav"
}
}
if oGameState.startplay
oGame.refresh()
playstart(oGame)
oGame.refresh()
ok
end
func playstart oGame
oSound = New Sound {
file = "sound/music2.wav"
}
while true
play(oGame)
if ogame.shutdown = true and oGameState.value = 0
exit
ok
ogame.refresh()
end
oSound.Delete()
func play oGame
oGame
{
FPS = 60
FixedFPS = 120
title = "Stars Fighter!"
sprite
{
file = "images/stars.jpg"
x = 0
y = 0
point = -370
direction = ge_direction_dec
type = ge_type_background
45.27. Stars Fighter Game 355
Ring Documentation, Release 1.2
state = func ogame,oself {
oself {
if x < -350
direction = ge_direction_inc
point = 370
but x = 0 and direction = ge_direction_inc
direction = ge_direction_dec
point = -370
ok
}
}
}
sprite
{
file = "images/player.png"
transparent = true
type = ge_type_player
x = 400 y =400 width=100 height=100
animate=false move=true Scaled=true
mouse = func ogame,oself,nType,aMouseList {
if not ( aMouseList[GE_MOUSE_X] >= oSelf.x and
aMouseList[GE_MOUSE_X] <= oSelf.x+oSelf.width and
aMouseList[GE_MOUSE_Y] >= oself.y and
aMouseList[GE_MOUSE_Y] <= oSelf.y+oSelf.height )
if nType = GE_MOUSE_DOWN
if aMouseList[1] < oSelf.X # left
oSelf.X -= 100
else
oSelf.X += 100
ok
if aMouseList[2] < oSelf.Y # up
oSelf.Y -= 100
else
oSelf.Y += 100
ok
ok
else
if nType = GE_MOUSE_UP
cFunc = oself.keypress
call cFunc(oGame,oSelf,Key_Space)
ok
ok
}
keypress = func oGame,oself,nkey {
if nkey = key_space
ogame {
sprite {
type = ge_type_fire
file = "images/rocket.png"
transparent = true
x = oself.x + 30
y = oself.y - 30
width = 30
height = 30
point = -30
45.27. Stars Fighter Game 356
Ring Documentation, Release 1.2
nstep = 20
direction = ge_direction_decvertical
state = func oGame,oSelf {
for x in oGame.aObjects
if x.type = ge_type_enemy
if oself.x >= x.x and oself.y >= x.y and
oself.x <= x.x + x.width and
oself.y <= x.y + x.height
showfire(oGame,x.x+40,x.y+40)
ogame.remove(x.nindex)
oGameState.score+=10
oGameState.enemies--
checkwin(oGame)
exit
ok
ok
next
}
}
}
but nkey = key_esc or nKey = GE_AC_BACK ogame.shutdown()
ok
}
state = func oGame,oSelf {
oself {
if x < 0 x = 0 ok
if y < 0 y = 0 ok
if x > ogame.screen_w-width x= ogame.screen_w - width ok
if y > ogame.screen_h-height y=ogame.screen_h-height ok
}
}
}
for g = 1 to oGameState.enemies
sprite
{
type = ge_type_enemy
file = "images/enemy.png"
transparent = true
x = g*random(50) y =g width=100 height=100
animate=true Scaled=true
direction = ge_direction_random
state = func oGame,oSelf {
oself {
if x < 0 x = 0 ok
if y < 0 y = 0 ok
if x > ogame.screen_w-width x= ogame.screen_w - width ok
if y > ogame.screen_h-height y=ogame.screen_h-height ok
}
if random(100) = 1
ogame {
sprite {
type = ge_type_fire
file = "images/rocket2.png"
transparent = true
x = oself.x + 30
y = oself.y + oself.height+ 30
width = 30
height = 30
45.27. Stars Fighter Game 357
Ring Documentation, Release 1.2
point = ogame.screen_h+30
nstep = 10
direction = ge_direction_incvertical
state = func oGame,oSelf {
x = oGame.aObjects[oGameState.playerindex]
if oself.x >= x.x and oself.y >= x.y and
oself.x <= x.x + x.width and
oself.y <= x.y + x.height
if oGameState.value > 0
oGameState.value-=10
ok
ogame.remove(oself.nindex)
checkgameover(oGame)
ok
}
}
}
ok
}
}
next
text {
size = 30
file = "fonts/pirulen.ttf"
text = "Destroy All Enemies!"
nstep = 3
color = GE_COLOR_GREEN
x = 100 y=50
direction = ge_direction_incvertical
point = 500
}
text {
animate = false
point = 400
size = 30
file = "fonts/pirulen.ttf"
text = "Score : " + oGameState.score
x = 500 y=10
state = func oGame,oSelf { oSelf { text = "Score : " + oGameState.score } }
}
text {
animate = false
point = 400
size = 30
file = "fonts/pirulen.ttf"
text = "Energy : " + oGameState.value
x = 500 y=50
state = func oGame,oSelf { oSelf { text = "Energy : " + oGameState.value } }
}
text {
animate = false
point = 400
size = 30
file = "fonts/pirulen.ttf"
text = "Level : " + oGameState.level
x = 500 y=90
}
}
45.27. Stars Fighter Game 358
Ring Documentation, Release 1.2
func checkwin ogame
if oGameState.gameresult return ok
if oGameState.enemies = 0
oGameState.gameresult = true
oGame {
if oGameState.level < 30
text {
point = 400
size = 30
file = "fonts/pirulen.ttf"
text = "Level Completed!"
nStep = 3
x = 500 y=10
state = func ogame,oself {
if oself.y >= 400
ogame.shutdown = true
oGameState.level++
oGameState.enemies = oGameState.level
oGameState.gameresult = false
ok
}
}
else
text {
point = 400
size = 30
nStep = 3
file = "fonts/pirulen.ttf"
text = "You Win !!!"
x = 500 y=10
state = func ogame,oself {
if oself.y >= 400
ogame.shutdown = true
oGameState.value = 0
ok
}
}
ok
}
ok
func checkgameover ogame
if oGameState.gameresult return ok
if oGameState.value <= 0
oGameState.gameresult = true
oGame {
text {
point = 400
size = 30
nStep = 3
file = "fonts/pirulen.ttf"
text = "Game Over !!!"
x = 500 y=10
state = func ogame,oself {
if oself.y >= 400
ogame.shutdown = true
ok
45.27. Stars Fighter Game 359

More Related Content

PDF
The Ring programming language version 1.8 book - Part 57 of 202
PDF
The Ring programming language version 1.5.1 book - Part 49 of 180
PDF
The Ring programming language version 1.5.2 book - Part 50 of 181
PDF
The Ring programming language version 1.6 book - Part 53 of 189
PDF
The Ring programming language version 1.7 book - Part 55 of 196
PDF
The Ring programming language version 1.4.1 book - Part 15 of 31
PDF
The Ring programming language version 1.9 book - Part 61 of 210
PDF
The Ring programming language version 1.5 book - Part 9 of 31
The Ring programming language version 1.8 book - Part 57 of 202
The Ring programming language version 1.5.1 book - Part 49 of 180
The Ring programming language version 1.5.2 book - Part 50 of 181
The Ring programming language version 1.6 book - Part 53 of 189
The Ring programming language version 1.7 book - Part 55 of 196
The Ring programming language version 1.4.1 book - Part 15 of 31
The Ring programming language version 1.9 book - Part 61 of 210
The Ring programming language version 1.5 book - Part 9 of 31

What's hot (20)

PDF
The Ring programming language version 1.5.4 book - Part 51 of 185
PDF
The Ring programming language version 1.3 book - Part 41 of 88
PDF
The Ring programming language version 1.9 book - Part 60 of 210
PDF
The Ring programming language version 1.5.3 book - Part 61 of 184
PDF
The Ring programming language version 1.5.1 book - Part 51 of 180
PDF
The Ring programming language version 1.9 book - Part 62 of 210
PDF
The Ring programming language version 1.3 book - Part 42 of 88
PDF
The Ring programming language version 1.2 book - Part 40 of 84
PDF
The Ring programming language version 1.2 book - Part 39 of 84
PDF
The Ring programming language version 1.10 book - Part 62 of 212
PDF
The Ring programming language version 1.3 book - Part 40 of 88
PDF
The Ring programming language version 1.8 book - Part 59 of 202
PDF
The Ring programming language version 1.5.2 book - Part 51 of 181
PDF
The Ring programming language version 1.7 book - Part 57 of 196
PDF
The Ring programming language version 1.5.2 book - Part 52 of 181
PDF
The Ring programming language version 1.6 book - Part 54 of 189
PDF
The Ring programming language version 1.5.4 book - Part 52 of 185
PDF
The Ring programming language version 1.5.3 book - Part 62 of 184
PDF
The Ring programming language version 1.10 book - Part 61 of 212
PDF
The Ring programming language version 1.4 book - Part 15 of 30
The Ring programming language version 1.5.4 book - Part 51 of 185
The Ring programming language version 1.3 book - Part 41 of 88
The Ring programming language version 1.9 book - Part 60 of 210
The Ring programming language version 1.5.3 book - Part 61 of 184
The Ring programming language version 1.5.1 book - Part 51 of 180
The Ring programming language version 1.9 book - Part 62 of 210
The Ring programming language version 1.3 book - Part 42 of 88
The Ring programming language version 1.2 book - Part 40 of 84
The Ring programming language version 1.2 book - Part 39 of 84
The Ring programming language version 1.10 book - Part 62 of 212
The Ring programming language version 1.3 book - Part 40 of 88
The Ring programming language version 1.8 book - Part 59 of 202
The Ring programming language version 1.5.2 book - Part 51 of 181
The Ring programming language version 1.7 book - Part 57 of 196
The Ring programming language version 1.5.2 book - Part 52 of 181
The Ring programming language version 1.6 book - Part 54 of 189
The Ring programming language version 1.5.4 book - Part 52 of 185
The Ring programming language version 1.5.3 book - Part 62 of 184
The Ring programming language version 1.10 book - Part 61 of 212
The Ring programming language version 1.4 book - Part 15 of 30
Ad

Viewers also liked (20)

PDF
The Ring programming language version 1.2 book - Part 14 of 84
PDF
The Ring programming language version 1.2 book - Part 27 of 84
PDF
The Ring programming language version 1.2 book - Part 13 of 84
PDF
The Ring programming language version 1.2 book - Part 36 of 84
PDF
The Ring programming language version 1.2 book - Part 29 of 84
PDF
The Ring programming language version 1.2 book - Part 32 of 84
PDF
The Ring programming language version 1.2 book - Part 28 of 84
PDF
The Ring programming language version 1.2 book - Part 30 of 84
PDF
The Ring programming language version 1.2 book - Part 35 of 84
PDF
The Ring programming language version 1.2 book - Part 46 of 84
PDF
The Ring programming language version 1.2 book - Part 48 of 84
PDF
The Ring programming language version 1.2 book - Part 31 of 84
PDF
The Ring programming language version 1.2 book - Part 26 of 84
PDF
The Ring programming language version 1.2 book - Part 11 of 84
PDF
The Ring programming language version 1.2 book - Part 12 of 84
PDF
The Ring programming language version 1.2 book - Part 10 of 84
PDF
The Ring programming language version 1.2 book - Part 7 of 84
PDF
The Ring programming language version 1.2 book - Part 9 of 84
PDF
The Ring programming language version 1.2 book - Part 43 of 84
PDF
The Ring programming language version 1.2 book - Part 5 of 84
The Ring programming language version 1.2 book - Part 14 of 84
The Ring programming language version 1.2 book - Part 27 of 84
The Ring programming language version 1.2 book - Part 13 of 84
The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 29 of 84
The Ring programming language version 1.2 book - Part 32 of 84
The Ring programming language version 1.2 book - Part 28 of 84
The Ring programming language version 1.2 book - Part 30 of 84
The Ring programming language version 1.2 book - Part 35 of 84
The Ring programming language version 1.2 book - Part 46 of 84
The Ring programming language version 1.2 book - Part 48 of 84
The Ring programming language version 1.2 book - Part 31 of 84
The Ring programming language version 1.2 book - Part 26 of 84
The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.2 book - Part 12 of 84
The Ring programming language version 1.2 book - Part 10 of 84
The Ring programming language version 1.2 book - Part 7 of 84
The Ring programming language version 1.2 book - Part 9 of 84
The Ring programming language version 1.2 book - Part 43 of 84
The Ring programming language version 1.2 book - Part 5 of 84
Ad

Similar to The Ring programming language version 1.2 book - Part 38 of 84 (20)

PDF
The Ring programming language version 1.5.3 book - Part 50 of 184
PDF
The Ring programming language version 1.5.3 book - Part 60 of 184
PDF
The Ring programming language version 1.5.4 book - Part 50 of 185
PDF
The Ring programming language version 1.2 book - Part 37 of 84
PDF
The Ring programming language version 1.8 book - Part 56 of 202
PDF
The Ring programming language version 1.5.1 book - Part 48 of 180
PDF
The Ring programming language version 1.3 book - Part 39 of 88
PDF
The Ring programming language version 1.7 book - Part 54 of 196
PDF
The Ring programming language version 1.6 book - Part 52 of 189
PDF
The Ring programming language version 1.8 book - Part 58 of 202
PDF
The Ring programming language version 1.5.3 book - Part 49 of 184
PDF
The Ring programming language version 1.5.3 book - Part 59 of 184
PDF
The Ring programming language version 1.5.2 book - Part 49 of 181
PDF
The Ring programming language version 1.5.1 book - Part 50 of 180
PDF
The Ring programming language version 1.5.4 book - Part 49 of 185
PDF
The Ring programming language version 1.10 book - Part 60 of 212
PDF
The Ring programming language version 1.6 book - Part 51 of 189
PDF
The Ring programming language version 1.8 book - Part 55 of 202
PDF
The Ring programming language version 1.9 book - Part 59 of 210
PDF
The Ring programming language version 1.5.2 book - Part 48 of 181
The Ring programming language version 1.5.3 book - Part 50 of 184
The Ring programming language version 1.5.3 book - Part 60 of 184
The Ring programming language version 1.5.4 book - Part 50 of 185
The Ring programming language version 1.2 book - Part 37 of 84
The Ring programming language version 1.8 book - Part 56 of 202
The Ring programming language version 1.5.1 book - Part 48 of 180
The Ring programming language version 1.3 book - Part 39 of 88
The Ring programming language version 1.7 book - Part 54 of 196
The Ring programming language version 1.6 book - Part 52 of 189
The Ring programming language version 1.8 book - Part 58 of 202
The Ring programming language version 1.5.3 book - Part 49 of 184
The Ring programming language version 1.5.3 book - Part 59 of 184
The Ring programming language version 1.5.2 book - Part 49 of 181
The Ring programming language version 1.5.1 book - Part 50 of 180
The Ring programming language version 1.5.4 book - Part 49 of 185
The Ring programming language version 1.10 book - Part 60 of 212
The Ring programming language version 1.6 book - Part 51 of 189
The Ring programming language version 1.8 book - Part 55 of 202
The Ring programming language version 1.9 book - Part 59 of 210
The Ring programming language version 1.5.2 book - Part 48 of 181

More from Mahmoud Samir Fayed (20)

PDF
The Ring programming language version 1.10 book - Part 212 of 212
PDF
The Ring programming language version 1.10 book - Part 211 of 212
PDF
The Ring programming language version 1.10 book - Part 210 of 212
PDF
The Ring programming language version 1.10 book - Part 208 of 212
PDF
The Ring programming language version 1.10 book - Part 207 of 212
PDF
The Ring programming language version 1.10 book - Part 205 of 212
PDF
The Ring programming language version 1.10 book - Part 206 of 212
PDF
The Ring programming language version 1.10 book - Part 204 of 212
PDF
The Ring programming language version 1.10 book - Part 203 of 212
PDF
The Ring programming language version 1.10 book - Part 202 of 212
PDF
The Ring programming language version 1.10 book - Part 201 of 212
PDF
The Ring programming language version 1.10 book - Part 200 of 212
PDF
The Ring programming language version 1.10 book - Part 199 of 212
PDF
The Ring programming language version 1.10 book - Part 198 of 212
PDF
The Ring programming language version 1.10 book - Part 197 of 212
PDF
The Ring programming language version 1.10 book - Part 196 of 212
PDF
The Ring programming language version 1.10 book - Part 195 of 212
PDF
The Ring programming language version 1.10 book - Part 194 of 212
PDF
The Ring programming language version 1.10 book - Part 193 of 212
PDF
The Ring programming language version 1.10 book - Part 192 of 212
The Ring programming language version 1.10 book - Part 212 of 212
The Ring programming language version 1.10 book - Part 211 of 212
The Ring programming language version 1.10 book - Part 210 of 212
The Ring programming language version 1.10 book - Part 208 of 212
The Ring programming language version 1.10 book - Part 207 of 212
The Ring programming language version 1.10 book - Part 205 of 212
The Ring programming language version 1.10 book - Part 206 of 212
The Ring programming language version 1.10 book - Part 204 of 212
The Ring programming language version 1.10 book - Part 203 of 212
The Ring programming language version 1.10 book - Part 202 of 212
The Ring programming language version 1.10 book - Part 201 of 212
The Ring programming language version 1.10 book - Part 200 of 212
The Ring programming language version 1.10 book - Part 199 of 212
The Ring programming language version 1.10 book - Part 198 of 212
The Ring programming language version 1.10 book - Part 197 of 212
The Ring programming language version 1.10 book - Part 196 of 212
The Ring programming language version 1.10 book - Part 195 of 212
The Ring programming language version 1.10 book - Part 194 of 212
The Ring programming language version 1.10 book - Part 193 of 212
The Ring programming language version 1.10 book - Part 192 of 212

Recently uploaded (20)

PDF
Complete Guide to Website Development in Malaysia for SMEs
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Operating system designcfffgfgggggggvggggggggg
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PDF
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PDF
Cost to Outsource Software Development in 2025
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
AutoCAD Professional Crack 2025 With License Key
PPTX
Transform Your Business with a Software ERP System
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Reimagine Home Health with the Power of Agentic AI​
Complete Guide to Website Development in Malaysia for SMEs
Computer Software and OS of computer science of grade 11.pptx
Operating system designcfffgfgggggggvggggggggg
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
CHAPTER 2 - PM Management and IT Context
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
Navsoft: AI-Powered Business Solutions & Custom Software Development
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
17 Powerful Integrations Your Next-Gen MLM Software Needs
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Salesforce Agentforce AI Implementation.pdf
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
Cost to Outsource Software Development in 2025
How to Choose the Right IT Partner for Your Business in Malaysia
AutoCAD Professional Crack 2025 With License Key
Transform Your Business with a Software ERP System
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Reimagine Home Health with the Power of Agentic AI​

The Ring programming language version 1.2 book - Part 38 of 84

  • 1. Ring Documentation, Release 1.2 Screen Shot: 45.26 Using the Game Engine - Object and Drawing We can use the Object keyword (defined by the game engine) to create objects from the GameObject class. Example: Load "gameengine.ring" # Give control to the game engine func main # Called by the Game Engine oGame = New Game # Create the Game Object { title = "My First Game" Object { x = 0 y=300 width = 200 height=200 draw = func oGame,oSelf { oSelf { for t = 1 to 210 gl_draw_circle(x,y,t, gl_map_rgb(t*random(255), t*2,t*3),1) 45.26. Using the Game Engine - Object and Drawing 350
  • 2. Ring Documentation, Release 1.2 next } } state = func oGame,oSelf { oSelf { if x <= 800 x+= 3 else x=0 ok } } keypress = func oGame,oSelf,nKey { oSelf { Switch nKey on KEY_LEFT x -= 10 on KEY_RIGHT x += 10 on KEY_UP y -= 10 on KEY_DOWN y += 10 off } } } } # Start the Events Loop Screen Shot: 45.26. Using the Game Engine - Object and Drawing 351
  • 3. Ring Documentation, Release 1.2 Example: Load "gameengine.ring" # Give control to the game engine func main # Called by the Game Engine oGame = New Game # Create the Game Object { title = "My First Game" Object { x = 400 y=300 width = 200 height=200 draw = func oGame,oSelf { oSelf { for t = 1 to 210 gl_draw_rectangle(x+t,y+t, x+t*2,y+t*2, gl_map_rgb(t*random(255), t*2,t*3),1) gl_draw_rectangle(x+t*2,y+t*2, x-t*2,y-t*2, gl_map_rgb(t*random(255), t*2,t*3),1) next } } 45.26. Using the Game Engine - Object and Drawing 352
  • 4. Ring Documentation, Release 1.2 keypress = func oGame,oSelf,nKey { oSelf { Switch nKey on KEY_LEFT x -= 10 on KEY_RIGHT x += 10 on KEY_UP y -= 10 on KEY_DOWN y += 10 off } } } } # Start the Events Loop Screen Shot: 45.27 Stars Fighter Game The Stars Fighter source code 45.27. Stars Fighter Game 353
  • 5. Ring Documentation, Release 1.2 # The Ring Standard Library # Game Engine for 2D Games # 2016, Mahmoud Fayed <[email protected]> oGameState = NULL load "gameengine.ring" func main oGame = New Game while true oGameState = new GameState oGame { title = "Stars Fighter!" sprite { file = "images/menu1.jpg" x = 0 y=0 width=800 height = 600 scaled = true animate = false keypress = func ogame,oself,nKey { if nkey = key_esc or nKey = GE_AC_BACK ogame.shutdown() but nKey = key_space oGameState.startplay=true ogame.shutdown=true ok } mouse = func ogame,oself,nType,aMouseList { if nType = GE_MOUSE_UP oGameState.startplay=true ogame.shutdown=true ok } } text { animate = false size = 35 file = "fonts/pirulen.ttf" text = "Stars Fighter" x = 10 y=50 } text { animate = false size = 25 file = "fonts/pirulen.ttf" text = "Version 1.0" x = 80 y=100 } text { animate = false size = 16 file = "fonts/pirulen.ttf" text = "(C) 2016, Mahmoud Fayed" x = 45 y=140 } 45.27. Stars Fighter Game 354
  • 6. Ring Documentation, Release 1.2 text { animate = false size = 25 file = "fonts/pirulen.ttf" text = "Press Space to start" x = 190 y=470 } text { animate = false size = 20 file = "fonts/pirulen.ttf" text = "Press Esc to Exit" x = 260 y=510 } Sound { file = "sound/music1.wav" } } if oGameState.startplay oGame.refresh() playstart(oGame) oGame.refresh() ok end func playstart oGame oSound = New Sound { file = "sound/music2.wav" } while true play(oGame) if ogame.shutdown = true and oGameState.value = 0 exit ok ogame.refresh() end oSound.Delete() func play oGame oGame { FPS = 60 FixedFPS = 120 title = "Stars Fighter!" sprite { file = "images/stars.jpg" x = 0 y = 0 point = -370 direction = ge_direction_dec type = ge_type_background 45.27. Stars Fighter Game 355
  • 7. Ring Documentation, Release 1.2 state = func ogame,oself { oself { if x < -350 direction = ge_direction_inc point = 370 but x = 0 and direction = ge_direction_inc direction = ge_direction_dec point = -370 ok } } } sprite { file = "images/player.png" transparent = true type = ge_type_player x = 400 y =400 width=100 height=100 animate=false move=true Scaled=true mouse = func ogame,oself,nType,aMouseList { if not ( aMouseList[GE_MOUSE_X] >= oSelf.x and aMouseList[GE_MOUSE_X] <= oSelf.x+oSelf.width and aMouseList[GE_MOUSE_Y] >= oself.y and aMouseList[GE_MOUSE_Y] <= oSelf.y+oSelf.height ) if nType = GE_MOUSE_DOWN if aMouseList[1] < oSelf.X # left oSelf.X -= 100 else oSelf.X += 100 ok if aMouseList[2] < oSelf.Y # up oSelf.Y -= 100 else oSelf.Y += 100 ok ok else if nType = GE_MOUSE_UP cFunc = oself.keypress call cFunc(oGame,oSelf,Key_Space) ok ok } keypress = func oGame,oself,nkey { if nkey = key_space ogame { sprite { type = ge_type_fire file = "images/rocket.png" transparent = true x = oself.x + 30 y = oself.y - 30 width = 30 height = 30 point = -30 45.27. Stars Fighter Game 356
  • 8. Ring Documentation, Release 1.2 nstep = 20 direction = ge_direction_decvertical state = func oGame,oSelf { for x in oGame.aObjects if x.type = ge_type_enemy if oself.x >= x.x and oself.y >= x.y and oself.x <= x.x + x.width and oself.y <= x.y + x.height showfire(oGame,x.x+40,x.y+40) ogame.remove(x.nindex) oGameState.score+=10 oGameState.enemies-- checkwin(oGame) exit ok ok next } } } but nkey = key_esc or nKey = GE_AC_BACK ogame.shutdown() ok } state = func oGame,oSelf { oself { if x < 0 x = 0 ok if y < 0 y = 0 ok if x > ogame.screen_w-width x= ogame.screen_w - width ok if y > ogame.screen_h-height y=ogame.screen_h-height ok } } } for g = 1 to oGameState.enemies sprite { type = ge_type_enemy file = "images/enemy.png" transparent = true x = g*random(50) y =g width=100 height=100 animate=true Scaled=true direction = ge_direction_random state = func oGame,oSelf { oself { if x < 0 x = 0 ok if y < 0 y = 0 ok if x > ogame.screen_w-width x= ogame.screen_w - width ok if y > ogame.screen_h-height y=ogame.screen_h-height ok } if random(100) = 1 ogame { sprite { type = ge_type_fire file = "images/rocket2.png" transparent = true x = oself.x + 30 y = oself.y + oself.height+ 30 width = 30 height = 30 45.27. Stars Fighter Game 357
  • 9. Ring Documentation, Release 1.2 point = ogame.screen_h+30 nstep = 10 direction = ge_direction_incvertical state = func oGame,oSelf { x = oGame.aObjects[oGameState.playerindex] if oself.x >= x.x and oself.y >= x.y and oself.x <= x.x + x.width and oself.y <= x.y + x.height if oGameState.value > 0 oGameState.value-=10 ok ogame.remove(oself.nindex) checkgameover(oGame) ok } } } ok } } next text { size = 30 file = "fonts/pirulen.ttf" text = "Destroy All Enemies!" nstep = 3 color = GE_COLOR_GREEN x = 100 y=50 direction = ge_direction_incvertical point = 500 } text { animate = false point = 400 size = 30 file = "fonts/pirulen.ttf" text = "Score : " + oGameState.score x = 500 y=10 state = func oGame,oSelf { oSelf { text = "Score : " + oGameState.score } } } text { animate = false point = 400 size = 30 file = "fonts/pirulen.ttf" text = "Energy : " + oGameState.value x = 500 y=50 state = func oGame,oSelf { oSelf { text = "Energy : " + oGameState.value } } } text { animate = false point = 400 size = 30 file = "fonts/pirulen.ttf" text = "Level : " + oGameState.level x = 500 y=90 } } 45.27. Stars Fighter Game 358
  • 10. Ring Documentation, Release 1.2 func checkwin ogame if oGameState.gameresult return ok if oGameState.enemies = 0 oGameState.gameresult = true oGame { if oGameState.level < 30 text { point = 400 size = 30 file = "fonts/pirulen.ttf" text = "Level Completed!" nStep = 3 x = 500 y=10 state = func ogame,oself { if oself.y >= 400 ogame.shutdown = true oGameState.level++ oGameState.enemies = oGameState.level oGameState.gameresult = false ok } } else text { point = 400 size = 30 nStep = 3 file = "fonts/pirulen.ttf" text = "You Win !!!" x = 500 y=10 state = func ogame,oself { if oself.y >= 400 ogame.shutdown = true oGameState.value = 0 ok } } ok } ok func checkgameover ogame if oGameState.gameresult return ok if oGameState.value <= 0 oGameState.gameresult = true oGame { text { point = 400 size = 30 nStep = 3 file = "fonts/pirulen.ttf" text = "Game Over !!!" x = 500 y=10 state = func ogame,oself { if oself.y >= 400 ogame.shutdown = true ok 45.27. Stars Fighter Game 359