Passed
Push — master ( ff156b...4e45e0 )
by Yohann
01:13
created

src.app.App.__init__()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
import random
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
from time import perf_counter_ns
3
from typing import Set
4
5
import pygame
6
7
from . import FPS_LIMIT, TITLE, screen, viewport,  SCREEN_SIZE
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
Loading history...
8
from .classes.tiles import Tile
9
from .classes.camera import camera
10
11
12
class App:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
13
14
    def __init__(self) -> None:
15
        self.clock: pygame.time.Clock = pygame.time.Clock()
16
        self.is_running: bool = True
17
18
        self.debug = False
19
        self.fps_limit = FPS_LIMIT
20
21
        self.pressed: Set[int] = set()
22
23
    def handle_event(self, event: pygame.event.Event) -> None:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
24
        if event.type == pygame.QUIT:
0 ignored issues
show
Bug introduced by
The Module pygame does not seem to have a member named QUIT.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
25
            self.is_running = False
26
27
        elif event.type == pygame.KEYDOWN:
0 ignored issues
show
Bug introduced by
The Module pygame does not seem to have a member named KEYDOWN.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
28
            self.pressed.add(event.key)
29
30
            if event.key == pygame.K_f:
0 ignored issues
show
Bug introduced by
The Module pygame does not seem to have a member named K_f.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
31
                self.toggle_debug_mode()
32
33
        elif event.type == pygame.KEYUP:
0 ignored issues
show
Bug introduced by
The Module pygame does not seem to have a member named KEYUP.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
34
            self.pressed.remove(event.key)
35
36
    def toggle_debug_mode(self):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
37
        if self.debug:
38
            self.debug = False
39
            self.fps_limit = FPS_LIMIT
40
41
        else:
42
            pygame.display.set_caption(TITLE)
43
            self.debug = True
44
            # The more fps, the more cpu usage
45
            self.fps_limit = 1000
46
47
    def run(self) -> None:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
48
        tiles = [
49
            [
50
                Tile(
51
                    random.choice(list(Tile.SPRITES.keys())),
52
                    (x * Tile.SIZE - camera.x, y * Tile.SIZE - camera.y)
53
                ) for x in range(Tile.TILE_COUNT_X)
54
            ] for y in range(Tile.TILE_COUNT_Y)
55
        ]
56
57
        while self.is_running:
58
            marker: float = perf_counter_ns()
59
60
            # Temporary white bg to simulate scratch background.
61
            viewport.fill((255, 255, 255))
62
63
            camera.update(self.pressed)
64
65
            for line in tiles:
66
                for tile in line:
67
                    tile.update(camera)
68
69
                    viewport.blit(
70
                        tile.sprite,
71
                        (
72
                            tile.rect.x - camera.x,
73
                            tile.rect.y - camera.y
74
                        )
75
                    )
76
77
            screen.blit(pygame.transform.scale(viewport, SCREEN_SIZE), (0, 0))
78
            pygame.display.update()
79
80
            for event in pygame.event.get():
81
                self.handle_event(event)
82
83
            if self.debug:
84
                pygame.display.set_caption(
85
                    f"{TITLE} - {self.clock.get_fps():.0f} fps "
86
                    f"- {(perf_counter_ns() - marker) / 1000:.0f} µs"
87
                )
88
89
            self.clock.tick(self.fps_limit)
90
91
    def __del__(self) -> None:
92
        pygame.display.quit()
93
        pygame.quit()
0 ignored issues
show
Bug introduced by
The Module pygame does not seem to have a member named quit.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
94