Skip to content

Commit e9e18c8

Browse files
committed
walking player
1 parent ee4e031 commit e9e18c8

File tree

4 files changed

+137
-13
lines changed

4 files changed

+137
-13
lines changed

.vscode/settings.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"python.pythonPath": "/usr/bin/python"
2+
"python.linting.pylintEnabled": false,
3+
"python.linting.flake8Enabled": true,
4+
"python.linting.enabled": true
35
}

Pipfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[source]]
2+
url = "https://pypi.python.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
pygame = "*"
8+
9+
[dev-packages]
10+
flake8 = "*"
11+
12+
[requires]
13+
python_version = "3.8"

Pipfile.lock

+98
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

raycaster.py

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pygame
22
import sys
33
from pygame.locals import *
4-
import math
4+
# import math
55

66
clock = pygame.time.Clock()
77

@@ -14,7 +14,9 @@
1414
[[300, 100], [300, 300]]
1515
]
1616

17-
ray_position = pygame.Vector2(100, 200)
17+
# ray_position = pygame.Vector2(100, 200)
18+
19+
player = pygame.Vector2(0, 0)
1820
ray_direction = pygame.Vector2(1, 0)
1921

2022

@@ -28,10 +30,10 @@ def cast_ray(walls):
2830
x2 = wall[1][0]
2931
y2 = wall[1][1]
3032

31-
x3 = ray_position.x
32-
y3 = ray_position.y
33-
x4 = (ray_position + ray_direction).x
34-
y4 = (ray_position + ray_direction).y
33+
x3 = player.x
34+
y3 = player.y
35+
x4 = (player + ray_direction).x
36+
y4 = (player + ray_direction).y
3537

3638
den = (x1-x2) * (y3-y4) - (y1-y2) * (x3-x4)
3739

@@ -52,19 +54,19 @@ def cast_ray(walls):
5254
while True:
5355

5456
screen.fill((0, 0, 0))
55-
5657
mouse = pygame.mouse.get_pos()
5758
# pygame.draw.line(screen, (255, 255, 255),
5859
# (WINDOW[0]/2, WINDOW[1]/2), mouse, 1)
5960

60-
ray_direction.update((mouse[0] - ray_position.x) - ray_direction.x,
61-
(mouse[1] - ray_position.y) - ray_direction.y)
61+
ray_direction.update((mouse[0] - player.x) - ray_direction.x,
62+
(mouse[1] - player.y) - ray_direction.y)
6263
ray_direction = ray_direction.normalize()
6364

6465
ray_distance = pygame.Vector2(
65-
ray_position.x + ray_direction.x*20, ray_position.y+ray_direction.y*20)
66+
player.x + ray_direction.x*20, player.y+ray_direction.y*20
67+
)
6668

67-
pygame.draw.line(screen, (255, 255, 0), ray_position, ray_distance)
69+
pygame.draw.line(screen, (255, 255, 0), player, ray_distance)
6870

6971
for wall in walls:
7072
pygame.draw.line(screen, (255, 255, 0), (300, 100), (300, 300))
@@ -77,7 +79,16 @@ def cast_ray(walls):
7779
(int(point.x), int(point.y)), 3, 3)
7880

7981
for event in pygame.event.get():
80-
if event.type == QUIT:
82+
if event.type == pygame.KEYDOWN:
83+
if event.key == pygame.K_w:
84+
player.y -= 5
85+
if event.key == pygame.K_s:
86+
player.y += 5
87+
if event.key == pygame.K_d:
88+
player.x += 5
89+
if event.key == pygame.K_a:
90+
player.x -= 5
91+
if event.type == pygame.QUIT:
8192
pygame.quit()
8293
sys.exit()
8394

0 commit comments

Comments
 (0)