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

Mahmoud Samir Fayed
Mahmoud Samir FayedThe Creator of Ring at Ring Programming Language em Ring Programming Language

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

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

Recomendados

The Ring programming language version 1.5.3 book - Part 48 of 184 por
The Ring programming language version 1.5.3 book - Part 48 of 184The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184Mahmoud Samir Fayed
19 visualizações10 slides
The Ring programming language version 1.2 book - Part 36 of 84 por
The Ring programming language version 1.2 book - Part 36 of 84The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84Mahmoud Samir Fayed
31 visualizações10 slides
The Ring programming language version 1.4 book - Part 14 of 30 por
The Ring programming language version 1.4 book - Part 14 of 30The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.4 book - Part 14 of 30Mahmoud Samir Fayed
23 visualizações30 slides
The Ring programming language version 1.9 book - Part 58 of 210 por
The Ring programming language version 1.9 book - Part 58 of 210The Ring programming language version 1.9 book - Part 58 of 210
The Ring programming language version 1.9 book - Part 58 of 210Mahmoud Samir Fayed
25 visualizações10 slides
Intro to Python (High School) Unit #3 por
Intro to Python (High School) Unit #3Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3Jay Coskey
2.5K visualizações42 slides
The Ring programming language version 1.7 book - Part 53 of 196 por
The Ring programming language version 1.7 book - Part 53 of 196The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196Mahmoud Samir Fayed
8 visualizações10 slides

Mais conteúdo relacionado

Mais procurados

Lec 9 05_sept [compatibility mode] por
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Palak Sanghani
383 visualizações44 slides
Creating Games for Asha - platform por
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platformJussi Pohjolainen
1.1K visualizações41 slides
WP7 HUB_XNA por
WP7 HUB_XNAWP7 HUB_XNA
WP7 HUB_XNAMICTT Palma
331 visualizações39 slides
Intro to programming games with clojure por
Intro to programming games with clojureIntro to programming games with clojure
Intro to programming games with clojureJuio Barros
1K visualizações58 slides
Artdm170 week12 user_interaction por
Artdm170 week12 user_interactionArtdm170 week12 user_interaction
Artdm170 week12 user_interactionGilbert Guerrero
401 visualizações23 slides
C++ Windows Forms L08 - GDI P1 por
C++ Windows Forms L08 - GDI P1 C++ Windows Forms L08 - GDI P1
C++ Windows Forms L08 - GDI P1 Mohammad Shaker
776 visualizações64 slides

Mais procurados(19)

Lec 9 05_sept [compatibility mode] por Palak Sanghani
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]
Palak Sanghani383 visualizações
Creating Games for Asha - platform por Jussi Pohjolainen
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platform
Jussi Pohjolainen1.1K visualizações
WP7 HUB_XNA por MICTT Palma
WP7 HUB_XNAWP7 HUB_XNA
WP7 HUB_XNA
MICTT Palma331 visualizações
Intro to programming games with clojure por Juio Barros
Intro to programming games with clojureIntro to programming games with clojure
Intro to programming games with clojure
Juio Barros1K visualizações
Artdm170 week12 user_interaction por Gilbert Guerrero
Artdm170 week12 user_interactionArtdm170 week12 user_interaction
Artdm170 week12 user_interaction
Gilbert Guerrero401 visualizações
C++ Windows Forms L08 - GDI P1 por Mohammad Shaker
C++ Windows Forms L08 - GDI P1 C++ Windows Forms L08 - GDI P1
C++ Windows Forms L08 - GDI P1
Mohammad Shaker776 visualizações
Maximizing performance of 3 d user generated assets in unity por WithTheBest
Maximizing performance of 3 d user generated assets in unityMaximizing performance of 3 d user generated assets in unity
Maximizing performance of 3 d user generated assets in unity
WithTheBest832 visualizações
libGDX: Simple Frame Animation por Jussi Pohjolainen
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
Jussi Pohjolainen4.7K visualizações
Peyton jones-2009-fun with-type_functions-slide por Takayuki Muranushi
Peyton jones-2009-fun with-type_functions-slidePeyton jones-2009-fun with-type_functions-slide
Peyton jones-2009-fun with-type_functions-slide
Takayuki Muranushi1.3K visualizações
Peyton jones-2011-type classes por Takayuki Muranushi
Peyton jones-2011-type classesPeyton jones-2011-type classes
Peyton jones-2011-type classes
Takayuki Muranushi2.2K visualizações
The Ring programming language version 1.2 book - Part 35 of 84 por Mahmoud Samir Fayed
The Ring programming language version 1.2 book - Part 35 of 84The Ring programming language version 1.2 book - Part 35 of 84
The Ring programming language version 1.2 book - Part 35 of 84
Mahmoud Samir Fayed35 visualizações
WP7 HUB_XNA overview por MICTT Palma
WP7 HUB_XNA overviewWP7 HUB_XNA overview
WP7 HUB_XNA overview
MICTT Palma686 visualizações
libGDX: User Input and Frame by Frame Animation por Jussi Pohjolainen
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame Animation
Jussi Pohjolainen2.8K visualizações
Procedural Content Generation with Clojure por Mike Anderson
Procedural Content Generation with ClojureProcedural Content Generation with Clojure
Procedural Content Generation with Clojure
Mike Anderson1.7K visualizações
Enter The Matrix por Mike Anderson
Enter The MatrixEnter The Matrix
Enter The Matrix
Mike Anderson20.1K visualizações
Clojure for Data Science por henrygarner
Clojure for Data ScienceClojure for Data Science
Clojure for Data Science
henrygarner3.7K visualizações
maXbox starter68 machine learning VI por Max Kleiner
maXbox starter68 machine learning VImaXbox starter68 machine learning VI
maXbox starter68 machine learning VI
Max Kleiner189 visualizações
Learn Java 3D por Jay Thakkar
Learn Java 3D Learn Java 3D
Learn Java 3D
Jay Thakkar1.2K visualizações
Extreme dxt compression por Ataceyhun Çelik
Extreme dxt compressionExtreme dxt compression
Extreme dxt compression
Ataceyhun Çelik355 visualizações

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

