Sunteți pe pagina 1din 10

Ring Documentation, Release 1.5.

# Move Down
y += 3
if y > 550 y=550 ok

keypress = func ogame,oself,nKey {


oself {
if nkey = key_space
y -= 55
if y<=0 y=0 ok
ok
}
}

mouse = func ogame,oself,nType,aMouseList {


if nType = GE_MOUSE_UP
cFunc = oself.keypress
call cFunc(oGame,oSelf,Key_Space)
ok
}
}
} # Start the Events Loop

Screen Shot:

52.23. Using the Game Engine - Animate - Events 465


Ring Documentation, Release 1.5.4

52.24 Using the Game Engine - Map


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"

Map {

blockwidth = 80
blockheight = 80

aMap = [
[0,0,0,0,0,0,0,0,0,1,0,0,0,3,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,2,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,2,0,0,0,3,0,0,0,1,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,3,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,3,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],

52.24. Using the Game Engine - Map 466


Ring Documentation, Release 1.5.4

[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]
]

aImages = ["images/fbwall.png",
"images/fbwallup.png",
"images/fbwalldown.png"]

state = func oGame,oSelf {


oSelf {
x -= 3
if x < - 2100 x = 0 ok
}
}

}
} # Start the Events Loop

Screen Shot:

52.24. Using the Game Engine - Map 467


Ring Documentation, Release 1.5.4

52.25 Using the Game Engine - Map Events


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"

Map {

blockwidth = 80
blockheight = 80

aMap = [
[0,0,0,0,0,0,0,0,0,1,0,0,0,3,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,2,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,2,0,0,0,3,0,0,0,1,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,3,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,3,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]
]

aImages = ["images/fbwall.png",
"images/fbwallup.png",
"images/fbwalldown.png"]

state = func oGame,oSelf {


oSelf {
x -= 3
if x < - 2100 x = 0 ok
}
}

mouse = func ogame,oself,nType,aMouseList {


if nType = GE_MOUSE_UP
oSelf {
mX = aMouseList[GE_MOUSE_X]
mY = aMouseList[GE_MOUSE_Y]
nValue = GetValue(mX,mY)
nRow = GetRow(mX,mY)
nCol = GetCol(mX,mY)
Switch nValue
On 1 aMap[nRow][nCol] = 0
On 2 aMap[nRow][nCol] = 0
On 3 aMap[nRow][nCol] = 0
On 0 aMap[nRow][nCol] = 1
Off
}
ok
}

}
} # Start the Events Loop

52.25. Using the Game Engine - Map Events 468


Ring Documentation, Release 1.5.4

Screen Shot:

52.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)

52.26. Using the Game Engine - Object and Drawing 469


Ring Documentation, Release 1.5.4

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:

52.26. Using the Game Engine - Object and Drawing 470


Ring Documentation, Release 1.5.4

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
}
}

52.26. Using the Game Engine - Object and Drawing 471


Ring Documentation, Release 1.5.4

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:

52.27 Stars Fighter Game

The Stars Fighter source code

52.27. Stars Fighter Game 472


Ring Documentation, Release 1.5.4

# 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
}

52.27. Stars Fighter Game 473


Ring Documentation, Release 1.5.4

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

52.27. Stars Fighter Game 474

S-ar putea să vă placă și