Compare commits

3 Commits

Author SHA1 Message Date
Sem van der Hoeven
0c7e0db7ad edits 2020-07-30 15:34:28 +02:00
Sem van der Hoeven
2d5e115bea added displaying rays 2020-07-02 18:53:39 +02:00
Sem van der Hoeven
fc62c1c167 added basics for topdown map drawing 2020-07-02 18:18:21 +02:00

View File

@@ -8,8 +8,9 @@ from tkinter import *
# constants
mapWidth = 24
mapHeight = 24
screenWidth = 900 #640
screenHeight = 600 #480
screenWidth = 640
screenHeight = 480
# world map
worldMap = [
@@ -46,7 +47,7 @@ pygame.init()
pygame.font.init()
# create the screen
screen = pygame.display.set_mode((screenWidth, screenHeight))
screen = pygame.display.set_mode((screenWidth, 2 * screenHeight))
pygame.display.set_caption("Python Raycaster")
# create game clock
@@ -67,6 +68,15 @@ def switch(number: int):
}
return switcher.get(number, pygame.Color("#ffff00")) # default is yellow
def switch_default_black(number: int):
switcher = {
1: (255, 0, 0), # red
2: (0, 255, 0), # green
3: (0, 0, 255), # blue
4: (255, 255, 255), # white
5: (255,255,0) # yellow
}
return switcher.get(number, pygame.Color("#000000")) # default is yellow
def stop():
pygame.quit()
quit()
@@ -133,6 +143,31 @@ def game_intro():
pygame.display.update()
clock.tick(30)
def draw_topdown(size: int, posX: float, posY: float, dirX: float, dirY: float):
draw_y = screenHeight
draw_x = 0
for y in range(mapHeight):
for x in range(mapWidth):
square = worldMap[x][y]
# pygame.draw.rect(screen, color, (x,y,width,height), thickness)
# draw the map
pygame.draw.rect(screen, switch_default_black(square), (draw_x, draw_y, size, size), 0)
draw_x += size
draw_y += size
draw_x = 0
x_start = posX * size
y_start = posY * size + screenHeight
x_end = x_start + 2 * dirX
y_end = y_start + 2 * dirY
# player
points = [(x_start,y_start),(x_end,y_end ),(x_start+1,y_start+1),(x_end+1, y_end+1),(x_start-1,y_start-1),(x_end-1, y_end-1)]
# direction
pygame.draw.line(screen,(255, 255, 0),(x_start, y_start),(x_start + 10 * dirX, y_start + 10 * dirY), 2)
pygame.draw.lines(screen,(0, 255, 255),True,points,5)
def game_loop(clock, screen):
"""main game loop that runs the game.
@@ -150,6 +185,12 @@ def game_loop(clock, screen):
planeX = 0.0 # 2d raycaster version of camera plane x
planeY = 0.66 # 2d raycaster version of camera plane y
width = int(screenWidth / mapWidth)
print("width: ",width)
height = int(screenHeight / mapHeight)
print("heigth: ", height)
print("mapwidth: ", mapWidth)
running = True
rotateLeft = False
rotateRight = False
@@ -209,6 +250,7 @@ def game_loop(clock, screen):
# loop that goes over every x (vertical line)
screen.fill((0, 0, 0))
draw_topdown(height,posX,posY,dirX,dirY)
for x in range(screenWidth):
# calculate camera x, the x coordinate on the camera plane that the current
@@ -222,6 +264,8 @@ def game_loop(clock, screen):
# which box of the map we're in
mapX = int(posX)
mapY = int(posY)
map_ray_x = posX
map_ray_y = posY
# length of the ray from current position to next x or y side
sideDistX = 0.0 # (double)
@@ -242,8 +286,7 @@ def game_loop(clock, screen):
# if the ray direction has a negative x-component, stepX will be -1. If it has a positive x-component,
# it will be +1. If the x-component is 0 the value of stepX won't matter because it won't be used.
# same for stepY
stepX = 0 # (int)
stepY = 0 # (int)
stepX = 0 #
hit = 0 # was there a wall hit?
side = 0 # (int) was a vertical or horizontal wall hit?
@@ -273,10 +316,12 @@ def game_loop(clock, screen):
if (sideDistX < sideDistY): # the ray is going morea horizontal than vertical
sideDistX += deltaDistX
mapX += stepX
map_ray_x += stepX
side = 0
else:
sideDistY += deltaDistY
mapY += stepY
map_ray_y += stepY
side = 1
# check if the ray has hit a wall
# if it is not 0, so the square does not contain a walkable square, so a wall
@@ -309,6 +354,9 @@ def game_loop(clock, screen):
pygame.draw.line(screen, color, (x, int(drawStart)),
(x, int(drawEnd)), 1)
pygame.draw.line(screen, (255, 0, 0), (posX * height, posY * height + screenHeight), (mapX *height , mapY * height + screenHeight), 1)
if moveForward:
movePosX = int(posX + dirX * moveSpeed) # x position to move to next
movePosY = int(posY + dirY * moveSpeed) # y position to move to next