summaryrefslogtreecommitdiffstats
path: root/Lasertag/laser_tag.py
blob: fa6724ced979a7d1c7c6fac0030ef1d68658cf6b (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import pygame
import math
import time
import random


def format_number(num):
    if num < 1000:
        return num

    out = ""
    for i, val in enumerate((str(num)[::-1])):
        out += val
        if (i + 1) % 3 == 0 and i != len(str(num)) - 1:
            out += ","
    out = out[::-1]

    return out


def deg2rad(angle):  # not accurate
    pi = 3.14
    return angle * pi / 180


class Label:
    horizontal_padding = 5
    vertical_padding = 5
    outline_thickness = 2

    def __init__(self, x, y, text, font_size, text_colour=(0, 0, 0), antialias=True, fill_colour=(150, 150, 150),
                 outline_colour=(0, 0, 0)):
        self.position = pygame.Vector2(x, y)
        self.font = pygame.font.Font("MontserratBlack-ZVK6J.otf", font_size)
        self.text_colour = text_colour
        self.antialias = antialias
        self.fill_colour = fill_colour
        self.outline_colour = outline_colour
        self.text, self.width, self.height = self.create_text(text)
        self.centering = pygame.Vector2(0, 0)

    def create_text(self, text):
        out = []
        max_width = 0
        max_height = 0
        for line in text:
            text_surface = self.font.render(line, self.antialias, self.text_colour)
            width = text_surface.get_width()
            height = text_surface.get_height()
            out.append((text_surface, width, height))
            max_width = max(max_width, width)
            max_height += height
        return out, max_width, max_height

    def update(self, delta, new_text=None, x=None, y=None, width=None, height=None, colour=None):
        if new_text is not None:
            self.text, self.width, self.height = self.create_text(new_text)
        if x is not None:
            self.position.x = x
        if y is not None:
            self.position.y = y
        if width is not None:
            self.width = width
        if height is not None:
            self.height = height
        if colour is not None:
            self.fill_colour = colour

    def draw(self, surface, centered_x=True, centered_y=True, outlined=False, filled=False):
        if centered_x:
            self.centering.x = self.width / 2
        if centered_y:
            self.centering.y = self.height / 2
        if filled:
            r = pygame.Rect(
                self.position - pygame.Vector2(self.horizontal_padding, self.vertical_padding) - self.centering,
                pygame.Vector2(self.width + 2 * self.horizontal_padding,
                               self.height + self.vertical_padding * 2))
            pygame.draw.rect(surface, self.fill_colour, r)
        if outlined:
            r = pygame.Rect(
                self.position - pygame.Vector2(self.horizontal_padding, self.vertical_padding) - self.centering,
                pygame.Vector2(self.width + 2 * self.horizontal_padding,
                               self.height + self.vertical_padding * 2))
            pygame.draw.rect(surface, self.outline_colour, r, self.outline_thickness)

        prev_height = 0
        for line in self.text:
            text_surface, width, height = line
            surface.blit(text_surface, self.position - self.centering + pygame.Vector2(0, prev_height))
            prev_height += height


class Button(Label):

    def __init__(self, x, y, text, font_size, text_colour=(0, 0, 0), antialias=True, fill_color=(150, 150, 150),
                 outline_colour=(0, 0, 0)):
        super().__init__(x, y, text, font_size, text_colour=text_colour, antialias=antialias, fill_colour=fill_color,
                         outline_colour=outline_colour)

    def is_touching_mouse_pointer(self):
        mouse_x, mouse_y = pygame.mouse.get_pos()
        if (self.position.x - self.centering.x <= mouse_x <= self.position.x + self.width - self.centering.x
                and self.position.y - self.centering.y <= mouse_y <= self.position.y + self.height - self.centering.y):
            return True
        return False

    def lighten(self):
        self.fill_colour = (100, 100, 100)

    def darken(self):
        self.fill_colour = (150, 150, 150)

    def update(self, delta, new_text=None, x=None, y=None, width=None, height=None, colour=None):
        super().update(delta, new_text=new_text, x=x, y=y, width=width, height=height, colour=colour)
        if self.is_touching_mouse_pointer():
            self.lighten()
        else:
            self.darken()

    def run_function(self, function):
        return function()


class StationaryLaser:
    colour = (255,0,0)
    thickness = 10
    radius = 30

    def __init__(self,x,y,rotation):
        self.position = pygame.Vector2(x,y)
        self.rotation = rotation
        self.endpoint = pygame.Vector2(0,0)

    def update(self,delta):
        self.project_laser()

    def project_laser(self):
        laser_skip_step_amount = 10
        direction = pygame.Vector2(math.cos(deg2rad(self.rotation)), math.sin(deg2rad(self.rotation))) * laser_skip_step_amount
        new_pos = self.position
        
        while

    def draw(self,surface):
        pygame.draw.circle(surface, (255,255,255), self.position, self.radius, 10)

class Wall:
    outline_colour = (40, 40, 40)
    outline_thickness = 3

    def __init__(self, x, y, width, height, colour, centered_x=True, centered_y=True, outlined=True):
        self.width = width
        self.height = height
        self.centering = pygame.Vector2(0, 0)
        if centered_x:
            self.centering.x = self.width / 2
        if centered_y:
            self.centering.y = self.height / 2
        self.position = pygame.Vector2(x, y) - self.centering
        self.fill_colour = colour
        self.outlined = outlined
        self.collision_rect = pygame.Rect(self.position, pygame.Vector2(self.width, self.height))

    def update(self, delta, x=None, y=None, width=None, height=None, colour=None):
        if x is not None:
            self.position.x = x
        if y is not None:
            self.position.y = y
        if width is not None:
            self.width = width
        if height is not None:
            self.height = height
        if colour is not None:
            self.fill_colour = colour

    def draw(self, surface):
        pygame.draw.rect(surface, self.fill_colour, self.collision_rect)
        if self.outlined:
            pygame.draw.rect(surface, self.outline_colour, self.collision_rect, self.outline_thickness)


class Player:
    width = 70
    height = 70
    move_strength = 0.35
    player_image = pygame.image.load("player.png")
    player_image = pygame.transform.scale(player_image, (width, height))
    centering = pygame.Vector2(width, height)

    def __init__(self, x, y):
        self.position = pygame.Vector2(x, y)
        self.velocity = pygame.Vector2(0, 0)
        self.collision_rect = pygame.Rect(self.position.x, self.position.y, self.width, self.height)
        self.position -= self.centering

    def movement(self):
        pressed = pygame.key.get_pressed()
        direction = pygame.Vector2(0, 0)
        if pressed[pygame.K_RIGHT] or pressed[pygame.K_d]:
            direction.x += 1
        if pressed[pygame.K_LEFT] or pressed[pygame.K_a]:
            direction.x -= 1
        if pressed[pygame.K_UP] or pressed[pygame.K_w]:
            direction.y -= 1
        if pressed[pygame.K_DOWN] or pressed[pygame.K_s]:
            direction.y += 1
        if direction.x == 0 and direction.y == 0:
            return direction
        return direction.normalize()

    def update(self, delta):
        self.sort_collision(delta)

        self.velocity = self.movement() * self.move_strength

    def sort_collision(self, delta):
        self.collision_rect = pygame.Rect(self.position.x + self.velocity.x * delta, self.position.y, self.width,
                                          self.height)
        if not self.check_collision():
            self.position.x += self.velocity.x * delta

        self.collision_rect = pygame.Rect(self.position.x, self.position.y + self.velocity.y * delta, self.width,
                                          self.height)
        if not self.check_collision():
            self.position.y += self.velocity.y * delta

    def check_collision(self):
        for wall in walls:
            if self.collision_rect.colliderect(wall.collision_rect):
                return True
        return False

    def draw(self, surface):
        surface.blit(self.player_image, self.position)


pygame.init()

SCREEN_HEIGHT = 640
SCREEN_WIDTH = 1080
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

pygame.display.set_caption("Laser Tag")
# icon = pygame.transform.scale(Cookie.cookie_image, (32, 32))
# pygame.display.set_icon(icon)

player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
walls = [Wall(SCREEN_WIDTH * 3 / 4, SCREEN_HEIGHT / 2, 15, SCREEN_HEIGHT / 3, (255, 255, 255)),
         Wall(SCREEN_WIDTH * 1 / 4, SCREEN_HEIGHT / 2, 15, SCREEN_HEIGHT / 3, (255, 255, 255)),
         Wall(SCREEN_WIDTH / 2, SCREEN_HEIGHT * 3 / 4, SCREEN_WIDTH / 3, 15, (255, 255, 255)),]

fps_cap = 60
fps = fps_cap
clock = pygame.time.Clock()
delta = int(1000 / fps_cap)

background_colour = (134, 161, 219)

is_running = True
while is_running:

    screen.fill(background_colour)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            is_running = False
        elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
            pass

    player.update(delta)
    player.draw(screen)

    for wall in walls:
        wall.update(delta)
        wall.draw(screen)

    pygame.display.update()
    delta = clock.tick(fps_cap)

pygame.quit()