The Ring programming language version 1.5.3 book - Part 58 of 184 por
The Ring programming language version 1.5.3 book - Part 58 of 184The Ring programming language version 1.5.3 book - Part 58 of 184
The Ring programming language version 1.5.3 book - Part 58 of 184Mahmoud Samir Fayed
15 visualizações10 slides
The Ring programming language version 1.5.4 book - Part 48 of 185 por
The Ring programming language version 1.5.4 book - Part 48 of 185The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185Mahmoud Samir Fayed
10 visualizações10 slides
CS 354 Pixel Updating por
CS 354 Pixel UpdatingCS 354 Pixel Updating
CS 354 Pixel UpdatingMark Kilgard
1K visualizações42 slides
The Ring programming language version 1.5.1 book - Part 47 of 180 por
The Ring programming language version 1.5.1 book - Part 47 of 180The Ring programming language version 1.5.1 book - Part 47 of 180
The Ring programming language version 1.5.1 book - Part 47 of 180Mahmoud Samir Fayed
16 visualizações10 slides
The Ring programming language version 1.8 book - Part 55 of 202 por
The Ring programming language version 1.8 book - Part 55 of 202The Ring programming language version 1.8 book - Part 55 of 202
The Ring programming language version 1.8 book - Part 55 of 202Mahmoud Samir Fayed
11 visualizações10 slides
ACM Mid-Southeast Slides por
ACM Mid-Southeast SlidesACM Mid-Southeast Slides
ACM Mid-Southeast Slideskrinchan
351 visualizações26 slides

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

