-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRacetrack.py
98 lines (74 loc) · 4.15 KB
/
Racetrack.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'''
===============================================================================
ENGR 133 Fa 2020
Assignment Information
Assignment: Final Project
Author: Alec Pannunzio, afpannun@purdue.edu
Team ID: LC4-5
===============================================================================
'''
from math import sqrt,cos,sin,pi
from game_engine.RelativeSprite import RelativeSprite
from edgeDetection.Input_Output import inputImage
from edgeDetection.Grayscale import grayscale
import pygame
# the racetrack that the car drives on. Also can tell the racecar if it is off the track and where the track edges are
'''
class members:
racetrackEdges - pygame.surface - the edge-detected image showing the edges of the racetrack in white
racetrackRoad - pygame.surface - image containing the road in black and the rest in white
'''
racetrack_source = "assets/racetrack3.png"; #the filepath for the racetrack image
racetrackEdges_source = "assets/racetrack3Edges.png";
racetackRoad_source = "assets/racetrack3Road.png";
class Racetrack(RelativeSprite):
def __init__(self,objectDraw,racecar,isFirstPerson):
screenXSize = objectDraw.screenSizeX;
screenYSize = objectDraw.screenSizeY;
diagonal = sqrt(screenXSize**2 + screenYSize**2);
# load in the edges and road images
self.racetrackEdges = pygame.image.load(racetrackEdges_source).convert();
self.racetrackEdges.set_colorkey((0,0,0));
self.racetrackRoad = pygame.image.load(racetackRoad_source).convert();
self.racetrackRoad.set_colorkey((0,0,0));
#these control the size of the car
if isFirstPerson:
diagonalMultiplier = 0.002;
else:
diagonalMultiplier = 0.001;
super(Racetrack,self).__init__("racetrack", screenXSize/2,screenYSize/2,diagonal*diagonalMultiplier,racetrack_source,objectDraw);
if isFirstPerson:
self.setCamera(racecar);
# self.img = pygame.transform.rotate(self.img,90);
racecar.setRacetrack(self);
# returns true if the passed coordinate is on the track
def isOnTrack(self,x,y):
#calcululate the indices in the picture array for the passed coordinate
xPixelPos = int((x-self.xPosition+self.xSize/2)/self.imgScale);
yPixelPos = int((y-self.yPosition+self.ySize/2)/self.imgScale);
try:
color = self.racetrackRoad.get_at((xPixelPos,yPixelPos)); # get the color at that pixel position
brightness = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b; # convert to brightness
return brightness < 200; # return whether that brightness is dark enough to be the road (picture values are either 0 for on track or 254.999... for off track)
except IndexError: # we are outside the picture and definitely not on the track
return False;
# returns true if the passed coordinate is the edge of the track
def isTrackEdge(self,x,y):
#calcululate the indices in the picture array for the passed coordinate
xPixelPos = int((x-self.xPosition+self.xSize/2)/self.imgScale);
yPixelPos = int((y-self.yPosition+self.ySize/2)/self.imgScale);
try:
color = self.racetrackRoad.get_at((xPixelPos,yPixelPos)); # get the color at that pixel position
brightness = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b; # convert to brightness
return brightness > 150; # return whether that brightness is bright enough to be a track edge (picture values are either 0 for not edge or 254.999... for edge)
except IndexError: # we are outside the picture and definitely not on the edge of the track
return False;
'''
===============================================================================
ACADEMIC INTEGRITY STATEMENT
I have not used source code obtained from any other unauthorized
source, either modified or unmodified. Neither have I provided
access to my code to another. The project I am submitting
is my own original work.
===============================================================================
'''