Exemple #1
0
class Main(pyglet.window.Window):
    logo = pyglet.resource.image("logo.png")
    info = pyglet.resource.image("info.png")
    logo.anchor_x, logo.anchor_y = logo.width // 2, logo.height // 2
    info.anchor_x, info.anchor_y = info.width // 2, info.height // 2
    coin = pyglet.resource.media("coin.wav")

    def __init__(self):
        pyglet.window.Window.__init__(self, 600, 800)
        self.info = True
        self.logo = pyglet.sprite.Sprite(Main.logo, 300, 700)
        self.info = pyglet.sprite.Sprite(Main.info, 300, 130)
        self.story = pyglet.text.Label(x=300,
                                       y=600,
                                       anchor_x="center",
                                       width=512,
                                       multiline=True)
        self.story.text = "As The Master of Intergalactic Goatoperium, Lord Francis von Vuohi, you have stood against countless enemies as you conquered the Stars to place them under your Iron Hooves.  Blood has been spilled, but for reason, for how would those pesky ingrates learn otherwise.\n\nStill, now you again stand on the battlefield.  Those pesky rebels, usually no more than an annoyance, have gathered in millions to... 'liberate' the Goatoperium.  Ha, what a laugh.\n\nUngrateful peasants.  You gave them work, a meaning, to serve their Goatomperor!  Yet they dare oppose you still, maggots.\n\nVery well.  Let them bring thousands, bring millions!  With the guns of the greatest Warship of Goatomperium, the GOATSTAR, all will be crushed and their blood shall colour the stars themselves red.  Let them cry for mercy, only to receive none.\n\n---\n\nNow you're the big guy.  Destroy as many enemy fighters as you can before you fall."

    def update(self, dt):
        if not self.info:
            self.world.update(dt)

    def on_draw(self):
        self.clear()

        if not self.info:
            self.world.render()
        else:
            self.logo.draw()
            self.info.draw()
            self.story.draw()

    def on_key_press(self, button, modifiers):
        if self.info:
            if button == pyglet.window.key.SPACE:
                self.world = World(0)
                self.world.add("knight", 300.0, 64.0)
                self.world.add("geometry", 0, 0, w=800, h=16)
                self.world.add("player", 300.0, 700.0)

                for i in range(0, 50):
                    self.world.add("potato", 0, 0)

                self.world.init()
                self.info = False
                Main.coin.play()
            else:
                return
        if self.world.controllable:
            self.world.handleinput(button)

    def on_key_release(self, button, modifiers):
        if self.info:
            return
        if self.world.controllable:
            self.world.handleinput(button, release=True)
    def testRayIntersections(self):
        world = World()

        sphere1 = Sphere(transformation=translation(VEC_X * 2))
        sphere2 = Sphere(transformation=translation(VEC_X * 8))
        world.add(sphere1)
        world.add(sphere2)

        intersection1 = world.ray_intersection(
            Ray(origin=Point(0.0, 0.0, 0.0), dir=VEC_X))
        assert intersection1
        assert intersection1.world_point.is_close(Point(1.0, 0.0, 0.0))

        intersection2 = world.ray_intersection(
            Ray(origin=Point(10.0, 0.0, 0.0), dir=-VEC_X))

        assert intersection2
        assert intersection2.world_point.is_close(Point(9.0, 0.0, 0.0))