The Ring programming language version 1.5.3 book - Part 58 of 184 por Mahmoud Samir Fayed
The Ring programming language version 1.5.3 book - Part 58 of 184The Ring programming language version 1.5.3 book - Part 58 of 184
The Ring programming language version 1.5.3 book - Part 58 of 184
Mahmoud Samir Fayed15 visualizações
The Ring programming language version 1.5.4 book - Part 48 of 185 por Mahmoud Samir Fayed
The Ring programming language version 1.5.4 book - Part 48 of 185The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185
Mahmoud Samir Fayed10 visualizações
CS 354 Pixel Updating por Mark Kilgard
CS 354 Pixel UpdatingCS 354 Pixel Updating
CS 354 Pixel Updating
Mark Kilgard1K visualizações
The Ring programming language version 1.5.1 book - Part 47 of 180 por Mahmoud Samir Fayed
The Ring programming language version 1.5.1 book - Part 47 of 180The Ring programming language version 1.5.1 book - Part 47 of 180
The Ring programming language version 1.5.1 book - Part 47 of 180
Mahmoud Samir Fayed16 visualizações
The Ring programming language version 1.8 book - Part 55 of 202 por Mahmoud Samir Fayed
The Ring programming language version 1.8 book - Part 55 of 202The Ring programming language version 1.8 book - Part 55 of 202
The Ring programming language version 1.8 book - Part 55 of 202
Mahmoud Samir Fayed11 visualizações
ACM Mid-Southeast Slides por krinchan
ACM Mid-Southeast SlidesACM Mid-Southeast Slides
ACM Mid-Southeast Slides
krinchan351 visualizações
Bow&amp;arrow game por Shaibal Ahmed
Bow&amp;arrow gameBow&amp;arrow game
Bow&amp;arrow game
Shaibal Ahmed151 visualizações
Scmad Chapter07 por Marcel Caraciolo
Scmad Chapter07Scmad Chapter07
Scmad Chapter07
Marcel Caraciolo525 visualizações
Lab Assignment 17 - Working with Object ArraysIn the old days we w.pdf por eyewaregallery
Lab Assignment 17 - Working with Object ArraysIn the old days we w.pdfLab Assignment 17 - Working with Object ArraysIn the old days we w.pdf
Lab Assignment 17 - Working with Object ArraysIn the old days we w.pdf
eyewaregallery3 visualizações
The Ring programming language version 1.9 book - Part 56 of 210 por Mahmoud Samir Fayed
The Ring programming language version 1.9 book - Part 56 of 210The Ring programming language version 1.9 book - Part 56 of 210
The Ring programming language version 1.9 book - Part 56 of 210
Mahmoud Samir Fayed8 visualizações
Introduction to Coding por Fabio506452
Introduction to CodingIntroduction to Coding
Introduction to Coding
Fabio50645212 visualizações
Tim Popl por mchaar
Tim PoplTim Popl
Tim Popl
mchaar775 visualizações
The Next Mainstream Programming Language: A Game Developer’s Perspective por guest4fd7a2
The Next Mainstream Programming Language: A Game Developer’s PerspectiveThe Next Mainstream Programming Language: A Game Developer’s Perspective
The Next Mainstream Programming Language: A Game Developer’s Perspective
guest4fd7a21.2K visualizações
Programming projects shall be submitted by the date due.  Items tu.docx por wkyra78
Programming projects shall be submitted by the date due.  Items tu.docxProgramming projects shall be submitted by the date due.  Items tu.docx
Programming projects shall be submitted by the date due.  Items tu.docx
wkyra782 visualizações
The Ring programming language version 1.3 book - Part 83 of 88 por Mahmoud Samir Fayed
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88
Mahmoud Samir Fayed46 visualizações
The Ring programming language version 1.5.4 book - Part 47 of 185 por Mahmoud Samir Fayed
The Ring programming language version 1.5.4 book - Part 47 of 185The Ring programming language version 1.5.4 book - Part 47 of 185
The Ring programming language version 1.5.4 book - Part 47 of 185
Mahmoud Samir Fayed12 visualizações
Advanced Game Development with the Mobile 3D Graphics API por Tomi Aarnio
Advanced Game Development with the Mobile 3D Graphics APIAdvanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics API
Tomi Aarnio1.4K visualizações
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx por fredharris32
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docxasmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
fredharris324 visualizações
Introduction to Deep Learning and Tensorflow por Oswald Campesato
Introduction to Deep Learning and TensorflowIntroduction to Deep Learning and Tensorflow
Introduction to Deep Learning and Tensorflow
Oswald Campesato203 visualizações
C# Summer course - Lecture 2 por mohamedsamyali
C# Summer course - Lecture 2C# Summer course - Lecture 2
C# Summer course - Lecture 2
mohamedsamyali553 visualizações

Mais de Mahmoud Samir Fayed

The Ring programming language version 1.10 book - Part 212 of 212 por
The Ring programming language version 1.10 book - Part 212 of 212The Ring programming language version 1.10 book - Part 212 of 212
The Ring programming language version 1.10 book - Part 212 of 212Mahmoud Samir Fayed
89 visualizações6 slides
The Ring programming language version 1.10 book - Part 211 of 212 por
The Ring programming language version 1.10 book - Part 211 of 212The Ring programming language version 1.10 book - Part 211 of 212
The Ring programming language version 1.10 book - Part 211 of 212Mahmoud Samir Fayed
50 visualizações10 slides
The Ring programming language version 1.10 book - Part 210 of 212 por
The Ring programming language version 1.10 book - Part 210 of 212The Ring programming language version 1.10 book - Part 210 of 212
The Ring programming language version 1.10 book - Part 210 of 212Mahmoud Samir Fayed
43 visualizações10 slides
The Ring programming language version 1.10 book - Part 208 of 212 por
The Ring programming language version 1.10 book - Part 208 of 212The Ring programming language version 1.10 book - Part 208 of 212
The Ring programming language version 1.10 book - Part 208 of 212Mahmoud Samir Fayed
38 visualizações10 slides
The Ring programming language version 1.10 book - Part 207 of 212 por
The Ring programming language version 1.10 book - Part 207 of 212The Ring programming language version 1.10 book - Part 207 of 212
The Ring programming language version 1.10 book - Part 207 of 212Mahmoud Samir Fayed
31 visualizações10 slides
The Ring programming language version 1.10 book - Part 205 of 212 por
The Ring programming language version 1.10 book - Part 205 of 212The Ring programming language version 1.10 book - Part 205 of 212
The Ring programming language version 1.10 book - Part 205 of 212Mahmoud Samir Fayed
31 visualizações10 slides

