-
Notifications
You must be signed in to change notification settings - Fork 2
/
overlay.py
92 lines (77 loc) · 3.79 KB
/
overlay.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
""" Overlay """
import random
import pygame
import grid
import text_display
from tween_colours import tween_colours
class Overlay:
""" An overlay is a grid and targeting reticle that will track around the screen """
def __init__(self, width, height):
random.seed()
self.start_colour = self.pick_new_colour()
self.target_colour = self.pick_new_colour()
self.colour = self.start_colour
self.pos = (random.randrange(0, width), random.randrange(0, height))
self.target_pos = (random.randrange(0, width), random.randrange(0, height))
self.start_pos = self.pos
self.move_time = 5
self.width = width
self.height = height
self.grid = grid.Grid(self.width, self.height)
self.history = text_display.TextDisplay((10, 10), 230, 480)
self.update_pos_text()
self.now = 0
def draw(self, surface):
""" Draw the overlay """
self.grid.draw(surface, self.colour)
pygame.draw.line(surface, self.colour, (0, self.pos[1]), (self.width, self.pos[1]))
pygame.draw.line(surface, self.colour, (self.pos[0], 0), (self.pos[0], self.height))
pygame.draw.circle(surface, self.colour, self.pos, 20, 1)
pygame.draw.line(surface, self.colour, (self.pos[0] - 10, self.pos[1] - 10), (self.pos[0] - 18, self.pos[1] - 18))
pygame.draw.line(surface, self.colour, (self.pos[0] + 10, self.pos[1] - 10), (self.pos[0] + 18, self.pos[1] - 18))
pygame.draw.line(surface, self.colour, (self.pos[0] + 10, self.pos[1] + 10), (self.pos[0] + 18, self.pos[1] + 18))
pygame.draw.line(surface, self.colour, (self.pos[0] - 10, self.pos[1] + 10), (self.pos[0] - 18, self.pos[1] + 18))
self.history.draw(surface, self.colour)
def update_pos_text(self):
""" Update the current position on the history """
self.history.update_current_text(''.join((str(self.pos[0]/10.0), ', ', str(self.pos[1]/10.0))), self.colour)
def pick_new_colour(self):
""" Generates a new random colour """
red = random.randrange(10, 255)
green = random.randrange(10, 255)
blue = random.randrange(10, 255)
alpha = random.randrange(128, 255)
if red + green + blue < 255:
selector = random.randrange(0, 2)
if selector == 0:
red = random.randrange(200, 255)
if selector == 1:
green = random.randrange(200, 255)
if selector == 2:
blue = random.randrange(200, 255)
return (red, green, blue, alpha)
def on_loop(self, update_time):
""" Update loop """
self.now += update_time
self.update_pos_text()
if self.pos == self.target_pos:
self.target_reached()
else:
if self.now > self.move_time:
self.target_reached()
else:
time_offset = self.now / self.move_time
x_pos = self.start_pos[0] + ((self.target_pos[0] - self.start_pos[0]) * time_offset)
y_pos = self.start_pos[1] + ((self.target_pos[1] - self.start_pos[1]) * time_offset)
self.colour = tween_colours(self.start_colour, self.target_colour, time_offset)
self.pos = (int(round(x_pos)), int(round(y_pos)))
def target_reached(self):
""" Set a new target for the overlay """
self.now = 0
self.pos = self.target_pos
self.start_pos = self.target_pos
self.target_pos = (random.randrange(0, self.width), random.randrange(0, self.height))
self.move_time = random.randrange(2, 5)
self.start_colour = self.target_colour
self.target_colour = self.pick_new_colour()
self.history.update_history()