daily pastebin goal
48%
SHARE
TWEET

Stellar Game Engine Tour by onpon

Kesha Nov 27th, 2013 39 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 21:00 < onpon4> Alright, any other questions before I start the SGE tour?
  2. 21:00 < kremling> What is the SGE tour?
  3. 21:01 < Magnemania> S'tour on a python-run Game Maker lookalike.
  4. 21:01 < onpon4> That's the next workshop. :) I gave my self a more reasonable time for this one, so it shouldn't be so rushed.
  5. 21:01 < Magnemania> S'FREEEEEE in that the source code is free and available, and that means it's not the darkest of evils
  6. 21:01 < kremling> Is it something that I can download and follow along with?
  7. 21:02 < Magnemania> Check out the workshop page, he put some downloads up
  8. 21:02 < onpon4> Yep, stellarengine.nongnu.org.
  9. 21:02 < onpon4> The SGE is a 2-D engine for Python, meant to be kind of similar to Game Maker's underlying engine.
  10. 21:03 < onpon4> We'll be going through two examples: a basic "Hello, world!", and Pong.
  11. 21:03 < onpon4> This is the first, "hello world" example: http://pastebin.com/UEQghcRk
  12. 21:03 < onpon4> The first line is, of course, the shebang. It's best to always include this.
  13. 21:04 < onpon4> After that is the license. In this case, I've chosen CC0, i.e. public domain dedication.
  14. 21:04 < onpon4> For imports, we include the "sge" module. This gives you all the classes and functions from the SGE.
  15. 21:05 < onpon4> We're looking at this, guys: http://pastebin.com/UEQghcRk
  16. 21:06 < onpon4> Line 25 defines the Game class. The Game class controls everything "global". We define a subclass of sge.Game to define events, which in the SGE are special methods.
  17. 21:06 < onpon4> In this case, we defined the key press event, and the close event. Both do the same thing: they call the game's end method, which ends the game.
  18. 21:07 < onpon4> Below the game is a room definition. Rooms in the SGE are like rooms in Game Maker: they represent separate places, such as levels.
  19. 21:08 < onpon4> In this case, I have one sge.Room sub-class, which defines a step event. Step events happen every frame. Here, the main functionality of this example happens: the text
  20.                 "Hello, world!" is projected every frame.
  21. 21:08 < onpon4> The reason we do this every frame is projection lasts only one frame. Think of it as being like a projector: as soon as you stop projecting, the image disappears.
  22. 21:09 < onpon4> Finally is out main function. Here, we follow a fairly specific format: first, we create a Game object. We don't need to assign it to anything, because it automatically
  23.                 gets assigned to sge.game.
  24. 21:10 < onpon4> Second, we load resources and create things. In this example, that's just a single background, a font, and a single room object.
  25. 21:11 < onpon4> Finally, we call sge.game.start() to start the game.
  26. 21:11 < onpon4> If you run this example, you will see the text, "Hello, world!" shown in the center of a white screen.
  27. 21:12 < onpon4> Any questions so far?
  28. 21:12 < onpon4> Let's move onto a more exciting example: an actual game. Here is a very basic Pong game we are going to start with: http://pastebin.com/pH2Z73du
  29. 21:13 < onpon4> You'll notice the odd class, "glob". We're using this as a container class for "global" variables, mainly because of the requirement of the "global" keyword to assign to
  30.                 global variables, which can cause bugs.
  31. 21:14 < onpon4> For our Game class, we have some more events. First off, pressing "P" or Enter will pause the game with the SGE's built-in pause function. Second, pressing F8 will take a
  32.                 screenshot and save it.
  33. 21:15 < onpon4> While the game is paused with the SGE's pause function, all events are suspended, and instead similar "event_paused" events execute. Therefore, we define
  34.                 event_paused_key_press and event_paused_close to be able to close and unpause the game.
  35. 21:16 < onpon4> In event_close, we ask the player before actually closing, with the sge.show_message function.
  36. 21:17 < onpon4> We use a small hack here: show_message is going to return 0 for the first button, and 1 for the second button, so we check for truthfulness of the result. If "Yes" is
  37.                 clicked, it will return 1, which is True.
  38. 21:18 < onpon4> Next, line 76, starts our Player class. This is going to be used for the paddles.
  39. 21:18 < onpon4> Player inherits from a special class called sge.StellarClass. This class is similar to Game Maker's "objects"; it's for objects that appear within a room.
  40. 21:20 < onpon4> In this case, we are extending the constructor method (special method name __init__), so that we don't need to specify everything when creating a paddle; we just specify
  41.                 the player name and infer everything else.
  42. 21:20 < onpon4> The super() method allows us to call the parent's version of __init__, making this an extension rather than an override.
  43. 21:21 < onpon4> For the Player object's step event, we check the state of up_key and down_key (which were set in the constructor method) and use these states to decide how to move.
  44. 21:22 < onpon4> Then, we check if we're outside the room, and return us to the inside of the room if we are.
  45. 21:22 < onpon4> The Ball class is similar.
  46. 21:23 < onpon4> The ball class has a "serve" method, which sends it off from the center of the table to one of the players.
  47. 21:24 < onpon4> Simply, this serve method sets the ball's position back to its original position, and then sets its yvelocity (vertical movement) to 0 and its xvelocity (horizontal
  48.                 movement) based on a "direction" argument and a constant called BALL_START_SPEED (defined at the start of the file).
  49. 21:25 < onpon4> We call this serve method in the create event., since we want the game to start immediately.
  50. 21:26 < onpon4> In the step event, we bounce the ball off of the top and bottom, and bring it back into play if it passes one of the players.
  51. 21:26 < onpon4> Finally, we have the collision event, where we bounce off of each of the paddles.
  52. 21:27 < onpon4> If anyone is ever confused, please ask questions.
  53. 21:27 < onpon4> In the main function, we set our FPS to 120 (this is to somewhat hide that our controls are digital), and dynamically create the ball and paddle sprites.
  54. 21:28 < onpon4> Now, if you run this file, you may find it disappointing. It has no sound or scoring.
  55. 21:29 < onpon4> Let's fix that, with version 2: http://pastebin.com/Wv29fHKF
  56. 21:30 < onpon4> First off, we need to add a HUD.
  57. 21:30 < onpon4> We do this here as an objet, created on line 251.
  58. 21:31 < onpon4> The HUD object is never directly accessed, but instead it constantly displays a HUD sprite which we modify as needed.
  59. 21:32 < onpon4> Is anyone paying attention to this?
  60. 21:34 < Magnemania> Dropping in and out, here.  Not properly following along.
  61. 21:34 < Magnemania> Still trying to see what's goin' on in the code, though.
  62. 21:35 < onpon4> Do you have any questions?
  63. 21:35 < Magnemania> Not particularly.  The only thing that's been coming to mind is mild cracks at not being able to generate sounds dynamically.
  64. 21:36 < onpon4> Cracks?
  65. 21:36 < Magnemania> Jokes.
  66. 21:36 < Magnemania> Incidentally, can one draw on the screen directly?
  67. 21:36 < Magnemania> Using the same events that you use to generate the sprite, of course?
  68. 21:36 < onpon4> Sort of. You can draw to the room directly with the project methods.
  69. 21:38 < Magnemania> Oh!  And is it possible to make functions that don't take self as an input?
  70. 21:38 < Magnemania> Seems rather bloaty to put 'self' as an argument with functions that potentially don't use 'self'.
  71. 21:39 < onpon4> That's a syntactic requirement of Python. The first argument represents the object it's from.
  72. 21:39 < onpon4> (They're methods, not functions.)
  73. 21:40 < onpon4> There is a such thing as static methods, which are more like functions and have no connection to objects or classes, and thus don't take this mandatory argument, but those
  74.                 are different.
  75. 21:40 < Magnemania> method, function, equation, crackpot, WHAT HAVE YOU
  76. 21:40 < Magnemania> Oh.
  77. 21:41 < onpon4> Take a look at line 137, for example: self is used in line 139. "self" isn't a special keyword or anything like that in Python, so you could just as well use "this" instead.
  78. 21:41 < Magnemania> Waaaait...I thought that regular methods defined outside of classes were basically static like that.
  79. 21:42 < onpon4> That would be a function. :)
  80. 21:42 < Magnemania> That would-and I said-and
  81. 21:42 < Magnemania> ...FEH!   FEH, I SAY!
  82. 21:42 < onpon4> Line 206 is an example of a function.
  83. 21:43 < onpon4> Well, this is a mess (my fault; not enough planning), so I'm just going to mention a couple mentions.
  84. 21:43 < kremling> This actually quite interesting. Will there soon be an option to create an executable?
  85. 21:44 < kremling> I know Python doesn't really do that.
  86. 21:44 < onpon4> kremling: I think what you're thinking is something like py2exe, or cx_Freeze.
  87. 21:44 < onpon4> You take these things, and they "freeze" any Python program into an executable that you can portably execute, to distribute to people.
  88. 21:45 < kremling> Yeah, that's what I was referring. Sorry for interrupting.
  89. 21:45 < onpon4> No problem.
  90. 21:46 < onpon4> So, this was loosely based on a tutorial that I already wrote a while back. You can find it in the SGE's documentation; it tries to walk you through making these examples
  91.                 I've shown, and I think it does better than my rambling here.
  92. 21:47 < onpon4> Specifically, doc/hello.html, and doc/pong.html.
  93. 21:47 < onpon4> Sounds: a good way to get sound effects that are simple is the free/libre program, Sfxr. Some of you may know about it already.
  94. 21:48 < onpon4> Other art assets: OpenGameArt.org is a great resource.
  95. 21:48 < Magnemania> wait wha-a sound effect generator?  I could use one o' 'dose.
  96. 21:49 < onpon4> I used it for the Pong example. Take a look at examples/pong.py (run it).
  97. 21:49 < Magnemania> Ehm...I didn't actually download anything from the project, and I'm lazy enough to take your word for it.
  98. 21:50 < Magnemania> ah, so it's for making arcade-ey sorts of sounds
  99. 21:50 < TiKi> so is the workshop over yet?
  100. 21:50 < onpon4> Yep.
  101. 21:50 < onpon4> TiKi: I'm just picking up the pieces and wrapping up.
  102. 21:51 < kremling> By the way, what text editor do you use for programming in Python? Python is requires indents, and it can be quite tedious when copying and pasting code examples into
  103.                   IDLE and indention errors come up. I'm sure there are some editors that can handle this problem.
  104. 21:52 < onpon4> IDLE handles indentation quite fine (the best actually), and it's my favorite editor for Python. My second favorite is gedit.
  105. 21:52 < kremling> How do you fix the indentation problem from copying and pasting chunks of code?
  106. 21:53 < Magnemania> Whatcha copy-pasting FROM, kremling?
  107. 21:53 < kremling> The examples he provided on the pastebin website
  108. 21:54 < TiKi> this game making stuff sounds complicated
  109. 21:54 < onpon4> TiKi: It's not, really.
  110. 21:54 < TiKi> is it time-consuming
  111. 21:54 < onpon4> kremling: That shouldn't be a problem, but did you try the raw paste data?
  112. 21:54 < onpon4> TiKi: Well, yeah.
  113. 21:55 < Magnemania> It's really not that different from Game Maker, it's just that there's not much of an interface.
  114. 21:55 < kremling> I did not, that fixed the problem. Thank you.
  115. 21:55 < Magnemania> ...Is there an IDE for that thing, incidentally?
  116. 21:56 < TiKi> would it be hijacking the workshop to say that I'm a bit befuddled by how everyone goes "well DUH it's time consuming" as if I'm supposed to naturally think that
  117. 21:56 < onpon4> Which thing, Magnemania? The SGE, or something else?
  118. 21:56 < Magnemania> The SGE.
  119. 21:56 < Magnemania> to be fair, TiKi, any sort of art takes time
  120. 21:56 < Magnemania> and game design is an art
  121. 21:56 < Magnemania> and coding is, kinda, too a lesser extent
  122. 21:56 < onpon4> I'm developing a universal level editor, and one of my goals is to have an IDE as easy to use as (though probably pretty different from) Game Maker.
  123. 21:57 < Magnemania> too to a lesser extent
  124. 21:59 < TiKi> You see guys, I'm from the "Super Mario Bros. X" community, which is a level editor with a limited amount of NPC customization (i.e. you can set whether or not some enemies
  125.               are immune to fireballs) and a limited trigger-event-making system (i.e. well, just look it up)
  126. 21:59 < kremling> Are you working on this by yourself onpon4?
  127. 21:59 < TiKi> BTW onpon4 it's not libre
  128. 21:59 < onpon4> Yep.
  129. 21:59 < onpon4> (That was to kremling.)
  130. 22:00 < onpon4> It used to be part of an effort started by someone else, but he ended up stopping his part (which was, incidentally, a full Game Maker-inspired IDE).
  131. 22:00 < TiKi> (pretty nice of you to target specific people)
  132. 22:00 < onpon4> TiKi: That's normal behavior in IRC.
  133. 22:00 < kremling> If you were requesting assistance, what would I need to know to help you in creating this framework based off of pygame?
  134. 22:01 < Magnemania> TiKi, starting a game from scratch (which is what one's doing when one uses something like this) is very different from level design.
  135. 22:02 < onpon4> Well, I don't need help with the Pygame SGE. That's pretty much done. What I could use help with more is developing the IDE, and knowledge of any toolkit library (wxPython,
  136.                 GTK, Qt, Tkinter) would do.
  137. 22:02 < TiKi> and if you use something like Hello Engine you're a bad person apparently
  138. 22:02 < Magnemania> There are some pre-built Mario engines for Game Maker (which now has a somewhat expansive free version but is inherently EVIIIL) that would allow you to function in
  139.                     that way.
  140. 22:02 < TiKi> I see how that's a bad engine sure
  141. 22:02 < TiKi> but if I did that then MFGG would grill me
  142. 22:02 < Magnemania> to be fair, Drag's engine hasn't gotten any flak
  143. 22:03 < Magnemania> or Adder, or Lash, or gosh no one's going to get that reference
  144. 22:03 < onpon4> Now that it's possible to migrate to ENIGMA realistically, Game Maker isn't as bad. Just migrate to ENIGMA and it's fine. :P
  145. 22:03 < TiKi> I think that's because there's only been a few clones made with it
  146. 22:03 < TiKi> onpon4 how long you estimate until Enigma=GM compatibility
  147. 22:03 < Magnemania> Hah!  It'll be lightyears before it's completed!
  148. 22:03 < onpon4> Heck if I know. Ask ENIGMA's developers.
  149. 22:04 < Magnemania> wait, light years measure distance, not time
  150. 22:04 < onpon4> But it's made quite a bit of progress recently; it used to be little more than something kind of interesting, and now it can legitimately be used, it seems.
  151. 22:04 < TiKi> am I diverting this workshop thing too much
  152. 22:04 < onpon4> No, workshop's over.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top