Mais de Mahmoud Samir Fayed(20)

The Ring programming language version 1.10 book - Part 212 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 212 of 212The Ring programming language version 1.10 book - Part 212 of 212
The Ring programming language version 1.10 book - Part 212 of 212
Mahmoud Samir Fayed89 visualizações
The Ring programming language version 1.10 book - Part 211 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 211 of 212The Ring programming language version 1.10 book - Part 211 of 212
The Ring programming language version 1.10 book - Part 211 of 212
Mahmoud Samir Fayed50 visualizações
The Ring programming language version 1.10 book - Part 210 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 210 of 212The Ring programming language version 1.10 book - Part 210 of 212
The Ring programming language version 1.10 book - Part 210 of 212
Mahmoud Samir Fayed43 visualizações
The Ring programming language version 1.10 book - Part 208 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 208 of 212The Ring programming language version 1.10 book - Part 208 of 212
The Ring programming language version 1.10 book - Part 208 of 212
Mahmoud Samir Fayed38 visualizações
The Ring programming language version 1.10 book - Part 207 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 207 of 212The Ring programming language version 1.10 book - Part 207 of 212
The Ring programming language version 1.10 book - Part 207 of 212
Mahmoud Samir Fayed31 visualizações
The Ring programming language version 1.10 book - Part 205 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 205 of 212The Ring programming language version 1.10 book - Part 205 of 212
The Ring programming language version 1.10 book - Part 205 of 212
Mahmoud Samir Fayed31 visualizações
The Ring programming language version 1.10 book - Part 206 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 206 of 212The Ring programming language version 1.10 book - Part 206 of 212
The Ring programming language version 1.10 book - Part 206 of 212
Mahmoud Samir Fayed25 visualizações
The Ring programming language version 1.10 book - Part 204 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 204 of 212The Ring programming language version 1.10 book - Part 204 of 212
The Ring programming language version 1.10 book - Part 204 of 212
Mahmoud Samir Fayed24 visualizações
The Ring programming language version 1.10 book - Part 203 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 203 of 212The Ring programming language version 1.10 book - Part 203 of 212
The Ring programming language version 1.10 book - Part 203 of 212
Mahmoud Samir Fayed30 visualizações
The Ring programming language version 1.10 book - Part 202 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 202 of 212The Ring programming language version 1.10 book - Part 202 of 212
The Ring programming language version 1.10 book - Part 202 of 212
Mahmoud Samir Fayed16 visualizações
The Ring programming language version 1.10 book - Part 201 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 201 of 212The Ring programming language version 1.10 book - Part 201 of 212
The Ring programming language version 1.10 book - Part 201 of 212
Mahmoud Samir Fayed12 visualizações
The Ring programming language version 1.10 book - Part 200 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 200 of 212The Ring programming language version 1.10 book - Part 200 of 212
The Ring programming language version 1.10 book - Part 200 of 212
Mahmoud Samir Fayed26 visualizações
The Ring programming language version 1.10 book - Part 199 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 199 of 212The Ring programming language version 1.10 book - Part 199 of 212
The Ring programming language version 1.10 book - Part 199 of 212
Mahmoud Samir Fayed18 visualizações
The Ring programming language version 1.10 book - Part 198 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 198 of 212The Ring programming language version 1.10 book - Part 198 of 212
The Ring programming language version 1.10 book - Part 198 of 212
Mahmoud Samir Fayed14 visualizações
The Ring programming language version 1.10 book - Part 197 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 197 of 212The Ring programming language version 1.10 book - Part 197 of 212
The Ring programming language version 1.10 book - Part 197 of 212
Mahmoud Samir Fayed13 visualizações
The Ring programming language version 1.10 book - Part 196 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 196 of 212The Ring programming language version 1.10 book - Part 196 of 212
The Ring programming language version 1.10 book - Part 196 of 212
Mahmoud Samir Fayed17 visualizações
The Ring programming language version 1.10 book - Part 195 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 195 of 212The Ring programming language version 1.10 book - Part 195 of 212
The Ring programming language version 1.10 book - Part 195 of 212
Mahmoud Samir Fayed18 visualizações
The Ring programming language version 1.10 book - Part 194 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 194 of 212The Ring programming language version 1.10 book - Part 194 of 212
The Ring programming language version 1.10 book - Part 194 of 212
Mahmoud Samir Fayed13 visualizações
The Ring programming language version 1.10 book - Part 193 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 193 of 212The Ring programming language version 1.10 book - Part 193 of 212
The Ring programming language version 1.10 book - Part 193 of 212
Mahmoud Samir Fayed22 visualizações
The Ring programming language version 1.10 book - Part 192 of 212 por Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 192 of 212The Ring programming language version 1.10 book - Part 192 of 212
The Ring programming language version 1.10 book - Part 192 of 212
Mahmoud Samir Fayed19 visualizações

