Example #1
0
def userInputToPlayer(player):
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        player.moveForward()
    if keys[pygame.K_DOWN]:
        player.moveBackward()
    if keys[pygame.K_LEFT]:
        player.rotateLeft()
    if keys[pygame.K_RIGHT]:
        player.rotateRight()
    if keys[pygame.K_a]:
        player.accelerate()
    if keys[pygame.K_d]:
        player.brake()
Example #2
0
    def __game_cycle(self):
        p = self.player
        l = self.level

        p.travel(l.track_length(), self.window)

        base_segment = l.find_segment(p.position)
        player_segment = l.find_segment(p.position + s.PLAYER_Z)

        p.accelerate()
        p.steer(player_segment)
        p.climb(base_segment)
        p.detect_collisions(player_segment)
        p.handle_crash()

        # Sprinkle some random bonuses into the next lap if we are lucky.
        if p.new_lap:
            if random.randint(1, s.CHANCE_OF_BONUSES) == 1:
                l.insert_bonuses()

        # Move the other players.
        for c in l.competitors:
            old_seg = l.find_segment(c.position)
            c.travel(l.track_length())
            new_seg = l.find_segment(c.position)

            c.play_engine(p.position)

            if old_seg.index != new_seg.index:
                if c in old_seg.competitors:
                    old_seg.competitors.remove(c)
                new_seg.competitors.append(c)

        coverage = [base_segment, base_segment, base_segment]
        tunnel_exit = base_segment
        pre_renders = []
        curve = 0
        curve_delta = -(base_segment.curve * p.segment_percent())

        # Position backgrounds according to current curve.
        for bg in l.backgrounds:
            if base_segment.curve != 0:
                bg.step(base_segment.curve, p.speed_percent())
            bg.render(self.window)

        # Loop through segments we should draw for this frame.
        for i in range(s.DRAW_DISTANCE):
            segment = l.offset_segment(base_segment.index + i)
            projected_position = p.position
            camera_x = p.x * s.ROAD_WIDTH

            # Past end of track and looped back.
            if segment.index < base_segment.index:
                projected_position -= l.track_length()

            segment.project(camera_x, curve, curve_delta, projected_position,
                            p.y)

            curve += curve_delta
            curve_delta += segment.curve

            # Remember biggest LEFT, TOP, RIGHT coordinates so we can clip sprites later.
            segment.clip = [
                coverage[0].bottom["screen"]["x"] -
                coverage[0].bottom["screen"]["w"],
                coverage[1].top["screen"]["y"],
                coverage[2].bottom["screen"]["x"] +
                coverage[2].bottom["screen"]["w"]
            ]

            if len(segment.pre_polygons) > 0:
                pre_renders.append(segment)

            if segment.tunnel_end:
                tunnel_exit = segment

            if segment.should_ignore(coverage[1]):
                continue

            segment.render_grass(self.window)
            segment.render_road(self.window)

            if (segment.top["screen"]["y"] > coverage[1].top["screen"]["y"]):
                coverage[1] = segment

            # Remember where we should draw the left and right tunnel walls.
            if segment.in_tunnel:
                s_top = segment.top["screen"]
                tl_top = coverage[0].top["screen"]
                tr_top = coverage[2].top["screen"]

                if (s_top["x"] - s_top["w"]) > (tl_top["x"] - tl_top["w"]):
                    coverage[0] = segment

                if (s_top["x"] + s_top["w"]) < (tr_top["x"] + tr_top["w"]):
                    coverage[2] = segment

        # Draw tunnel roof and walls.
        if base_segment.in_tunnel:
            self.player.in_tunnel = True

            tunnel_exit.render_tunnel_roof(self.window,
                                           coverage[1].top["screen"]["y"])
            coverage[0].render_left_tunnel(self.window)
            coverage[2].render_right_tunnel(self.window)
        else:
            self.player.in_tunnel = False

        # Let backgrounds know how much height they need to cover on the next paint.
        for bg in l.backgrounds:
            bg.visible_height = s.DIMENSIONS[1] - coverage[1].top["screen"]["y"]

        # Draw sprites in from back to front (painters algorithm).
        for segment in reversed(pre_renders):
            segment.render_polygons(self.window, coverage)

        for i in reversed(range(1, s.DRAW_DISTANCE)):
            segment = l.offset_segment(base_segment.index + i)
            segment.render_world_objects(self.window)

        p.render(self.window, base_segment)
        p.render_hud(self.window)

        if p.blood_alpha > 0:
            p.render_blood(self.window)

        for e in pygame.event.get():
            u.try_quit(e)

            if e.type == pygame.KEYDOWN and e.key == pygame.K_RETURN:
                pygame.mixer.music.pause()
                self.paused = True

        # Steering, acceleration.
        keys = pygame.key.get_pressed()
        p.set_acceleration(keys)
        p.set_direction(keys)

        gameplay = np.flip(pygame.surfarray.pixels3d(self.window), axis=(0, 2))
        gameplay = np.rot90(gameplay, k=-1)
        ret, gameplay = cv2.imencode('.jpg', gameplay)

        ret, video = self.cam.read()
        video = cv2.resize(video, s.DIMENSIONS)
        ret, video = cv2.imencode('.jpg', video)

        publish.single("gameplay",
                       gameplay.tobytes(),
                       hostname="mr4b11zrabb.messaging.mymaas.net",
                       port=1883,
                       auth={
                           'username': "******",
                           'password': "******"
                       },
                       transport="tcp")
        publish.single("video",
                       video.tobytes(),
                       hostname="mr4b11zrabb.messaging.mymaas.net",
                       port=1883,
                       auth={
                           'username': "******",
                           'password': "******"
                       },
                       transport="tcp")
