Exemplo n.º 1
0
#!/usr/bin/python
import os
import sys
import tempfile

# Third-party modules
import pygame

# App modules
from langbots import lib

Surfaces = lib.struct("Surfaces", ["robots", "bullet", "ground"])
RobotSurfaces = lib.struct("RobotSurfaces", ["body", "turret"])

def create_screen(size, caption):
    """Create Pygame display with pixel size (width, height) and caption.""" 
    window = pygame.display.set_mode(size)
    pygame.display.set_caption(caption)
    return pygame.display.get_surface()

def load_image(filename, rotate=None):
    """Load image and return Pygame surface."""
    path = os.path.join("images", filename)
    surface = pygame.image.load(path) 
    return (pygame.transform.rotate(surface, rotate) if rotate else surface)

def draw(screen, surface, pos, angle=None):
    """Draw surface in screen at pos (x, y) with an optional rotation angle."""
    surface2 = (pygame.transform.rotate(surface, angle) if angle else surface)
    x, y = pos
    w, h = surface2.get_size()
Exemplo n.º 2
0
#!/usr/bin/python
import pygame

# App modules
from langbots import lib
from langbots import battlefield

# We need a global variable to hold the events for a loop. It will be set only 
# by the first callback, otherwise the events in the queue would be removed 
# and all subsequent callbacks would see no events. 
loop_events = {}

KeyboardControls = lib.struct("KeyboardControls", 
    ["forward", "backward", "rotate_right", "rotate_left", 
     "turret_rotate_right", "turret_rotate_left", "fire"]) 
     
def process_default_events(events): 
    """Respond to Pygame events."""
    for event in events:
        if event.type == pygame.QUIT: 
            return False              
    return True

def get_input_callback(controls, first=False):
    """Get input callback."""
    def _callback(loop_id, field, new_robot):        
        return input_callback(loop_id, field, new_robot, controls, first)
    return _callback
    
def input_callback(loop_id, field, new_robot, controls, first):
    """Process input keyboard events to move and rotate the robot."""
Exemplo n.º 3
0
#!/usr/bin/python
import time
import math
import itertools

import yaml

# App modules
from langbots import lib
from langbots import geometry

# Define struct types (using a class wrapper)
Field = lib.struct("Field", ["config", "robots", "bullets", "battle_time"])
Robot = lib.struct("Robot", ["name", "x", "y", "width", "height", "speed", 
                             "rotation", "angle", "turret_rotation", 
                             "turret_angle", "shield", "time_to_fire", 
                             "fire_angle", "turret_final_angle"])
Bullet = lib.struct("Bullet", ["x", "y", "angle", "speed", "origin"])
StateChange = lib.struct("StateChange", ["update_robots", "new_bullets"])

class AbortBattle(Exception):
    pass

def add_yaml_representers():
    def _object_representer(dumper, data):    
        mapping = dict((k, getattr(data, k)) for k in data.__slots__)
        return dumper.represent_mapping(data.__class__.__name__, mapping)
    yaml.add_representer(Field, _object_representer)
    yaml.add_representer(Robot, _object_representer)
    yaml.add_representer(Bullet, _object_representer)