Ring Documentation, Release 1.3
sdl_fillrect(surface,nullpointer(),0)
if sdl_get_sdl_event_motion_xrel(myevent) < 0
cMsg += " Left "
else
cMsg += " Right "
ok
if sdl_get_sdl_event_motion_yrel(myevent) < 0
cMsg += " Up "
else
cMsg += " Down "
ok
cMsg += " x = " + sdl_get_sdl_event_motion_x(myevent)
cMsg += " y = " + sdl_get_sdl_event_motion_y(myevent)
showmsg(cMsg)
off
end
SDL_Destroy_SDL_Color(Color)
TTF_CloseFont(font)
SDL_DestroyWindow(win)
SDL_Quit()
func showmsg mymsg
text = TTF_RenderText_Solid(font,mymsg,color)
SDL_BlitSurface(text, nullpointer(), surface, nullpointer())
SDL_UpdateWindowSurface(win)
SDL_FreeSurface(text)
46.10 Play Sound
Example:
Load "libsdl.ring"
SDL_Init(SDL_INIT_EVERYTHING)
win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN)
Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT , 2, 10000)
Mix_AllocateChannels(4)
sound = Mix_LoadWav( "sound.wav" )
Mix_VolumeChunk(sound,1)
Mix_PlayChannel(1,sound,0)
myevent = sdl_new_sdl_event()
while true
thevent = sdl_pollevent(myevent)
switch sdl_get_sdl_event_type(myevent)
on sdl_get_sdl_quit()
exit
on sdl_get_sdl_keydown()
Key = SDL_GET_SDL_Event_key_keysym_sym(myevent)
if key = 27 exit ok
off
end
Mix_FreeChunk( sound )
Mix_CloseAudio()
46.10. Play Sound 348
Ring Documentation, Release 1.3
Mix_Quit()
SDL_DestroyWindow(win)
SDL_Quit()
46.10. Play Sound 349
CHAPTER
FORTYSEVEN
DEMO PROJECT - GAME ENGINE FOR 2D GAMES
In this chapter we will learn about using the different programming paradigms in the same project.
We will create a simple Game Engine for 2D Games.
You can use the Engine directly to create 2D Games for Desktop or Mobile.
47.1 Project Layers
The project contains the next layers
• Games Layer (Here we will use declarative programming)
• Game Engine Classes (Here we will use the Object-Oriented Programming paradigm)
• Interface to graphics library (Here we will use procedural programming)
• Graphics Library bindings (Here we have RingAllegro and RingLibSDL)
47.2 Graphics Library bindings
We already have RingAllegro to use the Allegro game programming library and we have RingLibSDL to use the
LibSDL game programming library.
Both of RingAllegro and RingLibSDL are created using the C language with the help of the Ring code generator for
extensions.
Each of them is over 10,000 lines of C code which is generated after writing simple configuration files (That are
processed by the code generator).
Each configuration file determines the functions names, structures information and constants then the generator process
this configuration file to produce the C code and the library that can be loaded from Ring code.
Using RingAllegro and RingLibSDL is very similar to using Allegro and LibSDL from C code where you have the
same functions but we can build on that using the Ring language features
• RingAllegro Source Code : https://github.com/ring-lang/ring/tree/master/extensions/ringallegro
• RingLibSDL Source Code : https://github.com/ring-lang/ring/tree/master/extensions/ringsdl
350
Ring Documentation, Release 1.3
47.3 Interface to graphics library
In this layer we have gl_allegro.ring and gl_libsdl.ring
Each library provides the same functions to be used with interacting with the Graphics Library.
This layer hides the details and the difference between RingAllegro and RingLibSDL.
You have the same functions, Just use it and you can switch between Allegro and LibSDL at anytime.
Why ?
Allegro is very simple, we can use it to quickly create 2D games for Windows, Linux and MacOS X.
In Ring 1.0 we started by supporting Allegro.
Also LibSDL is very powerful and popular, very easy to use for Mobile Development.
Ring 1.1 comes with support for LibSDL so we can quickly create games for Mobile.
Note: We can use just one library for Desktop and Mobile development.
• gl_allegro.ring source code : https://github.com/ring-lang/ring/blob/master/ringlibs/gameengine/gl_allegro.ring
• gl_libsdl.ring source code : https://github.com/ring-lang/ring/blob/master/ringlibs/gameengine/gl_libsdl.ring
47.4 Game Engine Classes
The Engine comes with the next classes
• GameBase class
• Resources class
• Game class
• GameObject class
• Sprite class
• Text class
• Animate class
• Sound class
• Map class
• Source Code : https://github.com/ring-lang/ring/blob/master/ringlibs/gameengine/gameengine.ring
47.5 Games Layer
In this layer we create our games using the Game Engine classes
The classes are designed to be used through Declarative Programming.
In our games we will use the next classes
• Game class
• Sprite class
47.3. Interface to graphics library 351
Ring Documentation, Release 1.3
• Text class
• Animate class
• Sound class
• Map class
Note: Other classes in the engine are for internal use by the engine.
We will introduce some examples and three simple games :-
• Stars Fighter Game
• Flappy Bird 3000 Game
• Super Man 2016 Game
47.6 Game Class
The next table present the class attributes.
Attributes Description
FPS Number determines how many times the draw() method will be called per second.
FixedFPS Number determines how many times the animate() method will be called per second.
Title String determines the window title of the game.
aObjects List contains all objects in the game
shutdown True/False value to end the game loop
The next table present the class methods.
Method Description
refresh() Delete objects.
settitle(cTitle) Set the window title using a string parameter.
shutdown() Close the application.
The next table present a group of keywords defined by the class.
Keyword Description
sprite Create new Sprite object and add it to the game objects.
text Create new Text object and add it to the game objects.
animate Create new Animate object and add it to the game objects.
sound Create new Sound object and add it to the game objects.
map Create new Map object and add it ot the game objects.
47.7 GameObject Class
The next table present the class attributes.
47.6. Game Class 352
Ring Documentation, Release 1.3
Attributes Description
enabled True/False determine the state of the object (Active/Not Active)
x Number determine the x position of the object.
y Number determine the y position of the object.
width Number determine the width of the object.
height Number determine the height of the object.
nIndex Number determine the index of the object in objects list.
animate True/False to animate the object or not.
move True/False to move the object using the keyboard or not.
Scaled True/False to scale the object image or not.
draw Function to be called when drawing the object.
state Function to be called for object animation.
keypress Function to be called when a key is pressed.
mouse Function to be called when a mouse event happens.
The next table present the class methods.
Method Description
keyboard(oGame,nkey) Check Keyboard Events
mouse(oGame,nType,aMouseList) Check Mouse Events
rgb(r,g,b) Return new color using the RGB (Red, Green and Blue) Values.
47.8 Sprite Class
Parent Class : GameObject Class
The next table present the class attributes.
Attributes Description
image String determine the image file name.
point Number determine the limit of automatic movement of the object.
direction Number determine the direction of movement.
nstep Number determine the increment/decrement during movement.
type Number determine the object type in the game (Optional).
transparent True/False value determine if the image is transparent.
The next table present the class methods.
Method Description
Draw(oGame) Draw the object
47.9 Text Class
Parent Class : Sprite Class
The next table present the class attributes.
Attributes Description
size Number determine the font size
font String determine the font file name
text String determine the text to be displayed
color Number determine the color
The next table present the class methods.
47.8. Sprite Class 353
Ring Documentation, Release 1.3
Method Description
Draw(oGame) Draw the object
47.10 Animate Class
Parent Class : Sprite Class
The next table present the class attributes.
Attributes Description
frames Number determine the number of frames
frame Number determine the active frame
framewidth Number determine the frame width.
animate True/False determine using animate or not.
scaled True/False determine scaling image or not.
The next table present the class methods.
Method Description
Draw(oGame) Draw the object
47.11 Sound Class
Parent Class : GameObject Class
The next table present the class attributes.
Attributes Description
file String determine the sound file name.
once True/False determine to play the file one time or not (loop).
The next table present the class methods.
Method Description
playsound() Play the sound file
47.12 Map Class
Parent Class : Sprite Class
The next table present the class attributes.
Attributes Description
aMap List determine the map content using numbers.
aImages List determine the image used for each number in the map.
BlockWidth Number determine the block width (default = 32).
BlockHeight Number determine the block height (default = 32).
Animate True/False determine the animation status.
The next table present the class methods.
Method Description
getvalue(x,y) Return the item value in the Map according to the visible part
47.10. Animate Class 354
Ring Documentation, Release 1.3
47.13 Using the Game Engine - Creating the Game Window
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"
} # Start the Events Loop
Note: if you want to define global variables, this must be before load “gameengine.ring” because this instruction will
give the control to the game engine.
Screen Shot:
47.14 Using the Game Engine - Drawing Text
Load "gameengine.ring" # Give Control to the Game Engine
func main # Called by the Game Engine
47.13. Using the Game Engine - Creating the Game Window 355
Ring Documentation, Release 1.3
oGame = New Game # Create the Game Object
{
title = "My First Game"
text {
x = 10 y=50
animate = false
size = 20
file = "fonts/pirulen.ttf"
text = "game development using ring is very fun!"
color = rgb(0,0,0)
}
} # Start the Events Loop
Screen Shot:
47.15 Using the Game Engine - Moving Text
Load "gameengine.ring" # Give Control to the Game Engine
func main # Called by the Game Engine
oGame = New Game # Create the Game Object
47.15. Using the Game Engine - Moving Text 356
Ring Documentation, Release 1.3
{
title = "My First Game"
text {
x = 10 y=50
animate = false
size = 20
file = "fonts/pirulen.ttf"
text = "game development using ring is very fun!"
color = rgb(0,0,0) # Color = black
}
text {
x = 10 y=150
# Animation Part =====================================
animate = true # Use Animation
direction = GE_DIRECTION_INCVERTICAL # Increase y
point = 400 # Continue until y=400
nStep = 3 # Each time y+= 3
#=====================================================
size = 20
file = "fonts/pirulen.ttf"
text = "welcome to the real world!"
color = rgb(0,0,255) # Color = Blue
}
} # Start the Events Loop
Screen Shot:
47.15. Using the Game Engine - Moving Text 357