Exemple #3
0
def demo(width, height, angle_deg, orthogonal, pfm_output, png_output):
    image = HdrImage(width, height)

    # Create a world and populate it with a few shapes
    world = World()

    for x in [-0.5, 0.5]:
        for y in [-0.5, 0.5]:
            for z in [-0.5, 0.5]:
                world.add(
                    Sphere(transformation=translation(Vec(x, y, z)) *
                           scaling(Vec(0.1, 0.1, 0.1))))

    # Place two other balls in the bottom/left part of the cube, so
    # that we can check if there are issues with the orientation of
    # the image
    world.add(
        Sphere(transformation=translation(Vec(0.0, 0.0, -0.5)) *
               scaling(Vec(0.1, 0.1, 0.1))))
    world.add(
        Sphere(transformation=translation(Vec(0.0, 0.5, 0.0)) *
               scaling(Vec(0.1, 0.1, 0.1))))

    # Initialize a camera
    camera_tr = rotation_z(angle_deg=angle_deg) * translation(
        Vec(-1.0, 0.0, 0.0))
    if orthogonal:
        camera = OrthogonalCamera(aspect_ratio=width / height,
                                  transformation=camera_tr)
    else:
        camera = PerspectiveCamera(aspect_ratio=width / height,
                                   transformation=camera_tr)

    # Run the ray-tracer

    tracer = ImageTracer(image=image, camera=camera)

    def compute_color(ray: Ray) -> Color:
        if world.ray_intersection(ray):
            return WHITE
        else:
            return BLACK

    tracer.fire_all_rays(compute_color)

    # Save the HDR image
    with open(pfm_output, "wb") as outf:
        image.write_pfm(outf)
    print(f"HDR demo image written to {pfm_output}")

    # Apply tone-mapping to the image
    image.normalize_image(factor=1.0)
    image.clamp_image()

    # Save the LDR image
    with open(png_output, "wb") as outf:
        image.write_ldr_image(outf, "PNG")
    print(f"PNG demo image written to {png_output}")
Exemple #4
0
from view import View
from world import World
from player import Player
from controller import Controller

try:
    import psyco

    psyco.full()
    print "Psyco Running"
except ImportError:
    print "Psyco not available"

config = load(file("world.config"))
print "Creating world..."
world = World(config["world"])
print "...Done"
print "Loading player..."
player = Player(config["player"], world.start_point)
world.add(player)
print "...Done"
print "Generating View..."
view = View(player, world, config["view"])
print "...Done"
print "Loading controller..."
controller = Controller(player, world, view, config["controller"])
print "...Done"
print
print "Welcome to the Labyrinth"
controller.run()
from agents import Car, RectangleBuilding, Pedestrian, Painting
from geometry import Point
import time

human_controller = False

dt = 0.1  # time steps in terms of seconds. In other words, 1/dt is the FPS.
w = World(
    dt, width=120, height=120, ppm=6
)  # The world is 120 meters by 120 meters. ppm is the pixels per meter.

# Let's add some sidewalks and RectangleBuildings.
# A Painting object is a rectangle that the vehicles cannot collide with. So we use them for the sidewalks.
# A RectangleBuilding object is also static -- it does not move. But as opposed to Painting, it can be collided with.
# For both of these objects, we give the center point and the size.
w.add(Painting(Point(71.5, 106.5), Point(97, 27),
               'gray80'))  # We build a sidewalk.
w.add(
    RectangleBuilding(Point(72.5, 107.5), Point(95, 25))
)  # The RectangleBuilding is then on top of the sidewalk, with some margin.

# Let's repeat this for 4 different RectangleBuildings.
w.add(Painting(Point(8.5, 106.5), Point(17, 27), 'gray80'))
w.add(RectangleBuilding(Point(7.5, 107.5), Point(15, 25)))

w.add(Painting(Point(8.5, 41), Point(17, 82), 'gray80'))
w.add(RectangleBuilding(Point(7.5, 40), Point(15, 80)))

w.add(Painting(Point(71.5, 41), Point(97, 82), 'gray80'))
w.add(RectangleBuilding(Point(72.5, 40), Point(95, 80)))

# Let's also add some zebra crossings, because why not.
Exemple #6
0
lane_marker_width = 0.5
num_of_lane_markers = 50
lane_width = 3.5

w = World(
    dt, width=world_width, height=world_height, ppm=6
)  # The world is 120 meters by 120 meters. ppm is the pixels per meter.

# Let's add some sidewalks and RectangleBuildings.
# A Painting object is a rectangle that the vehicles cannot collide with. So we use them for the sidewalks / zebra crossings / or creating lanes.
# A CircleBuilding or RingBuilding object is also static -- they do not move. But as opposed to Painting, they can be collided with.

