Exemplo n.º 1
0
 def check_events(self, events):
     for e in events:
         # Resize
         if e.type == VIDEORESIZE:
             c.resize(e.w, e.h)
             pg.display.get_surface().fill(c.BACKGROUND)
         # Key pressed
         elif e.type == KEYDOWN:
             # Delete last character
             if e.key == K_BACKSPACE:
                 self.input = self.input[:-1]
             elif e.key == K_SPACE:
                 self.input += " "
             # Add single character
             elif len(pg.key.name(e.key)) == 1 and \
                     (self.char_limit == -1 or len(self.input) < self.char_limit):
                 self.input += e.unicode
         # Clicks
         elif e.type == MOUSEBUTTONUP and e.button == BUTTON_LEFT:
             pos = pg.mouse.get_pos()
             if self.rect.collidepoint(pos):
                 pos = (pos[0] - self.rect.x, pos[1] - self.rect.y)
                 if self.done_rect.collidepoint(
                         *pos) and len(self.input) > 0:
                     return self.input
                 elif self.quit_rect.collidepoint(*pos):
                     return ""
     self.redraw()
Exemplo n.º 2
0
 def check_events(self, events):
     for e in events:
         if e.type == VIDEORESIZE:
             c.resize(e.w, e.h)
         elif e.type == MOUSEBUTTONUP and e.button == BUTTON_LEFT:
             pos = pg.mouse.get_pos()
             pos = (pos[0] - self.rect.x, pos[1] - self.rect.y)
             if self.yes_rect.collidepoint(*pos):
                 return True
             elif self.no_rect.collidepoint(*pos):
                 return False
     self.redraw()
Exemplo n.º 3
0
    def run(self):
        self.load_files()

        self.resize()
        while True:
            events = []
            for e in pg.event.get():
                if e.type == QUIT:
                    return False
                elif e.type == VIDEORESIZE:
                    c.resize(e.w, e.h)
                    self.resize()
                else:
                    events.append(e)
            result = self.handle_events(events)
            if result is not None:
                return result
            self.draw()
            pg.display.flip()
Exemplo n.º 4
0
    def check_events(self, events):
        for e in events:
            # Check if we hit exit
            if self.can_exit and e.type == QUIT:
                return False
            # Check if we resized the screen
            elif e.type == VIDEORESIZE:
                c.resize(e.w, e.h)

        # Perform the task
        self.progress = self.task(self.progress, *self.task_args)
        if self.progress is None:
            return False

        if self.draw_ui:
            # Update ui
            self.draw_ui(self.progress, *self.draw_args)

        if self.progress >= 1:
            return True
Exemplo n.º 5
0
def tick():
    # Update game time and get time since last tick
    global game_time, dt, d_mouse
    dt = time() - game_time
    game_time += dt
    d_mouse = pg.mouse.get_rel()

    # Check events
    events = pg.event.get()
    for e in events:
        if e.type == QUIT:
            return False
        elif e.type == VIDEORESIZE:
            resize(e.w, e.h)
            player.on_resize()
            events.remove(e)

    # Update every animation
    for a in animations:
        a.update(dt)

    # Update world
    world.tick(dt)

    # Update the player
    if player.map_open:
        player.run_map(events)
    else:
        player.run_main(events)

    # Spawn entities
    handler.spawn()
    # Move entities, dropped items, and projectiles
    # Also checks collisions and attacks
    handler.move(player)

    draw()
    return True
Exemplo n.º 6
0
import os
from sys import exit
from Tools import game_vars
from Tools.constants import load_fonts, resize, MIN_W, MIN_H

# Make sure all necessary folders exist
folders = ["res", "res/items", "res/items", "res/entities",
           "saves", "saves/players/", "saves/universes/"]
for folder in folders:
    if not os.path.exists(folder):
        os.makedirs(folder)

# Do any initialization of variables and such
pg.init()
load_fonts()
resize(MIN_W, MIN_H)

game_vars.init()

clock = pg.time.Clock()

while True:
    result = False
    try:
        result = game_vars.tick()
    except:
        print("Crashed", print_exc())

    if not result:
        game_vars.close_world()
        pg.quit()