More Related Content

PDF
The Ring programming language version 1.5.3 book - Part 48 of 184
PDF
The Ring programming language version 1.2 book - Part 36 of 84
PDF
The Ring programming language version 1.4 book - Part 14 of 30
PDF
The Ring programming language version 1.9 book - Part 58 of 210
PPTX
Intro to Python (High School) Unit #3
PDF
The Ring programming language version 1.7 book - Part 53 of 196
PDF
Make a match3
The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.9 book - Part 58 of 210
Intro to Python (High School) Unit #3
The Ring programming language version 1.7 book - Part 53 of 196
Make a match3

What's hot (19)

PDF
Lec 9 05_sept [compatibility mode]
PDF
Creating Games for Asha - platform
PPTX
WP7 HUB_XNA
PDF
Intro to programming games with clojure
KEY
Artdm170 week12 user_interaction
PDF
C++ Windows Forms L08 - GDI P1
PPTX
Maximizing performance of 3 d user generated assets in unity
PDF
libGDX: Simple Frame Animation
PDF
Peyton jones-2009-fun with-type_functions-slide
PDF
Peyton jones-2011-type classes
PDF
The Ring programming language version 1.2 book - Part 35 of 84
PPTX
WP7 HUB_XNA overview
PDF
libGDX: User Input and Frame by Frame Animation
PPTX
Procedural Content Generation with Clojure
PPTX
Enter The Matrix
PDF
Clojure for Data Science
PDF
maXbox starter68 machine learning VI
DOC
Learn Java 3D
PDF
Extreme dxt compression
Lec 9 05_sept [compatibility mode]
Creating Games for Asha - platform
WP7 HUB_XNA
Intro to programming games with clojure
Artdm170 week12 user_interaction
C++ Windows Forms L08 - GDI P1
Maximizing performance of 3 d user generated assets in unity
libGDX: Simple Frame Animation
Peyton jones-2009-fun with-type_functions-slide
Peyton jones-2011-type classes
The Ring programming language version 1.2 book - Part 35 of 84
WP7 HUB_XNA overview
libGDX: User Input and Frame by Frame Animation
Procedural Content Generation with Clojure
Enter The Matrix
Clojure for Data Science
maXbox starter68 machine learning VI
Learn Java 3D
Extreme dxt compression

