-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathraycast.py
38 lines (31 loc) · 1.39 KB
/
raycast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import pygame
import math
RED = (255, 0, 0)
RAYCAST_LENGTH = 20
RAYCAST_WIDTH = 2
RAYCAST_SPEED = 5
class RayCaster(pygame.sprite.Sprite):
"""sensors that neo uses to scan objects in its area. It performs similar to the bullet class
if the ray caster collides with an object neo is notified"""
def __init__(self, positionX, positionY, dx, dy, angle):
# Call the parent class (Sprite) constructor
# the ray changes shape based on the angle it is traveling
super(RayCaster, self).__init__()
if 1 <= divmod(angle, 45)[0] < 3 or 5 <= divmod(angle, 45)[0] < 7:
self.image = pygame.Surface([RAYCAST_LENGTH, RAYCAST_WIDTH], pygame.SRCALPHA, 32)
self.rect = pygame.Rect(0, 0, RAYCAST_LENGTH, RAYCAST_WIDTH)
else:
self.image = pygame.Surface([RAYCAST_WIDTH, RAYCAST_LENGTH], pygame.SRCALPHA, 32)
self.rect = pygame.Rect(0, 0, RAYCAST_WIDTH, RAYCAST_LENGTH)
# this line makes the raycast object visible
self.image.fill(RED)
# these specify what direction the ray cast is traveling
self.dx = dx
self.dy = dy
self.rect.centerx = positionX + dx
self.rect.centery = positionY + dy
def update_movement(self):
self.rect.x += self.dx * RAYCAST_SPEED
self.rect.y += self.dy * RAYCAST_SPEED
def rotate_point(self, pivot_x, pivot_y, angle, p):
pass