Example #3
0
    def __game_cycle(self):
        p = self.player
        l = self.level

        p.travel(l.track_length(), self.window)

        base_segment   = l.find_segment(p.position)
        player_segment = l.find_segment(p.position + s.PLAYER_Z)

        p.accelerate()
        p.steer(player_segment)
        p.climb(base_segment)
        p.detect_collisions(player_segment)
        p.handle_crash()

        # Sprinkle some random bonuses into the next lap if we are lucky.
        if p.new_lap:
            if random.randint(1, s.CHANCE_OF_BONUSES) == 1:
                l.insert_bonuses()

        # Move the other players.
        for c in l.competitors:
            old_seg = l.find_segment(c.position)
            c.travel(l.track_length())
            new_seg = l.find_segment(c.position)

            c.play_engine(p.position)

            if old_seg.index != new_seg.index:
                if c in old_seg.competitors:
                    old_seg.competitors.remove(c)
                new_seg.competitors.append(c)

        coverage    = [base_segment, base_segment, base_segment]
        tunnel_exit = base_segment
        pre_renders = []
        curve       = 0
        curve_delta = -(base_segment.curve * p.segment_percent())

        # Position backgrounds according to current curve.
        for bg in l.backgrounds:
            if base_segment.curve != 0:
                bg.step(base_segment.curve, p.speed_percent())
            bg.render(self.window)

        # Loop through segments we should draw for this frame.
        for i in range(s.DRAW_DISTANCE):
            segment            = l.offset_segment(base_segment.index + i)
            projected_position = p.position
            camera_x           = p.x * s.ROAD_WIDTH

            # Past end of track and looped back.
            if segment.index < base_segment.index:
                projected_position -= l.track_length()

            segment.project(camera_x,
              curve,
              curve_delta,
              projected_position,
              p.y)

            curve       += curve_delta
            curve_delta += segment.curve

            # Remember biggest LEFT, TOP, RIGHT coordinates so we can clip sprites later.
            segment.clip = [
              coverage[0].bottom["screen"]["x"] - coverage[0].bottom["screen"]["w"],
              coverage[1].top["screen"]["y"],
              coverage[2].bottom["screen"]["x"] + coverage[2].bottom["screen"]["w"]]

            if len(segment.pre_polygons) > 0:
                pre_renders.append(segment)

            if segment.tunnel_end:
                tunnel_exit = segment

            if segment.should_ignore(coverage[1]):
                continue

            segment.render_grass(self.window)
            segment.render_road(self.window)

            if (segment.top["screen"]["y"] > coverage[1].top["screen"]["y"]):
                coverage[1] = segment

            # Remember where we should draw the left and right tunnel walls.
            if segment.in_tunnel:
                s_top  = segment.top["screen"]
                tl_top = coverage[0].top["screen"]
                tr_top = coverage[2].top["screen"]
                
                if (s_top["x"] - s_top["w"]) > (tl_top["x"] - tl_top["w"]):
                    coverage[0] = segment

                if (s_top["x"] + s_top["w"]) < (tr_top["x"] + tr_top["w"]):
                    coverage[2] = segment

        # Draw tunnel roof and walls.
        if base_segment.in_tunnel:
            self.player.in_tunnel = True

            tunnel_exit.render_tunnel_roof(self.window, coverage[1].top["screen"]["y"])
            coverage[0].render_left_tunnel(self.window)
            coverage[2].render_right_tunnel(self.window)
        else:
            self.player.in_tunnel = False

        # Let backgrounds know how much height they need to cover on the next paint.
        for bg in l.backgrounds:
            bg.visible_height = s.DIMENSIONS[1] - coverage[1].top["screen"]["y"]

        # Draw sprites in from back to front (painters algorithm).
        for segment in reversed(pre_renders):
            segment.render_polygons(self.window, coverage)

        for i in reversed(range(1, s.DRAW_DISTANCE)):
            segment = l.offset_segment(base_segment.index + i)
            segment.render_world_objects(self.window)

        p.render(self.window, base_segment)
        p.render_hud(self.window)

        if p.blood_alpha > 0:
            p.render_blood(self.window)

        for e in pygame.event.get():
            u.try_quit(e)

            if e.type == pygame.KEYDOWN and e.key == pygame.K_RETURN:
                pygame.mixer.music.pause()
                self.paused = True

        # Steering, acceleration.
        keys = pygame.key.get_pressed()
        p.set_acceleration(keys)
        p.set_direction(keys)
