Skip to content

Commit

Permalink
Update main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxRohowsky authored Sep 30, 2021
1 parent 4824305 commit 304cb15
Showing 1 changed file with 40 additions and 54 deletions.
94 changes: 40 additions & 54 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,58 @@
import pygame
import os
import random
import math
import sys
import neat

pygame.init()
# Global Constants
SCREEN_WIDTH = 1244
SCREEN_HEIGHT = 1016

SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
TRACK = pygame.image.load(os.path.join("Assets", "trackV2.png"))

TRACK = pygame.image.load(os.path.join("Assets", "track.png"))


class Car(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.original_image = pygame.image.load(os.path.join("Assets", "oldcar.png"))
self.original_image = pygame.image.load(os.path.join("Assets", "car.png"))
self.image = self.original_image
self.rect = self.image.get_rect(center=(490, 820))
self.center = self.rect.center
self.vel_vector = pygame.math.Vector2(0.8, 0)
self.angle = 0
self.rotation_vel = 5
self.direction = 0
self.alive = True
self.radars = []

def update(self, screen):
def update(self):
self.radars.clear()
self.rotate(screen)
self.drive()
self.rotate()
for radar_angle in (-60, -30, 0, 30, 60):
self.radar(SCREEN, radar_angle)
self.radar(radar_angle)
self.collision()
self.data()
self.collision(screen)


#print(self.radar(SCREEN, 0)[0])

def drive(self):
self.rect.center += self.vel_vector*6

def collision(self, screen):
len = 40
collision_point_right = [int(self.rect.center[0] + math.cos(math.radians(360+18-self.angle))*len),
int(self.rect.center[1] + math.sin(math.radians(360+18-self.angle))*len)]
collision_point_left = [int(self.rect.center[0] + math.cos(math.radians(360-18-self.angle))*len),
int(self.rect.center[1] + math.sin(math.radians(360-18-self.angle))*len)]

# print(screen.get_at(collision_point_right))
if screen.get_at(collision_point_right) == pygame.Color(2, 105, 31, 255) \
or screen.get_at(collision_point_left) == pygame.Color(2, 105, 31, 255):
self.rect.center += self.vel_vector * 6

def collision(self):
length = 40
collision_point_right = [int(self.rect.center[0] + math.cos(math.radians(self.angle + 18)) * length),
int(self.rect.center[1] - math.sin(math.radians(self.angle + 18)) * length)]
collision_point_left = [int(self.rect.center[0] + math.cos(math.radians(self.angle - 18)) * length),
int(self.rect.center[1] - math.sin(math.radians(self.angle - 18)) * length)]

# Die on Collision
if SCREEN.get_at(collision_point_right) == pygame.Color(2, 105, 31, 255) \
or SCREEN.get_at(collision_point_left) == pygame.Color(2, 105, 31, 255):
self.alive = False
print("collision")

# Draw Collision Points - (draw this after getting the color(!) using get_at)
pygame.draw.circle(screen, (0, 255, 255, 0), collision_point_right, 4)
pygame.draw.circle(screen, (0, 255, 255, 0), collision_point_left, 4)
# Draw Collision Points
pygame.draw.circle(SCREEN, (0, 255, 255, 0), collision_point_right, 4)
pygame.draw.circle(SCREEN, (0, 255, 255, 0), collision_point_left, 4)

def rotate(self, screen):
def rotate(self):
if self.direction == 1:
self.angle -= self.rotation_vel
self.vel_vector.rotate_ip(self.rotation_vel)
Expand All @@ -70,31 +62,29 @@ def rotate(self, screen):

self.image = pygame.transform.rotozoom(self.original_image, self.angle, 0.1)
self.rect = self.image.get_rect(center=self.rect.center)
# pygame.draw.rect(screen, (255, 255, 255, 255), self.rect)

def radar(self, screen, radar_angle):
def radar(self, radar_angle):
length = 0
x = int(self.rect.center[0] + math.cos(math.radians(360-self.angle+radar_angle)) * length)
y = int(self.rect.center[1] + math.sin(math.radians(360-self.angle+radar_angle)) * length)
x = int(self.rect.center[0])
y = int(self.rect.center[1])

while not screen.get_at((x, y)) == pygame.Color(2, 105, 31, 255) and length < 200:
while not SCREEN.get_at((x, y)) == pygame.Color(2, 105, 31, 255) and length < 200:
length += 1
x = int(self.rect.center[0] + math.cos(math.radians(360-self.angle+radar_angle)) * length)
y = int(self.rect.center[1] + math.sin(math.radians(360-self.angle+radar_angle)) * length)
x = int(self.rect.center[0] + math.cos(math.radians(self.angle + radar_angle)) * length)
y = int(self.rect.center[1] - math.sin(math.radians(self.angle + radar_angle)) * length)

# Draw Radar
pygame.draw.line(screen, (255, 255, 255, 255), self.rect.center, (x, y), 1)
pygame.draw.circle(screen, (0, 255, 0, 0), (x, y), 3)
pygame.draw.line(SCREEN, (255, 255, 255, 255), self.rect.center, (x, y), 1)
pygame.draw.circle(SCREEN, (0, 255, 0, 0), (x, y), 3)

dist = int(math.sqrt(math.pow(self.rect.center[0] - x, 2)
+ math.pow(self.rect.center[1] - y, 2)))

self.radars.append([radar_angle, dist])

def data(self):
radars = self.radars
input = [0, 0, 0, 0, 0]
for i, radar in enumerate(radars):
for i, radar in enumerate(self.radars):
input[i] = int(radar[1])
return input

Expand All @@ -104,9 +94,9 @@ def remove(index):
ge.pop(index)
nets.pop(index)


def eval_genomes(genomes, config):
global cars, ge, nets
clock = pygame.time.Clock()

cars = []
ge = []
Expand All @@ -128,10 +118,6 @@ def eval_genomes(genomes, config):

SCREEN.blit(TRACK, (0, 0))

for car in cars:
car.update(SCREEN)
car.draw(SCREEN)

if len(cars) == 0:
break

Expand All @@ -141,21 +127,22 @@ def eval_genomes(genomes, config):
remove(i)

for i, car in enumerate(cars):
print(car.sprite.data())
output = nets[i].activate(car.sprite.data())
#print(output)
if output[0] > 0.7:
car.sprite.direction = -1
if output[1] > 0.7:
car.sprite.direction = 1
if output[1] > 0.7:
car.sprite.direction = -1
if output[0] <= 0.7 and output[1] <= 0.7:
car.sprite.direction = 0

clock.tick(30)
# Update
for car in cars:
car.draw(SCREEN)
car.update()
pygame.display.update()


# Setup the NEAT Neural Network
# Setup NEAT Neural Network
def run(config_path):
global pop
config = neat.config.Config(
Expand All @@ -179,4 +166,3 @@ def run(config_path):
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, 'config.txt')
run(config_path)

0 comments on commit 304cb15

Please sign in to comment.