File: grid.py

package info (click to toggle)
lightyears 1.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,804 kB
  • sloc: python: 5,532; sh: 39; makefile: 6
file content (56 lines) | stat: -rw-r--r-- 1,680 bytes parent folder | download | duplicates (2)
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
#
# 20,000 Light Years Into Space
# This game is licensed under GPL v2, and copyright (C) Jack Whitham 2006-21.
#

import pygame
from . import game_types, primitives


class Grid:
    def __init__(self) -> None:
        self.Set_Grid_Size(10)

    def Scr_To_Grid(self, xy: game_types.SurfacePosition) -> game_types.GridPosition:
        (x,y) = xy
        return (x // self.size, y // self.size)

    def Grid_To_Scr(self, xy: game_types.GridPosition) -> game_types.SurfacePosition:
        (x,y) = xy
        return (( x * self.size ) + self.h_size,
                ( y * self.size ) + self.h_size )

    def Float_Grid_To_Scr(self, xy: game_types.FloatGridPosition) -> game_types.FloatSurfacePosition:
        (x,y) = xy
        return (( x * self.size ) + self.h_size,
                ( y * self.size ) + self.h_size )

    def Grid_To_Scr_Rect(self, xy: game_types.GridPosition) -> game_types.RectType:
        (x,y) = xy
        (cx,cy) = Grid_To_Scr((x,y))
        return pygame.Rect(cx - self.h_size_1, cy - self.h_size_1,
                self.size_1, self.size_1)

    def Set_Grid_Size(self, sz: int) -> None:
        assert type(sz) == int
        self.size = sz
        self.size_1 = sz - 1
        self.h_size = sz // 2
        self.h_size_1 = self.h_size - 1

    def Get_Grid_Size(self) -> int:
        return self.size


__grid = Grid()
Grid_To_Scr_Rect = __grid.Grid_To_Scr_Rect
Grid_To_Scr = __grid.Grid_To_Scr
Float_Grid_To_Scr = __grid.Float_Grid_To_Scr
Scr_To_Grid = __grid.Scr_To_Grid
Get_Grid_Size = __grid.Get_Grid_Size

def Set_Screen_Height(height: int) -> None:
    (w, h) = primitives.GRID_SIZE
    assert w == h
    __grid.Set_Grid_Size(height // h)