# To create a circular road, we will add a CircleBuilding and then a RingBuilding around it
cb = CircleBuilding(Point(world_width / 2, world_height / 2),
                    inner_building_radius, 'gray80')
w.add(cb)
rb = RingBuilding(
    Point(world_width / 2, world_height / 2), inner_building_radius +
    num_lanes * lane_width + (num_lanes - 1) * lane_marker_width,
    1 + np.sqrt((world_width / 2)**2 + (world_height / 2)**2), 'gray80')
w.add(rb)

# Let's also add some lane markers on the ground. This is just decorative. Because, why not.
for lane_no in range(num_lanes - 1):
    lane_markers_radius = inner_building_radius + (
        lane_no + 1) * lane_width + (lane_no + 0.5) * lane_marker_width
    lane_marker_height = np.sqrt(
        2 * (lane_markers_radius**2) * (1 - np.cos(
            (2 * np.pi) / (2 * num_of_lane_markers)))
    )  # approximate the circle with a polygon and then use cosine theorem
    for theta in np.arange(0, 2 * np.pi, 2 * np.pi / num_of_lane_markers):
Exemple #7
0
from time import sleep
from Tkinter import *
import time, threading
import random
from snake import Snake
from ptitpois import Ptitpois
from world import World

w = World()
vasya = Snake()

w.add(vasya,5, 5)
w.draw()
w.start()





from sys import argv
from node import Node
from world import World
import time

script_name, initial_strength, max_mutation, max_nodes = argv
initial_strength = int(initial_strength)
max_mutation = int(max_mutation)
max_nodes = int(max_nodes)

world = World()
world.add(Node(initial_strength, max_mutation, world))

i = 0
while world.size() < max_nodes and world.nodes_are_alive():
    print "\nROUND " + str(i) + ":"
    world.print_stats()

    for n in world.nodes():
        n.heartbeat()

    i += 1
    time.sleep(0.5)

print "\nFINAL ROUND:"
world.print_stats()
print ""
Exemple #9
0
from time import sleep
from Tkinter import *
import time, threading
import random
from snake import Snake
from ptitpois import Ptitpois
from world import World

w = World()
vasya = Snake()

w.add(vasya, 5, 5)
w.draw()
w.start()
Exemple #10
0
          # Es leuchtet blau.
          # Verstehe...
          ]


fov = 45
eye = Vec3(0, 3.3, 10)
center = Vec3(0, 3, 0)
up = Vec3(0, 1, 0)
camera = Camera(fov / 360 * pi, eye, center, up)

res = Resolution(480, 270)
view = View(res, camera)

world = World()
world.add(Sphere(Material(Vec3(1, 0, 0)), Vec3(1.41, 3, -14), 1))
world.add(Sphere(Material(Vec3(0, 1, 0)), Vec3(-1.41, 3, -14), 1))
world.add(Sphere(Material(Vec3(0, 0, 1)), Vec3(0, 5, -14), 1))
world.add(Plane(MaterialCheckerboard(Vec3(0.9, 0.9, 0.9), Vec3(0.1, 0.1, 0.1)), Vec3(0, 0, 0), Vec3(0, 1, 0)))
world.add(Triangle(Material(Vec3(1, 1, 0), reflect=0), Vec3(0, 5, -16), Vec3(2, 3, -16), Vec3(-2, 3, -16)))

# TODO fixen! Brechung funktioniert nicht und macht bei einer Glasscheibe auch wenig Sinn
# world.add(Triangle(Material(Vec3(0.6, 1, 0.6), reflect=0, transparency=0.7, refract=0), Vec3(-6, 0, -11), Vec3(-1, 0, -18), Vec3(-6, 8, -11)))
world.add(Triangle(Material(Vec3(0.6, 1, 0.6), reflect=0.7), Vec3(1, 0, -18), Vec3(6, 0, -11), Vec3(6, 8, -11)))

def trace(ray, level=0):
    if level <= MAX_DEPTH:
        hit = world.getIntersection(ray)
        if hit:
            return shade(hit, level)
    return BG_COLOR