Exemplo n.º 1
0
def initpygame(settings, caption, settingsfile):
    pygame.init()
    screen = get_screen(settings["res_x"], settings["res_y"], settings["hardware_buffer"], settings["fullscreen"])
    mode = "game"
    if "--editor" in sys.argv:
        mode = "editor"
        debug("Loading game editor")
    global imagecache
    imagecache = ImageCache()
    global frontend
    frontend = Frontend(mode=mode, settingsfile=settingsfile)
    global tilemaps
    tilemaps = Tilemaps()
    global messages
    imagecache.load()
    wallpaper = imagecache["landscape.png"]
    wallpaper = pygame.transform.smoothscale(wallpaper, (settings["res_x"], settings["res_y"]))
    screen.blit(wallpaper, (0, 0))
    pygame.display.set_caption(caption)
    frontend = Frontend(screen, imagecache, eventstack, tilemaps, mode=mode, settingsfile=settingsfile)
    messages = Messages(screen, eventstack)
    messages.error("Welcome to Mirthless")
    if not "--editor" in sys.argv:
        messages.error(
            "Game mode is barely started. The editor is much further along. Try starting the game with --editor"
        )
        messages.message(":)")
    tilemaps.initialize()
    return screen, frontend.screenlayout(), frontend
Exemplo n.º 2
0
    def __init__(self, ticks_per_second, max_frame_skip):
        self.TicksPerSecond = ticks_per_second
        self.TimePerTick = 1000.0 / self.TicksPerSecond # MS per tick
        self.MaxFrameSkip = max_frame_skip
        
        pygame.init()

        try:
            self.font = pygame.font.Font('VeraMono.ttf', 16)
        except:
            print 'problem loading font'
            self.font = None

        self.fontSurf = None
        self._displayFPS = False
        
        size = width, height = 800, 600
        self.black = 0,0,0
        #self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
        self.screen = pygame.display.set_mode(size)

        self.imageCache = ImageCache()
        self.imageCache.getSurface("units/Player1.png", "ball")
        self.imageCache.getSurface("units/Creature1.png", "mob")
        self.imageCache.getSurface("units/Mine1.png", "mine")
        self.imageCache.getSurface("terrains/Normal.jpg", "normal")
        self.imageCache.getSurface("terrains/Impassable5.jpg", "impassable-1")

        ball = self.imageCache.getCachedSurface("ball")
        self.mobs = pygame.sprite.Group()
        self.playerSprite = PlayerSprite(ball, width/2-16, height/2-16, self.imageCache, self.mobs)
        self.playerSpriteGroup = pygame.sprite.Group(self.playerSprite)
        self.mapView = None
Exemplo n.º 3
0
class Argon(object):
    def __init__(self, width, height):
        self.width  = width
        self.height = height

        self.load = ImageCache([in_module('assets'), os.getcwd(), '.'])
        self.default_font = self.load.font('AnonymousPro_17')

        self.running = False
        self.flags = HWSURFACE | OPENGL | DOUBLEBUF
        self.listeners = {}
        self.frame_latency = []

        pygame.display.set_mode((self.width, self.height), self.flags)
        glEnable(GL_TEXTURE_2D)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        self.render = Renderer(self, self.default_font)

    def listen(self, fn):
        self.listeners[fn.__name__] = fn
        return fn

    def run(self):
        self.running = True
        while self.running:
            now = time.time()
            self.listeners["on_frame"](now)
            pygame.display.flip()
            self.frame_latency.append(time.time() - now)
            while len(self.frame_latency) > 100:
                self.frame_latency.pop(0)
            for event in pygame.event.get():
                if event.type == QUIT:
                    self.running = False
                event_dispatch(event, self.listeners)

    def clear(self, color):
        r, g, b, a = color
        glClearColor(r/255.0, g/255.0, b/255.0, a/255.0)
        glClear(GL_COLOR_BUFFER_BIT)

    def bind(self):
        glBindFramebuffer(GL_FRAMEBUFFER, 0)

    def unbind(self):
        pass

    def show_performance_log(self):
        latency = self.frame_latency
        font = self.default_font
        if len(latency) > 0:
            avg  = sum(latency) / len(latency)
            high = max(latency) 
            text = "avg=%.2fms high=%.2fms" % (avg * 1000, high * 1000)
            self.render.text((0, self.height - font.height + font.baseline), text)
