<!DOCTYPE map SYSTEM "http://mapeditor.org/dtd/1.0/map.dtd">
<!DOCTYPE map SYSTEM "http://mapeditor.org/dtd/1.0/map.dtd">
This is not a requirement for my applications and it’s a huge performance overhead when works (almost 1 second for each map loaded) and when the applications is running in a environment without Internet it just waits for almost a minute and then fail with the remain decoding. A dirty workaround is open the XML file and get rid of the line containing the DTD reference.
But the correct way to programming XML decoding when we are not concerned on validate a XML schema is just the xml.parsers.expat. Instead of using a interface you just have to set some callback functions with the behaviors we want. This is a example from the documentation:
Here’s a simple key handle in Pygame wheres you move a circle using keyboard.
import pygame
from pygame.localsimport *
def main():
x,y =(100,100)
pygame.init()
screen = pygame.display.set_mode((400,400))while1:
pygame.time.delay(1000/60)# exit handlefor event in pygame.event.get():
if event.type== QUIT:
returnelif event.type== KEYDOWN and event.key== K_ESCAPE:
return# keys handle
key=pygame.key.get_pressed()if key[K_LEFT]:
x-=1if key[K_RIGHT]:
x+=1if key[K_UP]:
y-=1if key[K_DOWN]:
y+=1# fill background and draw a white circle
screen.fill((255,255,255))
pygame.draw.circle(screen,(0,0,0),[x,y],30)
pygame.display.flip()if __name__ =='__main__': main()
import pygame
from pygame.locals import *
def main():
x,y = (100,100)
pygame.init()
screen = pygame.display.set_mode((400, 400))
while 1:
pygame.time.delay(1000/60)
# exit handle
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
return
# keys handle
key=pygame.key.get_pressed()
if key[K_LEFT]:
x-=1
if key[K_RIGHT]:
x+=1
if key[K_UP]:
y-=1
if key[K_DOWN]:
y+=1
# fill background and draw a white circle
screen.fill((255,255,255))
pygame.draw.circle(screen, (0,0,0), [x,y], 30)
pygame.display.flip()
if __name__ == '__main__': main()
Here’s a video of it working:
Function pygame.key.get_pressed Returns a sequence of boolean values representing the state of every key on the keyboard. It’s very useful because usually on others game platforms I have to create it by myself.
This approach allow me to handle more than one key at time. For example, left and up keys can be pressed and each one is handled separately creating a diagonal movement.
We set some constants like the screen size and the number N of star we want.
N =200
SCREEN_W, SCREEN_H =(640,480)
N = 200
SCREEN_W, SCREEN_H = (640, 480)
Using list comprehension we create a list of random points in the screen, that will be our stars. The size of this list is N.
stars =[[random.randint(0, SCREEN_W),random.randint(0, SCREEN_H)]for x inrange(N)]
stars = [
[random.randint(0, SCREEN_W),random.randint(0, SCREEN_H)]
for x in range(N)
]
Each star is represented by one tuple on the stars list. The first star is on stars[0] and is a touple with [x, y] positions.
At each step from the game loop we draw and update the position of each star. A star is draw as a white line of one pixel. See the pygame.draw.line doc.
for star in stars:
pygame.draw.line(background,(255,255,255),(star[0], star[1]),(star[0], star[1]))
star[0]= star[0] - 1if star[0]<0:
star[0]= SCREEN_W
star[1]=random.randint(0, SCREEN_H)
for star in stars:
pygame.draw.line(background,
(255, 255, 255), (star[0], star[1]), (star[0], star[1]))
star[0] = star[0] - 1
if star[0] < 0:
star[0] = SCREEN_W
star[1] = random.randint(0, SCREEN_H)
In this example we update the position of a star by decreasing its horizontal position. When the horizontal position is less than zero, it’s not displayed on the screen anymore so we replace its horizontal position (star[0]) by the screen width (SCREEN_W) and the vertical position (star[1]) by a new random position. This will be like create a new star and guarantee always a different pattern of sliding stars.
The complete code:
#!/usr/bin/env python# A simple effect of sliding stars to create a deep space sensation.# by Silveira Neto <me@silveiraneto.net># Free under the terms of GPLv3 license# See http://silveiraneto.net/2009/08/12/pygame-simple-space-effect/importos,sys,randomimport pygame
from pygame.localsimport *
# Constants
N =200
SCREEN_W, SCREEN_H =(640,480)def main():
# basic start
pygame.init()
screen = pygame.display.set_mode((SCREEN_W,SCREEN_H))
pygame.display.set_caption('Simple Space Effect by Silveira Neto')# create background
background = pygame.Surface(screen.get_size())
background = background.convert()# generate N stars
stars =[[random.randint(0, SCREEN_W),random.randint(0, SCREEN_H)]for x inrange(N)]# main loop
clock = pygame.time.Clock()while1:
clock.tick(22)for event in pygame.event.get():
if event.type== QUIT:
returnelif event.type== KEYDOWN and event.key== K_ESCAPE:
return
background.fill((0,0,0))for star in stars:
pygame.draw.line(background,(255,255,255),(star[0], star[1]),(star[0], star[1]))
star[0]= star[0] - 1if star[0]<0:
star[0]= SCREEN_W
star[1]=random.randint(0, SCREEN_H)
screen.blit(background,(0,0))
pygame.display.flip()if __name__ =='__main__': main()
#!/usr/bin/env python
# A simple effect of sliding stars to create a deep space sensation.
# by Silveira Neto <me@silveiraneto.net>
# Free under the terms of GPLv3 license
# See http://silveiraneto.net/2009/08/12/pygame-simple-space-effect/
import os,sys,random
import pygame
from pygame.locals import *
# Constants
N = 200
SCREEN_W, SCREEN_H = (640, 480)
def main():
# basic start
pygame.init()
screen = pygame.display.set_mode((SCREEN_W,SCREEN_H))
pygame.display.set_caption('Simple Space Effect by Silveira Neto')
# create background
background = pygame.Surface(screen.get_size())
background = background.convert()
# generate N stars
stars = [
[random.randint(0, SCREEN_W),random.randint(0, SCREEN_H)]
for x in range(N)
]
# main loop
clock = pygame.time.Clock()
while 1:
clock.tick(22)
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
return
background.fill((0,0,0))
for star in stars:
pygame.draw.line(background,
(255, 255, 255), (star[0], star[1]), (star[0], star[1]))
star[0] = star[0] - 1
if star[0] < 0:
star[0] = SCREEN_W
star[1] = random.randint(0, SCREEN_H)
screen.blit(background, (0,0))
pygame.display.flip()
if __name__ == '__main__': main()