Showing posts with label pygame. Show all posts
Showing posts with label pygame. Show all posts

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!

Sunday, May 27, 2007

Pygame

When choosing library for my snake game I decided to use pygame, which is a SDL binding for Python with a bunch of extra game related functionalities.

Why I decided to use this library is mostly because of the fact that as soon as someone in any forum mentions python and game development in the same sentence the phrase "try pygame" has a tendency to follow.

My first impressions of pygame was that it was really handy, everything is quite straightforward and really clean. Setting up the window and drawing some simple graphics is really quick and easy.

But the performance on the other hand is extremely poor, and the only way to get reasonable framerates is by using dirtyrects, and keep optimizing in mind all the time. As soon as bigger parts of the screen needs to be redrawn the fps dropped dramatically. This is a huge pain in the ass in my opinion. I don't think I'll ever use this library for a game again, at least not without combining with opengl or any other library to do the actual drawing.