Exemplo n.º 4
0
    def __init__(self, width, height):
        self.width  = width
        self.height = height

        self.load = ImageCache([in_module('assets'), os.getcwd(), '.'])
        self.default_font = self.load.font('AnonymousPro_17')

        self.running = False
        self.flags = HWSURFACE | OPENGL | DOUBLEBUF
        self.listeners = {}
        self.frame_latency = []

        pygame.display.set_mode((self.width, self.height), self.flags)
        glEnable(GL_TEXTURE_2D)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        self.render = Renderer(self, self.default_font)
Exemplo n.º 5
0
Arquivo: main.py Projeto: nowl/games
    def __init__(self, ticks_per_second, max_frame_skip):
        self.TicksPerSecond = ticks_per_second
        self.TimePerTick = 1000.0 / self.TicksPerSecond # MS per tick
        self.MaxFrameSkip = max_frame_skip
        
        pygame.init()

        size = width, height = 800, 600
        self.black = 0,0,0
        self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)

        self.imageCache = ImageCache()
        self.imageCache.getSurface("units/Player1.png", "ball")
        self.imageCache.getSurface("units/Creature1.png", "mob")
        self.imageCache.getSurface("terrains/Normal.jpg", "normal")
        self.imageCache.getSurface("terrains/Impassable1.jpg", "impassable-1")

        ball = self.imageCache.getCachedSurface("ball")
        self.screenRect = pygame.Rect(0, 0, width, height)
        self.playerSprite = PlayerSprite(ball, self.screenRect)
        self.sprites = pygame.sprite.Group(self.playerSprite)
        self.mapView = None
