-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.py
More file actions
91 lines (71 loc) · 2.82 KB
/
objects.py
File metadata and controls
91 lines (71 loc) · 2.82 KB
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
import pygame
from pygame import Color
from constants import *
import random
BIRD_X = 100
GAP_SIZE = 200
X_SPEED = 4
pygame.font.init()
font = pygame.font.SysFont("Arial", 15)
class Bird:
def __init__(self, net, genome):
self.net = net
self.genome = genome
self.y = HEIGHT // 2
self.velocity = 0
self.gravity = 0.3
self.lift = -5
self.radius = 20
colors = ["red", "blue", "green", "yellow", "orange", "purple", "pink", "brown", "cyan", "magenta"]
self.color = Color(random.choice(colors))
self.time_since_last_jump = 0
def update(self):
"""Update bird's position using dt."""
self.time_since_last_jump += 1
self.velocity += self.gravity
self.velocity *= 0.9
self.y += self.velocity * 5
def die(self):
if self.genome is not None:
self.genome.fitness -= 5
def jump(self):
if self.time_since_last_jump > 10:
self.time_since_last_jump = 0
else:
return
self.velocity = 0
self.velocity += self.lift
def draw(self, win, pipes, show_input=False):
pygame.draw.circle(win, self.color, (BIRD_X, self.y), self.radius)
pygame.draw.circle(win, Color("red"), (BIRD_X, self.y), self.radius, 2)
if show_input:
# Next pipe gap top
next_pipe = pipes[0]
if next_pipe.x + next_pipe.width < BIRD_X:
next_pipe = pipes[1]
pygame.draw.line(win, Color("green"), (BIRD_X, self.y),
(next_pipe.x + next_pipe.width, next_pipe.gap_top), 2)
# Next pipe gap bottom
pygame.draw.line(win, Color("green"), (BIRD_X, self.y),
(next_pipe.x + next_pipe.width, next_pipe.gap_top + next_pipe.gap_size), 2)
class Pipe(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.x = WIDTH
self.gap_top = random.randint(50, HEIGHT - 50 - GAP_SIZE)
self.gap_size = GAP_SIZE
self.width = 50
self.color = Color("green")
def update(self):
self.x -= X_SPEED
def draw(self, win):
pygame.draw.rect(win, self.color, (self.x, 0, self.width, self.gap_top))
pygame.draw.rect(win, self.color, (self.x, self.gap_top + GAP_SIZE, self.width, HEIGHT))
# draw pipes outlines in red
pygame.draw.rect(win, Color("red"), (self.x, 0, self.width, self.gap_top), 2)
pygame.draw.rect(win, Color("red"), (self.x, self.gap_top + GAP_SIZE, self.width, HEIGHT), 2)
def check_collision(self, bird):
if BIRD_X + bird.radius > self.x and BIRD_X - bird.radius < self.x + self.width:
if bird.y - bird.radius < self.gap_top or bird.y + bird.radius > self.gap_top + GAP_SIZE:
return True
return False