Similar to The Ring programming language version 1.3 book - Part 38 of 88 (20)

PDF
The Ring programming language version 1.5.3 book - Part 58 of 184
PDF
The Ring programming language version 1.5.4 book - Part 48 of 185
PDF
The Ring programming language version 1.8 book - Part 55 of 202
PDF
The Ring programming language version 1.5.1 book - Part 47 of 180
PDF
The Ring programming language version 1.5.2 book - Part 47 of 181
PDF
The Ring programming language version 1.6 book - Part 51 of 189
PDF
The Ring programming language version 1.6 book - Part 50 of 189
PDF
The Ring programming language version 1.5.2 book - Part 48 of 181
PDF
The Ring programming language version 1.5.4 book - Part 49 of 185
PDF
The Ring programming language version 1.3 book - Part 39 of 88
PDF
The Ring programming language version 1.9 book - Part 59 of 210
PDF
The Ring programming language version 1.5.3 book - Part 59 of 184
PDF
The Ring programming language version 1.5.3 book - Part 49 of 184
PDF
The Ring programming language version 1.10 book - Part 60 of 212
PDF
The Ring programming language version 1.5.1 book - Part 46 of 180
PDF
The Ring programming language version 1.4.1 book - Part 14 of 31
PDF
The Ring programming language version 1.5.3 book - Part 57 of 184
PDF
The Ring programming language version 1.5.3 book - Part 47 of 184
PDF
The Ring programming language version 1.5.4 book - Part 50 of 185
PDF
The Ring programming language version 1.6 book - Part 52 of 189
The Ring programming language version 1.5.3 book - Part 58 of 184
The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.8 book - Part 55 of 202
The Ring programming language version 1.5.1 book - Part 47 of 180
The Ring programming language version 1.5.2 book - Part 47 of 181
The Ring programming language version 1.6 book - Part 51 of 189
The Ring programming language version 1.6 book - Part 50 of 189
The Ring programming language version 1.5.2 book - Part 48 of 181
The Ring programming language version 1.5.4 book - Part 49 of 185
The Ring programming language version 1.3 book - Part 39 of 88
The Ring programming language version 1.9 book - Part 59 of 210
The Ring programming language version 1.5.3 book - Part 59 of 184
The Ring programming language version 1.5.3 book - Part 49 of 184
The Ring programming language version 1.10 book - Part 60 of 212
The Ring programming language version 1.5.1 book - Part 46 of 180
The Ring programming language version 1.4.1 book - Part 14 of 31
The Ring programming language version 1.5.3 book - Part 57 of 184
The Ring programming language version 1.5.3 book - Part 47 of 184
The Ring programming language version 1.5.4 book - Part 50 of 185
The Ring programming language version 1.6 book - Part 52 of 189

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
1_Keynote_Breaking Barriers_한계를 넘어서_Charith Mendis.pdf
PPTX
Presentation - Principles of Instructional Design.pptx
PDF
substrate PowerPoint Presentation basic one
PDF
The Digital Engine Room: Unlocking APAC’s Economic and Digital Potential thro...
PPTX
Strategic Picks — Prioritising the Right Agentic Use Cases [2/6]
PDF
Uncertainty-aware contextual multi-armed bandits for recommendations in e-com...
PDF
Be ready for tomorrow’s needs with a longer-lasting, higher-performing PC
PDF
Decision Optimization - From Theory to Practice
PDF
Secure Java Applications against Quantum Threats
PPTX
How to use fields_get method in Odoo 18
PDF
Peak of Data & AI Encore: Scalable Design & Infrastructure
PDF
GDG Cloud Southlake #45: Patrick Debois: The Impact of GenAI on Development a...
PDF
Human Computer Interaction Miterm Lesson
PDF
TrustArc Webinar - Data Minimization in Practice_ Reducing Risk, Enhancing Co...
PPTX
Information-Technology-in-Human-Society.pptx
PDF
Examining Bias in AI Generated News Content.pdf
PDF
TicketRoot: Event Tech Solutions Deck 2025
PDF
Child-friendly e-learning for artificial intelligence education in Indonesia:...
PDF
Revolutionizing recommendations a survey: a comprehensive exploration of mode...
PDF
Applying Agentic AI in Enterprise Automation
1_Keynote_Breaking Barriers_한계를 넘어서_Charith Mendis.pdf
Presentation - Principles of Instructional Design.pptx
substrate PowerPoint Presentation basic one
The Digital Engine Room: Unlocking APAC’s Economic and Digital Potential thro...
Strategic Picks — Prioritising the Right Agentic Use Cases [2/6]
Uncertainty-aware contextual multi-armed bandits for recommendations in e-com...
Be ready for tomorrow’s needs with a longer-lasting, higher-performing PC
Decision Optimization - From Theory to Practice
Secure Java Applications against Quantum Threats
How to use fields_get method in Odoo 18
Peak of Data & AI Encore: Scalable Design & Infrastructure
GDG Cloud Southlake #45: Patrick Debois: The Impact of GenAI on Development a...
Human Computer Interaction Miterm Lesson
TrustArc Webinar - Data Minimization in Practice_ Reducing Risk, Enhancing Co...
Information-Technology-in-Human-Society.pptx
Examining Bias in AI Generated News Content.pdf
TicketRoot: Event Tech Solutions Deck 2025
Child-friendly e-learning for artificial intelligence education in Indonesia:...
Revolutionizing recommendations a survey: a comprehensive exploration of mode...
Applying Agentic AI in Enterprise Automation

