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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
"""graphics class, helps everyone to draw"""
import pygame, pygame.image
from pygame.locals import *
import game, stars
#the accessable screen surface and size
surface = None
rect = Rect(0, 0, 0, 0)
#the accessable dirty rectangles
dirtyrects = []
starobj = None
def initialize(size, fullscreen):
global surface, rect, starobj
try:
flags = 0
if fullscreen:
flags |= FULLSCREEN
depth = pygame.display.mode_ok(size, flags, 16)
surface = pygame.display.set_mode(size, flags, depth)
rect = surface.get_rect()
pygame.mouse.set_visible(0)
if surface.get_bytesize() == 1:
loadpalette()
except pygame.error, msg:
import messagebox
messagebox.error('Cannot Initialize Graphics', msg.args[0])
starobj = stars.Stars()
def dirty(rect):
dirtyrects.append(rect)
def dirty2(rect1, rect2):
if not rect2:
dirtyrects.append(rect1)
elif rect.colliderect(rect2):
dirtyrects.append(rect1.union(rect2))
else:
dirtyrects.append(rect1)
dirtyrects.append(rect2)
def updatestars(bgd, gfx):
starobj.erase_tick_draw(bgd, gfx)
def update():
global dirtyrects
pygame.display.update(dirtyrects)
dirtyrects = []
def text(font, color, text, center=None, pos='center'):
bgd = 0, 0, 0
try:
if surface.get_bytesize()>1:
img = font.render(text, 1, color, bgd)
img.set_colorkey(bgd, RLEACCEL)
else:
img = font.render(text, 0, color)
img = img.convert()
except pygame.error:
print 'TEXTFAILED', text, color
img = pygame.Surface((10, 10))
r = img.get_rect()
if center: setattr(r, pos, center)
return [img, r]
def load(name):
file = game.get_resource(name)
img = pygame.image.load(file)
#use rle acceleration if no hardware accel
if not surface.get_flags() & HWSURFACE:
clear = img.get_colorkey()
if clear:
img.set_colorkey(clear, RLEACCEL)
return img.convert()
def loadpalette():
file = game.get_resource('solarwolf.pal')
pal = []
for line in file.readlines()[3:]:
vals = [int(x) for x in line.split()]
pal.append(vals)
surface.set_palette(pal)
|