Thursday, August 23, 2007

Rabbyt Tutorial

This is a basic, hello world application for Rabbyt. It'll create a window and display a sprite.


import rabbyt
import pygame
from pygame.locals import *

#setup window
rabbyt.init_display(size=(640, 480))
rabbyt.set_viewport((640, 480))

#load our sprite
mySprite = rabbyt.Sprite("test.png")

#the gameloop
keepRunning = True

while keepRunning:
#messagepump
for event in pygame.event.get():
if event.type == QUIT:
keepRunning = False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
keepRunning = False

#the actual rendering code
rabbyt.clear() #clear the screen

mySprite.render() #render the sprite

pygame.display.flip() #flip the buffers

That's it! So, what does it really do?


import rabbyt
import pygame
from pygame.locals import *
Well, of course we need to import rabbyt, but we also need pygame because we use it to handle our window.

#setup window
rabbyt.init_display(size=(640, 480))
This line creates a window with the size 640x480 and initializes OpenGL on it.


#load our sprite
mySprite = rabbyt.Sprite("test.png")
Loads the image test.png and assigns it to a new sprite named mySprite.


rabbyt.clear() #clear the screen
Yup, just as it says, it clears the screen. It clears to black by default, but by giving a tuple with the desired rgba value (ie. (r, g, b, a) ) you get to choose which color to clear to.


mySprite.render() #render the sprite
Draws our sprite. Where it gets drawn is depending on the coordinates given in mySprite.x and mySprite.y, which determines the center of the sprite. They default to zero, which means our image will show up centered on the screen.


pygame.display.flip() #flip the buffers

This performs a OpenGL buffer swap, everything we have drawn to the backbuffer finally shows up on screen.

That'd be all that's needed to get Rabbyt up and running and drawing a sprite.

More detailed info about Rabbyt can be found at its homepage.

Got a question or just want to make yourself heard? Please drop a comment!

1 comment:

luke said...

Great tutorial! I think that this, along with rabbyt, could allow beginning game programmers to start makeing games with python!