-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_body.py
43 lines (36 loc) · 1.32 KB
/
ai_body.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
import pygame
import neat
class Robot(pygame.sprite.Sprite):
def __init__(self,id,DNA,settings,animation_frames,jump_frame,jump_sound) -> None:
super().__init__()
self.id = id
self.DNA = DNA
self.settings = settings
self.brain = neat.nn.FeedForwardNetwork.create(DNA,settings)
self.DNA.fitness = 0.0
self.animation_frames = animation_frames
self.jump_surf = jump_frame
self.rect = self.animation_frames[0].get_rect(midbottom=(100,300))
self.index = 0
self.image = self.animation_frames[self.index]
self.gravity = 0
self.jump_sound = jump_sound
self.jump_sound.set_volume(0.5)
self.output = 0.0
def animate(self):
if self.rect.bottom < 300:
self.image = self.jump_surf
return
self.image = self.animation_frames[int(self.index)]
self.index = (self.index + 0.2) % len(self.animation_frames)
def apply_gravity(self):
self.gravity += 1
self.rect.bottom += self.gravity
if self.rect.bottom > 300:
self.rect.bottom = 300
def handle_input(self,inputs):
self.output = (self.brain.activate(inputs))[0]
def update(self,inputs):
self.handle_input(inputs)
self.apply_gravity()
self.animate()