The Ring programming language version 1.3 book - Part 38 of 88

  • 1. Ring Documentation, Release 1.3 sdl_fillrect(surface,nullpointer(),0) if sdl_get_sdl_event_motion_xrel(myevent) < 0 cMsg += " Left " else cMsg += " Right " ok if sdl_get_sdl_event_motion_yrel(myevent) < 0 cMsg += " Up " else cMsg += " Down " ok cMsg += " x = " + sdl_get_sdl_event_motion_x(myevent) cMsg += " y = " + sdl_get_sdl_event_motion_y(myevent) showmsg(cMsg) off end SDL_Destroy_SDL_Color(Color) TTF_CloseFont(font) SDL_DestroyWindow(win) SDL_Quit() func showmsg mymsg text = TTF_RenderText_Solid(font,mymsg,color) SDL_BlitSurface(text, nullpointer(), surface, nullpointer()) SDL_UpdateWindowSurface(win) SDL_FreeSurface(text) 46.10 Play Sound Example: Load "libsdl.ring" SDL_Init(SDL_INIT_EVERYTHING) win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN) Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT , 2, 10000) Mix_AllocateChannels(4) sound = Mix_LoadWav( "sound.wav" ) Mix_VolumeChunk(sound,1) Mix_PlayChannel(1,sound,0) myevent = sdl_new_sdl_event() while true thevent = sdl_pollevent(myevent) switch sdl_get_sdl_event_type(myevent) on sdl_get_sdl_quit() exit on sdl_get_sdl_keydown() Key = SDL_GET_SDL_Event_key_keysym_sym(myevent) if key = 27 exit ok off end Mix_FreeChunk( sound ) Mix_CloseAudio() 46.10. Play Sound 348
  • 2. Ring Documentation, Release 1.3 Mix_Quit() SDL_DestroyWindow(win) SDL_Quit() 46.10. Play Sound 349
  • 3. CHAPTER FORTYSEVEN DEMO PROJECT - GAME ENGINE FOR 2D GAMES In this chapter we will learn about using the different programming paradigms in the same project. We will create a simple Game Engine for 2D Games. You can use the Engine directly to create 2D Games for Desktop or Mobile. 47.1 Project Layers The project contains the next layers • Games Layer (Here we will use declarative programming) • Game Engine Classes (Here we will use the Object-Oriented Programming paradigm) • Interface to graphics library (Here we will use procedural programming) • Graphics Library bindings (Here we have RingAllegro and RingLibSDL) 47.2 Graphics Library bindings We already have RingAllegro to use the Allegro game programming library and we have RingLibSDL to use the LibSDL game programming library. Both of RingAllegro and RingLibSDL are created using the C language with the help of the Ring code generator for extensions. Each of them is over 10,000 lines of C code which is generated after writing simple configuration files (That are processed by the code generator). Each configuration file determines the functions names, structures information and constants then the generator process this configuration file to produce the C code and the library that can be loaded from Ring code. Using RingAllegro and RingLibSDL is very similar to using Allegro and LibSDL from C code where you have the same functions but we can build on that using the Ring language features • RingAllegro Source Code : https://github.com/ring-lang/ring/tree/master/extensions/ringallegro • RingLibSDL Source Code : https://github.com/ring-lang/ring/tree/master/extensions/ringsdl 350
  • 4. Ring Documentation, Release 1.3 47.3 Interface to graphics library In this layer we have gl_allegro.ring and gl_libsdl.ring Each library provides the same functions to be used with interacting with the Graphics Library. This layer hides the details and the difference between RingAllegro and RingLibSDL. You have the same functions, Just use it and you can switch between Allegro and LibSDL at anytime. Why ? Allegro is very simple, we can use it to quickly create 2D games for Windows, Linux and MacOS X. In Ring 1.0 we started by supporting Allegro. Also LibSDL is very powerful and popular, very easy to use for Mobile Development. Ring 1.1 comes with support for LibSDL so we can quickly create games for Mobile. Note: We can use just one library for Desktop and Mobile development. • gl_allegro.ring source code : https://github.com/ring-lang/ring/blob/master/ringlibs/gameengine/gl_allegro.ring • gl_libsdl.ring source code : https://github.com/ring-lang/ring/blob/master/ringlibs/gameengine/gl_libsdl.ring 47.4 Game Engine Classes The Engine comes with the next classes • GameBase class • Resources class • Game class • GameObject class • Sprite class • Text class • Animate class • Sound class • Map class • Source Code : https://github.com/ring-lang/ring/blob/master/ringlibs/gameengine/gameengine.ring 47.5 Games Layer In this layer we create our games using the Game Engine classes The classes are designed to be used through Declarative Programming. In our games we will use the next classes • Game class • Sprite class 47.3. Interface to graphics library 351
  • 5. Ring Documentation, Release 1.3 • Text class • Animate class • Sound class • Map class Note: Other classes in the engine are for internal use by the engine. We will introduce some examples and three simple games :- • Stars Fighter Game • Flappy Bird 3000 Game • Super Man 2016 Game 47.6 Game Class The next table present the class attributes. Attributes Description FPS Number determines how many times the draw() method will be called per second. FixedFPS Number determines how many times the animate() method will be called per second. Title String determines the window title of the game. aObjects List contains all objects in the game shutdown True/False value to end the game loop The next table present the class methods. Method Description refresh() Delete objects. settitle(cTitle) Set the window title using a string parameter. shutdown() Close the application. The next table present a group of keywords defined by the class. Keyword Description sprite Create new Sprite object and add it to the game objects. text Create new Text object and add it to the game objects. animate Create new Animate object and add it to the game objects. sound Create new Sound object and add it to the game objects. map Create new Map object and add it ot the game objects. 47.7 GameObject Class The next table present the class attributes. 47.6. Game Class 352
  • 6. Ring Documentation, Release 1.3 Attributes Description enabled True/False determine the state of the object (Active/Not Active) x Number determine the x position of the object. y Number determine the y position of the object. width Number determine the width of the object. height Number determine the height of the object. nIndex Number determine the index of the object in objects list. animate True/False to animate the object or not. move True/False to move the object using the keyboard or not. Scaled True/False to scale the object image or not. draw Function to be called when drawing the object. state Function to be called for object animation. keypress Function to be called when a key is pressed. mouse Function to be called when a mouse event happens. The next table present the class methods. Method Description keyboard(oGame,nkey) Check Keyboard Events mouse(oGame,nType,aMouseList) Check Mouse Events rgb(r,g,b) Return new color using the RGB (Red, Green and Blue) Values. 47.8 Sprite Class Parent Class : GameObject Class The next table present the class attributes. Attributes Description image String determine the image file name. point Number determine the limit of automatic movement of the object. direction Number determine the direction of movement. nstep Number determine the increment/decrement during movement. type Number determine the object type in the game (Optional). transparent True/False value determine if the image is transparent. The next table present the class methods. Method Description Draw(oGame) Draw the object 47.9 Text Class Parent Class : Sprite Class The next table present the class attributes. Attributes Description size Number determine the font size font String determine the font file name text String determine the text to be displayed color Number determine the color The next table present the class methods. 47.8. Sprite Class 353
  • 7. Ring Documentation, Release 1.3 Method Description Draw(oGame) Draw the object 47.10 Animate Class Parent Class : Sprite Class The next table present the class attributes. Attributes Description frames Number determine the number of frames frame Number determine the active frame framewidth Number determine the frame width. animate True/False determine using animate or not. scaled True/False determine scaling image or not. The next table present the class methods. Method Description Draw(oGame) Draw the object 47.11 Sound Class Parent Class : GameObject Class The next table present the class attributes. Attributes Description file String determine the sound file name. once True/False determine to play the file one time or not (loop). The next table present the class methods. Method Description playsound() Play the sound file 47.12 Map Class Parent Class : Sprite Class The next table present the class attributes. Attributes Description aMap List determine the map content using numbers. aImages List determine the image used for each number in the map. BlockWidth Number determine the block width (default = 32). BlockHeight Number determine the block height (default = 32). Animate True/False determine the animation status. The next table present the class methods. Method Description getvalue(x,y) Return the item value in the Map according to the visible part 47.10. Animate Class 354
  • 8. Ring Documentation, Release 1.3 47.13 Using the Game Engine - Creating the Game Window 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" } # Start the Events Loop Note: if you want to define global variables, this must be before load “gameengine.ring” because this instruction will give the control to the game engine. Screen Shot: 47.14 Using the Game Engine - Drawing Text Load "gameengine.ring" # Give Control to the Game Engine func main # Called by the Game Engine 47.13. Using the Game Engine - Creating the Game Window 355
  • 9. Ring Documentation, Release 1.3 oGame = New Game # Create the Game Object { title = "My First Game" text { x = 10 y=50 animate = false size = 20 file = "fonts/pirulen.ttf" text = "game development using ring is very fun!" color = rgb(0,0,0) } } # Start the Events Loop Screen Shot: 47.15 Using the Game Engine - Moving Text Load "gameengine.ring" # Give Control to the Game Engine func main # Called by the Game Engine oGame = New Game # Create the Game Object 47.15. Using the Game Engine - Moving Text 356
  • 10. Ring Documentation, Release 1.3 { title = "My First Game" text { x = 10 y=50 animate = false size = 20 file = "fonts/pirulen.ttf" text = "game development using ring is very fun!" color = rgb(0,0,0) # Color = black } text { x = 10 y=150 # Animation Part ===================================== animate = true # Use Animation direction = GE_DIRECTION_INCVERTICAL # Increase y point = 400 # Continue until y=400 nStep = 3 # Each time y+= 3 #===================================================== size = 20 file = "fonts/pirulen.ttf" text = "welcome to the real world!" color = rgb(0,0,255) # Color = Blue } } # Start the Events Loop Screen Shot: 47.15. Using the Game Engine - Moving Text 357