raycasting.py

Created by rockingchair1196

Created on January 07, 2025

1.1 KB


from math import *
from kandinsky import *
from time import *

screen_width = 320
screen_height=222

map=[
    [1,1,1,1,1],
    [1,0,0,0,1],
    [1,0,0,0,1],
    [1,0,0,0,1],
    [1,1,1,1,1]
    ]

def sizeList(liste):
  count=0
  for i in liste:
    count+=1
  return count

def checkIn(x,y):
  ymap=-sizeList(map)/2
  for layer in map:
    xmap=-sizeList(map[0])/2
    for i in layer:
      if i==1:
        if x>xmap and x<xmap+1:
          if y>ymap and y<ymap+1:
            return True
      xmap+=1
    ymap+=1 
  return False
alr=0
for i in range(360):
  i = i * pi / 180
  addx=sin(i)
  addy=cos(i)
  rayx=0
  rayy=0
  fill_rect(int(rayx*64+screen_width/2),int(rayy*44.4+0.5*screen_height),2,2,color(0,0,255))
  touched=False
  while touched!=True:
    rayx+=addx * 0.15
    rayy+=addy * 0.15
    fill_rect(int(rayx*64+screen_width/2),int(rayy*44.4+0.5*screen_height),2,2,color(0,0,255))
    #sleep(0.01)
    if checkIn(rayx,rayy):
      alr+=1
      draw_string(str(alr),0,0)
      touched=True
      fill_rect(int(rayx*64+screen_width/2),int(rayy*44.4+0.5*screen_height),4,4,color(255,0,0))
    

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.