Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding best score and score #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ def getNode(self, id):
def mutate(self):
if random.uniform(0, 1) < 0.8:
for i in range(0, len(self.connections)):
self.connections[i].mutate_weight()
self.connections[i].mutate_weight()
77 changes: 60 additions & 17 deletions components.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from queue import Full
import pygame
import random


class Ground:
ground_level = 500

Expand All @@ -13,6 +13,61 @@ def draw(self, window):
pygame.draw.rect(window, (255, 255, 255), self.rect)


class BestScore:
_value = 0
main_score = 0
def __init__(self):
pygame.font.init()
self.x = 330
self.y = 11
self.width = 135
self.height = 40
self.font = pygame.font.Font(None, 36) # You can adjust the font size and style here
self.text = "Best: 0" # Example text
self.rendered_text = self.font.render(self.text, True, (255, 255, 255))
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)

def draw(self, window):
pygame.draw.rect(window, (117, 25, 145), self.rect, 0, 3)
window.blit(self.rendered_text, (self.x + 10, self.y + 10)) # Adjust text position as needed

def update(self,value):
if(value >= self._value):
self._value = value
self.rendered_text = self.font.render("best: "+str(value), True, (255, 255, 255))
else:
self.rendered_text = self.font.render("best: "+str(self._value), True, (255, 255, 255))



class Ui:
_score = 0
def __init__(self):
pygame.font.init()
self.x = 170
self.y = 11
self.width = 140
self.height = 40
self.font = pygame.font.Font(None, 36) # You can adjust the font size and style here
self.text = "Score: 0" # Example text
self.rendered_text = self.font.render(self.text, True, (255, 255, 255))
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)


def draw(self, window):
pygame.draw.rect(window, (117, 25, 145), self.rect, 0, 3)
window.blit(self.rendered_text, (self.x + 10, self.y + 10)) # Adjust text position as needed

def update(self):
self._score = self._score + 0.01
self.text = "Score: "+str(round(self._score,1) )
self.rendered_text = self.font.render(self.text, True, (255, 255, 255))
print(self.text)

def reset(self):
self._score = 0
self.rendered_text = self.font.render(self.text, True, (255, 255, 255))

class Pipes:
width = 15
opening = 100
Expand All @@ -27,27 +82,15 @@ def __init__(self, win_width):

def draw(self, window):
self.bottom_rect = pygame.Rect(self.x, Ground.ground_level - self.bottom_height, self.width, self.bottom_height)
pygame.draw.rect(window, (255, 255, 255), self.bottom_rect)
pygame.draw.rect(window, (0, 128, 0), self.bottom_rect)

self.top_rect = pygame.Rect(self.x, 0, self.width, self.top_height)
pygame.draw.rect(window, (255, 255, 255), self.top_rect)
pygame.draw.rect(window, (0, 128, 0), self.top_rect)

def update(self):

self.x -= 1
if self.x + Pipes.width <= 50:
self.passed = True
if self.x <= -self.width:
self.off_screen = True













self.off_screen = True
14 changes: 7 additions & 7 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import components
import pygame

win_height = 720
win_width = 550
window = pygame.display.set_mode((win_width, win_height))

import components
win_hieth = 620
win_width = 540
window = pygame.display.set_mode((win_width,win_hieth))
ground = components.Ground(win_width)
pipes = []
pipes = []
score = components.Ui()
best_score = components.BestScore()
21 changes: 12 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import population

pygame.init()

clock = pygame.time.Clock()
population = population.Population(100)

def generate_pipes():
config.pipes.append(components.Pipes(config.win_width))

Expand All @@ -19,16 +19,18 @@ def quit_game():

def main():
pipes_spawn_time = 10

while True:
quit_game()

# Spawn level
config.window.fill((0, 0, 0))

# Spawn Ground
# Spawn Ground
config.ground.draw(config.window)

# Spawn Pipes
#spawn score panels
config.score.draw(config.window)
config.best_score.draw(config.window)

# Spawn Pipes
if pipes_spawn_time <= 0:
generate_pipes()
pipes_spawn_time = 200
Expand All @@ -37,16 +39,17 @@ def main():
for p in config.pipes:
p.draw(config.window)
p.update()

if p.off_screen:
config.pipes.remove(p)

if not population.extinct():
population.update_live_players()
config.score.update()
config.best_score.update(int(config.score._score))
else:
config.pipes.clear()
population.natural_selection()

config.score.reset()
clock.tick(60)
pygame.display.flip()

main()
12 changes: 1 addition & 11 deletions node.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,4 @@ def clone(self):
clone = Node(self.id)
clone.id = self.id
clone.layer = self.layer
return clone










return clone
35 changes: 11 additions & 24 deletions player.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import brain
import random
import pygame
import config

import brain

class Player:
def __init__(self):
Expand All @@ -15,15 +14,15 @@ def __init__(self):
self.alive = True
self.lifespan = 0

# AI
# AI
self.decision = None
self.vision = [0.5, 1, 0.5]
self.fitness = 0
self.inputs = 3
self.brain = brain.Brain(self.inputs)
self.brain.generate_net()

# Game related functions
# Game related functions
def draw(self, window):
pygame.draw.rect(window, self.color, self.rect)

Expand All @@ -37,7 +36,7 @@ def pipe_collision(self):
for p in config.pipes:
return pygame.Rect.colliderect(self.rect, p.top_rect) or \
pygame.Rect.colliderect(self.rect, p.bottom_rect)

def update(self, ground):
if not (self.ground_collision(ground) or self.pipe_collision()):
# Gravity
Expand All @@ -58,14 +57,18 @@ def bird_flap(self):
self.vel = -5
if self.vel >= 3:
self.flap = False
#AI
def think(self):
self.decision = self.brain.feed_forward(self.vision)
if self.decision > 0.73:
self.bird_flap()

@staticmethod
def closest_pipe():
for p in config.pipes:
if not p.passed:
return p

# AI related functions
def look(self):
if config.pipes:

Expand All @@ -83,12 +86,7 @@ def look(self):
self.vision[2] = max(0, self.closest_pipe().bottom_rect.top - self.rect.center[1]) / 500
pygame.draw.line(config.window, self.color, self.rect.center,
(self.rect.center[0], config.pipes[0].bottom_rect.top))

def think(self):
self.decision = self.brain.feed_forward(self.vision)
if self.decision > 0.73:
self.bird_flap()


def calculate_fitness(self):
self.fitness = self.lifespan

Expand All @@ -97,15 +95,4 @@ def clone(self):
clone.fitness = self.fitness
clone.brain = self.brain.clone()
clone.brain.generate_net()
return clone











return clone
13 changes: 1 addition & 12 deletions population.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,4 @@ def extinct(self):
for p in self.players:
if p.alive:
extinct = False
return extinct











return extinct
14 changes: 2 additions & 12 deletions species.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, player):
def similarity(self, brain):
similarity = self.weight_difference(self.benchmark_brain, brain)
return self.threshold > similarity

@staticmethod
def weight_difference(brain_1, brain_2):
total_weight_difference = 0
Expand Down Expand Up @@ -50,14 +50,4 @@ def calculate_average_fitness(self):
def offspring(self):
baby = self.players[random.randint(1, len(self.players)) - 1].clone()
baby.brain.mutate()
return baby










return baby