Exemplo n.º 6
0
Arquivo: main.py Projeto: nowl/games
class GameLoop (object):
    def __init__(self, ticks_per_second, max_frame_skip):
        self.TicksPerSecond = ticks_per_second
        self.TimePerTick = 1000.0 / self.TicksPerSecond # MS per tick
        self.MaxFrameSkip = max_frame_skip
        
        pygame.init()

        size = width, height = 800, 600
        self.black = 0,0,0
        self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)

        self.imageCache = ImageCache()
        self.imageCache.getSurface("units/Player1.png", "ball")
        self.imageCache.getSurface("units/Creature1.png", "mob")
        self.imageCache.getSurface("terrains/Normal.jpg", "normal")
        self.imageCache.getSurface("terrains/Impassable1.jpg", "impassable-1")

        ball = self.imageCache.getCachedSurface("ball")
        self.screenRect = pygame.Rect(0, 0, width, height)
        self.playerSprite = PlayerSprite(ball, self.screenRect)
        self.sprites = pygame.sprite.Group(self.playerSprite)
        self.mapView = None

    def render(self):
        self.screen.fill(self.black)
        self.mapView.draw(self.screen)
        self.sprites.draw(self.screen)
        pygame.display.flip()

    def update(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            
            speed = 5

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    sys.exit()
                elif event.key == pygame.K_a:
                    self.playerSprite.dx -= speed
                elif event.key == pygame.K_d:
                    self.playerSprite.dx += speed
                elif event.key == pygame.K_w:
                    self.playerSprite.dy -= speed
                elif event.key == pygame.K_s:
                    self.playerSprite.dy += speed
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a:
                    self.playerSprite.dx += speed
                elif event.key == pygame.K_d:
                    self.playerSprite.dx -= speed
                elif event.key == pygame.K_w:
                    self.playerSprite.dy += speed
                elif event.key == pygame.K_s:
                    self.playerSprite.dy -= speed

        self.mapView.update()
        self.sprites.update()

    def loop(self):
        nextGameTick = pygame.time.get_ticks()

        renderClock = pygame.time.Clock()
        updateTick = 0

        levelMap = Map(30, 30, mapElementCB = initMapElement)

        astar = AStar(levelMap.getSquareAdjacencies,
                      lambda elem: getCost(levelMap, elem),
                      lambda x, y: getHeuristicCost(levelMap, x, y),
                      isImpassable)

        self.mapView = MapView(levelMap, self.imageCache, pygame.Rect(0, 0, 800, 600), 32, 32)
        self.playerSprite.setMapView(self.mapView)

        sprinkleMobs(10, 8, self.sprites, self.mapView, self.imageCache, self.screenRect, astar, self.playerSprite)

        while 1:
            loops = 0

            while pygame.time.get_ticks() > nextGameTick and loops < self.MaxFrameSkip:
                self.update()
                nextGameTick += self.TimePerTick
                loops += 1
                updateTick += 1
                
                if updateTick % 100 == 0:
                    print renderClock.get_fps()

            renderClock.tick()    
            self.render()
Exemplo n.º 7
0
class GameLoop (object):
    def __init__(self, ticks_per_second, max_frame_skip):
        self.TicksPerSecond = ticks_per_second
        self.TimePerTick = 1000.0 / self.TicksPerSecond # MS per tick
        self.MaxFrameSkip = max_frame_skip
        
        pygame.init()

        try:
            self.font = pygame.font.Font('VeraMono.ttf', 16)
        except:
            print 'problem loading font'
            self.font = None

        self.fontSurf = None
        self._displayFPS = False
        
        size = width, height = 800, 600
        self.black = 0,0,0
        #self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
        self.screen = pygame.display.set_mode(size)

        self.imageCache = ImageCache()
        self.imageCache.getSurface("units/Player1.png", "ball")
        self.imageCache.getSurface("units/Creature1.png", "mob")
        self.imageCache.getSurface("units/Mine1.png", "mine")
        self.imageCache.getSurface("terrains/Normal.jpg", "normal")
        self.imageCache.getSurface("terrains/Impassable5.jpg", "impassable-1")

        ball = self.imageCache.getCachedSurface("ball")
        self.mobs = pygame.sprite.Group()
        self.playerSprite = PlayerSprite(ball, width/2-16, height/2-16, self.imageCache, self.mobs)
        self.playerSpriteGroup = pygame.sprite.Group(self.playerSprite)
        self.mapView = None

    def _updateFPS(self, fps):
        self.fontSurf = self.font.render('FPS: %.3f' % fps, True, (0, 255, 0))

    def render(self):
        self.screen.fill(self.black)
        self.mapView.draw(self.screen)
        self.playerSpriteGroup.draw(self.screen)
        self.mobs.draw(self.screen)
        if self.fontSurf and self._displayFPS:
            self.screen.blit(self.fontSurf, (25, 25))
        pygame.display.flip()

    def update(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            
            speed = 15

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    sys.exit()
                elif event.key == pygame.K_a:
                    self.playerSprite.dx -= speed
                elif event.key == pygame.K_d:
                    self.playerSprite.dx += speed
                elif event.key == pygame.K_w:
                    self.playerSprite.dy -= speed
                elif event.key == pygame.K_s:
                    self.playerSprite.dy += speed
                elif event.key == pygame.K_F1:
                    self._displayFPS = not self._displayFPS
                    
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a:
                    self.playerSprite.dx += speed
                elif event.key == pygame.K_d:
                    self.playerSprite.dx -= speed
                elif event.key == pygame.K_w:
                    self.playerSprite.dy += speed
                elif event.key == pygame.K_s:
                    self.playerSprite.dy -= speed

        self.mapView.update()
        self.playerSpriteGroup.update()
        self.mobs.update()

    def loop(self):
        nextGameTick = pygame.time.get_ticks()

        renderClock = pygame.time.Clock()
        updateTick = 0

        self.levelMap = Map(mapElementCB = initMapElement)
        self.mapView = MapView(self.levelMap, self.imageCache, pygame.Rect(0, 0, 800, 600), 32, 32)
        self.playerSprite.mapView = self.mapView

        while 1:
            loops = 0

            while pygame.time.get_ticks() > nextGameTick and loops < self.MaxFrameSkip:
                self.update()
                nextGameTick += self.TimePerTick
                loops += 1
                updateTick += 1
                
                if updateTick % 10 == 0:
                    self._updateFPS(renderClock.get_fps())

            renderClock.tick()    
            self.render()