Python Forum
Need help on making raycasting faster
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help on making raycasting faster
#3
ok thank you ill go do that now

Its getting faster! I just need find an alternative to "break" because the loop needs to end immediately if the statement is true. anyway here is the updated code, I changed the whole thing up a bit.
import turtle
import math
import pygame
pygame.init()
pygame.display.set_mode()
events = pygame.event.get()

colors = {1: "Blue", 2: "Mediumblue", 3: "Red", 4: "Crimson"}

# Instructions:
# UP = forward
# LEFT = left
# RIGHT = right
# DOWN = backwards
# Do not run into the walls partly because I haven't coded the collision
# detection and also partly because it displays an error right after


screen = turtle.Screen()
screen.bgcolor("Black")
turtx = 0
turty = 0
plrx = -30
plry = -30
plrA = 45

draw = turtle.Turtle()
draw.penup()
draw.pensize(6)
draw.speed(0)
draw.hideturtle()

angle = -30
distance = 0

lis = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
lis2 = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]

while True:
  for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_LEFT:
        plrA = plrA - 20
        break
      if event.key == pygame.K_RIGHT:
        plrA = plrA + 20
        break
      if event.key == pygame.K_UP:
        plrx = plrx + (math.cos(math.radians(plrA)) * 10)
        plry = plry + (math.sin(math.radians(plrA)) * 10)
        break
      if event.key == pygame.K_DOWN:
        plrx = plrx - (math.cos(math.radians(plrA)) * 10)
        plry = plry - (math.sin(math.radians(plrA)) * 10)
        break
      
  angle = -30
  for i in range(96):
    turtx = plrx
    turty = plry
    distance = 0
    for m in range(150):
      lis2[i] = 0
      # bounderies
      if (turtx > 50):
        lis2[i] = 1
        break
      if (turty < -50):
        lis2[i] = 2
        break
      if (turty > 50):
        lis2[i] = 2
        break
      if (turtx < -50):
        lis2[i] = 1
        break
      # block
      if (turtx > 30 and turtx < 31.5 and turty > -20 and turty < 20):
        lis2[i] = 3
        break
      if (turtx > 37 and turtx < 38.5 and turty > -20 and turty < 20):
        lis2[i] = 3
        break
      if (turty > 20 and turty < 21 and turtx > 30 and turtx < 38.5):
        lis2[i] = 4
        break
      if (turty < -20 and turty > -21 and turtx > 30 and turtx < 38.5):
        lis2[i] = 4
        break
      # block 2
      if (turtx > 10 and turtx < 11.5 and turty > -20 and turty < 20):
        lis2[i] = 3
        break
      if (turtx > 17 and turtx < 18.5 and turty > -20 and turty < 20):
        lis2[i] = 3
        break
      if (turty > 20 and turty < 21 and turtx > 10 and turtx < 18.5):
        lis2[i] = 4
        break
      if (turty < -20 and turty > -21 and turtx > 10 and turtx < 18.5):
        lis2[i] = 4
        break
      
      turtx = turtx + math.cos(math.radians(plrA + angle))
      turty = turty + math.sin(math.radians(plrA + angle))
      distance = distance + 1
    lis[i] = distance * math.sin(math.radians(90 - angle))
    angle += 0.625
  draw.setx(-200)
  draw.clear()
  for i in range(96):
    draw.tracer(0, 0)
    draw.color(colors.get(lis2[i]))
    draw.sety(200)
    draw.sety(-2000 / lis[i])
    draw.pendown()
    draw.sety(2000 / lis[i])
    draw.penup()
    draw.setx(draw.xcor() + 5)
    draw.color("Black")
  draw.update()
Reply


Messages In This Thread
RE: Need help on making raycasting faster - by robie972003 - Mar-10-2019, 03:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Raycasting GPS coordinates drybulkfreight 10 4,665 Aug-18-2019, 02:54 PM
Last Post: drybulkfreight

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020