All pastes #752895 Raw Edit

base.py

public python v1 · immutable
#752895 ·published 2007-10-28 20:17 UTC
rendered paste body
import pygameimport osimport sysimport randomimport timefrom pygame.locals import *pygame.init()screen = pygame.display.set_mode((800,600))pygame.key.set_repeat(1,0)pygame.mouse.set_visible(0)done = 0arial = pygame.font.match_font('doesNotExist,Arial')arialFont = pygame.font.Font(arial, 13)class world:  def __init__(self,name):    self.camera_x = 0    self.camera_y = 0    self.rooms = []    self.name = name    self.tileset = []    self.vis_x = 15    self.vis_y = 40    self.max_x = 0    self.max_y = 0    pygame.display.set_caption(name)  def scroll(self,dir):    print self.camera_x,self.camera_y    #~ if dir == "left" and self.camera_x > 0 and self.camera_y + self.vis_y < self.max_y:    if dir == "right":      self.camera_x -= 1      #~ self.camera_y += 1    #~ elif dir == "right" and self.camera_x + self.vis_x < self.max_x and self.camera_y> 0:    elif dir == "left":      self.camera_x += 1      #~ self.camera_y -= 1    #~ elif dir == "up" and self.camera_x > 0 and self.camera_y > 0:    elif dir == "down":      #~ self.camera_x -= 1      self.camera_y += 2    #~ elif dir == "down" and self.camera_x + self.vis_x < self.max_x and self.camera_y + self.vis_y < self.max_y:    elif dir == "up":      #~ self.camera_x += 1      self.camera_y -= 2  def gen_map(self,mapw,maph,tilenum):    genmap =[]    maxtile = tilenum - 1    for y in range(0,mapw):      for x in range(0,maph):        genmap.append(random.randint(0,maxtile))    return genmap    def add_room(self,id,name,musicfile,tileset,tilenum,tilew,tileh,mapw,maph,map):    self.rooms.append(id)    self.rooms.append(name)    self.rooms.append(musicfile)    self.rooms.append(tileset)    self.rooms.append(tilenum)    self.rooms.append(tilew)    self.rooms.append(tileh)    self.rooms.append(mapw)    self.rooms.append(maph)    if map == []:      self.rooms.append(self.gen_map(mapw,maph,tilenum))    else:      self.rooms.append(map)      def del_room(self,id):    for x in range(0,len(self.rooms)):      if self.rooms[x] == id:        del(self.rooms[x:x+10])        break  def blit_room(self,id):    self.tileset = []    for x in range(0,len(self.rooms)):      if self.rooms[x] == id:        data = x    roomid = self.rooms[data]    roomname = self.rooms[data+1]    roommusic = load_sound(self.rooms[data+2])    pygame.display.set_caption(roomname + ": "+ str(roomid))    roomtileset = self.rooms[data+3]    tile = load_image(roomtileset,-1)    roomtilelen = self.rooms[data+4]    tilew = self.rooms[data+5]    tileh = self.rooms[data+6]    mapx = self.rooms[data+7]    mapy = self.rooms[data+8]    map = self.rooms[data+9]    screen.fill((0,0,0))    self.max_x = mapx    self.max_y = mapy    cx = (mapx * tilew)/10    cy = (mapy * tileh)/10    stw = tilew/2    sth = tileh/2    sx = 0    sy = 0    #~ Put the tiles in surface list    for x in range(0,roomtilelen):      self.tileset.append(tile.subsurface((0,x*tileh,tilew,tileh)))    for y in range(self.camera_y,self.vis_y+self.camera_y):      sy = sy + 1      for x in range(self.camera_x, self.vis_x+self.camera_x):        sx = sx + 1        text = arialFont.render(str(x) +" "+  str(y), 1, (128,128 ,128), (255,255,128))        #~ screenx = sx * stw - sy* stw        #~ screeny = sx * sth + sy* sth        Px = sx*64+(sy & 1) * (32) - 96        Py = sy * 16  - 32        try:          blit(screen,self.tileset[map[(x)+mapx*(y)]],(Px,Py))        except:          pass      sx = 0def blit(surface,s2b,(x,y)):  surface.blit(s2b,(x,y))def trans(surface,value):  surface.set_aplha(value)  def update():  pygame.display.flip()  def load_image(name, colorkey=None):    fullname = os.path.join('data', name)    try:        image = pygame.image.load(fullname)    except pygame.error, message:        print 'Cannot load image', fullname        raise SystemExit, message    image = image.convert()    if colorkey is not None:        if colorkey is -1:            colorkey = image.get_at((0,0))        image.set_colorkey(colorkey, RLEACCEL)    return imagedef load_sound(name):    class NoneSound:        def play(self): pass    if not pygame.mixer:        return NoneSound()    fullname = os.path.join('data', name)    try:        sound = pygame.mixer.Sound(fullname)    except pygame.error, message:        print "Cannot load sound:," + fullname        raise SystemExit, message    return sound#~ Set up the test worldtest = world("Test World")test.add_room(0,"test room","test.wav","testset.png",3,64,32,100,100,[])test.blit_room(0)update()while not done:        # Event Handling:        events = pygame.event.get()        for e in events:            if( e.type == QUIT ):                done = True                break            elif (e.type == KEYDOWN):                if( e.key == K_ESCAPE ):                    done = True                    break                if( e.key == K_LEFT ):                  test.scroll("left")                  test.blit_room(0)                  update()                if( e.key == K_RIGHT ):                  test.scroll("right")                  test.blit_room(0)                  update()                if( e.key == K_UP ):                  test.scroll("up")                  test.blit_room(0)                  update()                if( e.key == K_DOWN ):                  test.scroll("down")                  test.blit_room(0)                  update()