L
L
loser programmer2020-11-02 21:41:53
Python
loser programmer, 2020-11-02 21:41:53

Why do I have an error in my game code?

I am getting an error when launching the program

Traceback (most recent call last):
  File "F:/Python/KolfBloodC/game3d.py", line 24, in <module>
    drawing.world(player.pos, player.angle)
  File "F:\Python\KolfBloodC\drawing.py", line 19, in world
    ray_casting(self.sc, player_pos, player_angle, self.textures)
  File "F:\Python\KolfBloodC\raycasting.py", line 43, in ray_casting
    depth, offset, texture= (depth_v,yv , texture_v) if depth_v < depth_h else (depth_h , xh, texture_h)
UnboundLocalError: local variable 'texture_v' referenced before assignment

here is my code:
import pygame
from settings import *
from map import world_map


def mapping(a, b):
    return (a // TILE) * TILE, (b // TILE) * TILE


def ray_casting(sc, player_pos, player_angle,textures):
    ox, oy = player_pos
    xm, ym = mapping(ox, oy)
    cur_angle = player_angle - HALF_FOV
    for ray in range(NUM_RAYS):
        sin_a = math.sin(cur_angle)
        cos_a = math.cos(cur_angle)
        sin_a = sin_a if sin_a else 0.000001
        cos_a = cos_a if cos_a else 0.000001

        # verticals
        x, dx = (xm + TILE, 1) if cos_a >= 0 else (xm, -1)
        for i in range(0, WIDTH, TILE):
            depth_v = (x - ox) / cos_a
            yv = oy + depth_v * sin_a
            tile_v = mapping(x + dx , yv)
            if tile_v in world_map:
                texture_v = world_map[tile_v]
                break
            x += dx * TILE

        # horizontals
        y, dy = (ym + TILE, 1) if sin_a >= 0 else (ym, -1)
        for i in range(0, HEIGHT, TILE):
            depth_h = (y - oy) / sin_a
            xh = ox + depth_h * cos_a
            tile_h = mapping(xh , y + dy)
            if tile_h in world_map:
                texture_h = world_map[tile_h]
                break
            y += dy * TILE

        # projection
        depth, offset, texture= (depth_v,yv , texture_v) if depth_v < depth_h else (depth_h , xh, texture_h)
        offset = int(offset) % TILE
        depth *= math.cos(player_angle - cur_angle)
        depth =  max(depth , 0.00001)
        proj_height = min(int(PROJ_COEFF / depth),2 * HEIGHT)

        wall_column = texture[textures].subsurface(offset * TEXTURE_SCALE, 0, TEXTURE_SCALE, TEXTURE_HEIGHT)
        wall_column = pygame.transform.scale(wall_column, (SCALE, proj_height))
        sc.blit(wall_column, (ray * SCALE, HALF_HEIGHT - proj_height // 2))

        cur_angle += DELTA_ANGLE

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-11-02
@SoreMix

Well actually, as it is written, it is texture_vnot defined. Condition not met if tile_v in world_map

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question