Último

PharoJS - Zürich Smalltalk Group Meetup November 2023 por
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023Noury Bouraqadi
127 visualizações17 slides
Future of Indian ConsumerTech por
Future of Indian ConsumerTechFuture of Indian ConsumerTech
Future of Indian ConsumerTechKapil Khandelwal (KK)
21 visualizações68 slides
PRODUCT PRESENTATION.pptx por
PRODUCT PRESENTATION.pptxPRODUCT PRESENTATION.pptx
PRODUCT PRESENTATION.pptxangelicacueva6
14 visualizações1 slide
SAP Automation Using Bar Code and FIORI.pdf por
SAP Automation Using Bar Code and FIORI.pdfSAP Automation Using Bar Code and FIORI.pdf
SAP Automation Using Bar Code and FIORI.pdfVirendra Rai, PMP
23 visualizações38 slides
Design Driven Network Assurance por
Design Driven Network AssuranceDesign Driven Network Assurance
Design Driven Network AssuranceNetwork Automation Forum
15 visualizações42 slides
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive por
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveNetwork Automation Forum
31 visualizações35 slides

Último(20)

PharoJS - Zürich Smalltalk Group Meetup November 2023 por Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi127 visualizações
PRODUCT PRESENTATION.pptx por angelicacueva6
PRODUCT PRESENTATION.pptxPRODUCT PRESENTATION.pptx
PRODUCT PRESENTATION.pptx
angelicacueva614 visualizações
SAP Automation Using Bar Code and FIORI.pdf por Virendra Rai, PMP
SAP Automation Using Bar Code and FIORI.pdfSAP Automation Using Bar Code and FIORI.pdf
SAP Automation Using Bar Code and FIORI.pdf
Virendra Rai, PMP23 visualizações
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive por Network Automation Forum
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Network Automation Forum31 visualizações
Attacking IoT Devices from a Web Perspective - Linux Day por Simone Onofri
Attacking IoT Devices from a Web Perspective - Linux Day Attacking IoT Devices from a Web Perspective - Linux Day
Attacking IoT Devices from a Web Perspective - Linux Day
Simone Onofri16 visualizações
Five Things You SHOULD Know About Postman por Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman33 visualizações
Piloting & Scaling Successfully With Microsoft Viva por Richard Harbridge
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft Viva
Richard Harbridge12 visualizações
Uni Systems for Power Platform.pptx por Uni Systems S.M.S.A.
Uni Systems for Power Platform.pptxUni Systems for Power Platform.pptx
Uni Systems for Power Platform.pptx
Uni Systems S.M.S.A.56 visualizações
Microsoft Power Platform.pptx por Uni Systems S.M.S.A.
Microsoft Power Platform.pptxMicrosoft Power Platform.pptx
Microsoft Power Platform.pptx
Uni Systems S.M.S.A.53 visualizações
Data Integrity for Banking and Financial Services por Precisely
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial Services
Precisely21 visualizações
SUPPLIER SOURCING.pptx por angelicacueva6
SUPPLIER SOURCING.pptxSUPPLIER SOURCING.pptx
SUPPLIER SOURCING.pptx
angelicacueva615 visualizações
Business Analyst Series 2023 - Week 3 Session 5 por DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10248 visualizações
AMAZON PRODUCT RESEARCH.pdf por JerikkLaureta
AMAZON PRODUCT RESEARCH.pdfAMAZON PRODUCT RESEARCH.pdf
AMAZON PRODUCT RESEARCH.pdf
JerikkLaureta26 visualizações
Igniting Next Level Productivity with AI-Infused Data Integration Workflows por Safe Software
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Safe Software263 visualizações

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