Example #4
0
    def __game_cycle(self):

        #       rospy.init_node('listener', anonymous=True)
        rospy.Subscriber("/bender/joy/joy0", Joy, callback)

        global pausa

        p = self.player
        l = self.level

        resp = p.travel(l.track_length(), self.window)

        if resp == "exit":
            return resp

        base_segment = l.find_segment(p.position)
        player_segment = l.find_segment(p.position + s.PLAYER_Z)

        p.accelerate()
        p.steer(player_segment)
        p.climb(base_segment)
        p.detect_collisions(player_segment)
        p.handle_crash()

        # Sprinkle some random bonuses into the next lap if we are lucky.
        if p.new_lap:
            if random.randint(1, s.CHANCE_OF_BONUSES) == 1:
                l.insert_bonuses()

        # Move the other players.
        for c in l.competitors:
            old_seg = l.find_segment(c.position)
            c.travel(l.track_length())
            new_seg = l.find_segment(c.position)

            c.play_engine(p.position)

            if old_seg.index != new_seg.index:
                if c in old_seg.competitors:
                    old_seg.competitors.remove(c)
                new_seg.competitors.append(c)

        coverage = [base_segment, base_segment, base_segment]
        tunnel_exit = base_segment
        pre_renders = []
        curve = 0
        curve_delta = -(base_segment.curve * p.segment_percent())

        # Position backgrounds according to current curve.
        for bg in l.backgrounds:
            if base_segment.curve != 0:
                bg.step(base_segment.curve, p.speed_percent())
            bg.render(self.window)

        # Loop through segments we should draw for this frame.
        for i in range(s.DRAW_DISTANCE):
            segment = l.offset_segment(base_segment.index + i)
            projected_position = p.position
            camera_x = p.x * s.ROAD_WIDTH

            # Past end of track and looped back.
            if segment.index < base_segment.index:
                projected_position -= l.track_length()

            segment.project(camera_x, curve, curve_delta, projected_position,
                            p.y)

            curve += curve_delta
            curve_delta += segment.curve

            # Remember biggest LEFT, TOP, RIGHT coordinates so we can clip sprites later.
            segment.clip = [
                coverage[0].bottom["screen"]["x"] -
                coverage[0].bottom["screen"]["w"],
                coverage[1].top["screen"]["y"],
                coverage[2].bottom["screen"]["x"] +
                coverage[2].bottom["screen"]["w"]
            ]

            if len(segment.pre_polygons) > 0:
                pre_renders.append(segment)

            if segment.tunnel_end:
                tunnel_exit = segment

            if segment.should_ignore(coverage[1]):
                continue

            segment.render_grass(self.window)
            segment.render_road(self.window)

            if (segment.top["screen"]["y"] > coverage[1].top["screen"]["y"]):
                coverage[1] = segment

            # Remember where we should draw the left and right tunnel walls.
            if segment.in_tunnel:
                s_top = segment.top["screen"]
                tl_top = coverage[0].top["screen"]
                tr_top = coverage[2].top["screen"]

                if (s_top["x"] - s_top["w"]) > (tl_top["x"] - tl_top["w"]):
                    coverage[0] = segment

                if (s_top["x"] + s_top["w"]) < (tr_top["x"] + tr_top["w"]):
                    coverage[2] = segment

        # Draw tunnel roof and walls.
        if base_segment.in_tunnel:
            self.player.in_tunnel = True

            tunnel_exit.render_tunnel_roof(self.window,
                                           coverage[1].top["screen"]["y"])
            coverage[0].render_left_tunnel(self.window)
            coverage[2].render_right_tunnel(self.window)
        else:
            self.player.in_tunnel = False

        # Let backgrounds know how much height they need to cover on the next paint.
        for bg in l.backgrounds:
            bg.visible_height = s.DIMENSIONS[1] - coverage[1].top["screen"]["y"]

        # Draw sprites in from back to front (painters algorithm).
        for segment in reversed(pre_renders):
            segment.render_polygons(self.window, coverage)

        for i in reversed(range(1, s.DRAW_DISTANCE)):
            segment = l.offset_segment(base_segment.index + i)
            segment.render_world_objects(self.window)

        p.render(self.window, base_segment)
        #        p.render_hud(self.window)

        resp = p.render_hud(self.window)
        #print resp
        if resp == "over":
            return resp

        if resp == "win":
            return resp

        if p.blood_alpha > 0:
            p.render_blood(self.window)

        for e in pygame.event.get():
            u.try_quit(e)

            if e.type == pygame.KEYDOWN and e.key == pygame.K_RETURN:
                pygame.mixer.music.pause()
                self.paused = True

        if pausa == 1:
            pygame.mixer.music.pause()
            self.paused = True

        pausa = 0

        # Steering, acceleration.
        keys = pygame.key.get_pressed()
        p.set_acceleration(keys)
        p.set_direction(keys)