Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ray_casting_with_turtle.py ZZZ
- import turtle
- from math import *
- t=turtle.Turtle()
- turtle.speed(0) # 5000
- turtle.bgcolor("black")
- loc=turtle.Turtle()
- px=float(5.5)
- py=float(5.5)
- pa=float(0)
- sw=int(120)
- sh=int(40)
- my=int(16)
- mx=int(16)
- fov=float(0.15)
- fd=float(10)
- tx=0
- turtle.setworldcoordinates(-300,200,-72,0)
- hit=False
- t.pu()
- t.pensize(7)
- t.goto(-300,200)
- t.right(90)
- t.pd()
- ty=0
- t.pendown()
- turtle.tracer(120)
- h=float(0)
- loc.goto(5.5,5.5)
- loc.penup()
- loc.ht()
- t.ht()
- ma1 = ['x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x',
- 'x','.','.','.','.','.','.','.','.','.','.','.','.','.','.','x',
- 'x','.','.','.','.','.','.','.','.','.','.','.','.','.','.','x',
- 'x','.','.','.','.','.','.','.','.','.','x','x','x','x','.','x',
- 'x','.','.','.','.','.','.','.','.','.','.','.','.','.','.','x',
- 'x','.','.','.','.','.','.','.','.','.','.','.','.','.','.','x',
- 'x','x','x','.','.','.','.','.','.','.','.','.','.','.','.','x',
- 'x','x','.','.','.','.','.','.','.','.','.','.','.','.','.','x',
- 'x','.','.','.','.','.','.','.','.','.','x','.','.','x','x','x',
- 'x','.','.','.','.','.','.','.','.','.','x','.','.','.','.','x',
- 'x','.','.','.','.','x','.','.','.','.','x','.','.','.','.','x',
- 'x','.','.','.','.','x','.','.','.','.','.','.','.','.','.','x',
- 'x','.','.','.','.','x','.','.','.','.','.','.','.','.','.','x',
- 'x','.','.','.','.','x','.','.','.','.','.','.','.','.','.','x',
- 'x','.','.','.','.','.','.','.','.','.','.','.','.','.','.','x',
- 'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x']
- turtle.tracer(0,1)
- wn = turtle.Screen()
- keyboard = ''
- def up():
- global keyboard
- keyboard = 'U'
- def down():
- global keyboard
- keyboard = 'D'
- def left():
- global keyboard
- keyboard = 'L'
- def right():
- global keyboard
- keyboard = 'R'
- wn.listen()
- wn.onkey(up, 'Up')
- wn.onkey(down, 'Down')
- wn.onkey(left, 'Left')
- wn.onkey(right, 'Right')
- def everything():
- global hit
- global py
- global px
- global pa
- global keyboard
- while True:
- for i in range (int(sw)):
- if keyboard == 'L':
- loc.left(0.1)
- elif keyboard == 'R':
- loc.right(0.1)
- elif keyboard == 'U':
- loc.backward(0.01)
- if (ma1[int(px-0.1) * mx + int(py-0.1)] == 'x'):
- loc.backward(-0.01)
- elif keyboard == 'D':
- loc.forward(0.01)
- if (ma1[int(px-0.1) * mx + int(py-0.1)] == 'x'):
- loc.forward(-0.01)
- ra=float(((pa-fov/2)+(i)/(mx)*fov))
- ex=float(sin(ra))
- ey=float (cos(ra))
- dis=float (0)
- while hit ==False and dis< fd:
- dis=dis+0.1
- tx=int(px+ex*dis)
- ty=int(py+ey*dis)
- if tx<0 or tx>=mx or ty<0 or ty>= my:
- hit=True
- else:
- if(ma1[(tx*mx+ty)]=="x"):
- hit=True
- if dis>= fd:
- hit=True
- if hit ==True:
- if keyboard in 'UDLR':
- t.color("black")
- h=40-(dis*4)
- t.forward((((40-h)/2)*5))
- turtle.colormode(255)
- t.pencolor((255-(int(dis*1.2*12.75))),0,0)
- t.forward((h)*5)
- t.color("black")
- t.fd((((40-h)/2))*5)
- t.pu()
- t.goto((-300)+i*2,200)
- t.pd()
- dis=0
- hit=False
- px=loc.pos()[1]
- py=loc.pos()[0]
- pi=22/7
- radian = (loc.heading()-40)*(pi/180)
- pa=radian
- if keyboard: print keyboard
- keyboard = ''
- turtle.update()
- while True:
- turtle.ontimer(everything(),0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement