Learning Python – A first foray into games & graphics
I have started close to 50 game projects in the last few years, many reached only planning stages but a few resulted in something somewhat playable. I’ve used a couple of technologies, text & canvas web games and DarkBasic, however neither really felt ideal to me.
Programming a game in canvas requires too many different languages: Javascript, PHP, HTML & MySQL if you want any persistance; while the syntax of DarkBasic is not to my liking and as a language it has some very frustrating structural limitations.
What I’m looking for is a language that can cover as much of the process as possible; graphics, logic & networking, yet also not be over-simplified. I have looked at C/C++, but these languages have the issue of being ridiculously nerdy and complicated.
Python, therefore, looks to be the solution. Lovely.
PyGame is a great looking plugin for python that saves you the agony of struggling with multiple operating-system graphics and coding the core interactions from scratch. It also has the best website in existence.
The first tutorial is a simple bouncing ball, much like the old Windows 98 screensavers. In just a few lines of code (and a quick use of MS Paint) you can have your first 2d graphics. Sorted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import sys, pygame pygame.init() size = width, height = 1280, 768 speed = [2, 2] black = 0, 0, 0 screen = pygame.display.set_mode(size) ball = pygame.image.load("ball.bmp") ballrect = ball.get_rect() while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ballrect = ballrect.move(speed) if ballrect.left < 0 or ballrect.right > width: speed[0] = -speed[0] if ballrect.top < 0 or ballrect.top > height: speed[1] = -speed[1] screen.fill(black) screen.blit(ball, ballrect) pygame.display.flip() |
import sys, pygame
pygame.init()
size = width, height = 1280, 768
speed = [2, 2]
black = 0, 0, 0
screen = pygame.display.set_mode(size)
ball = pygame.image.load("ball.bmp")
ballrect = ball.get_rect()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.top > height:
speed[1] = -speed[1]
screen.fill(black)
screen.blit(ball, ballrect)
pygame.display.flip()
Getting rid of the screen reset on line 23 after a few bounces results in a rather cool effect:
comments and stuff
