-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.py
62 lines (49 loc) · 1.85 KB
/
screen.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
import pygame as pg
import random as R
class Screen:
def __init__(self, w, h, tile_size=32):
self.surface = pg.display.set_mode((w,h)) # définit l'écran
self.camera = pg.Rect(0,0,w,h)
self.blit = self.surface.blit # crée un raccourci
self.background = None
self.width = w
self.height = h
self.tile_size = tile_size
self.amount_x_tile = 0
self.amount_y_tile = 0
def get_size(self):
return (self.width,self.height)
def draw_grid(self):
for x in range(0, self.width, self.tile_size):
pg.draw.line(self.surface, (181,181,181), (x,0), (x,self.height))
for y in range(0, self.height, self.tile_size):
pg.draw.line(self.surface, (181,181,181), (0,y), (self.width,y))
def set_size_tile(self, w, h):
self.amount_x_tile = w
self.amount_y_tile = h
def draw_background(self, rect):
return self.blit(self.background, rect, rect)
def background_cam(self, rect):
return self.draw_background(rect.move(self.camera.topleft))
def blit_cam(self, image, rect):
return self.blit(image, rect.move(self.camera.topleft))
def update_camera(self, target):
x = -target.rect.x + self.width//2
y = -target.rect.y + self.height//2
x = min(0, x)
y = min(0, y)
width_map = self.amount_x_tile*self.tile_size
height_map = self.amount_y_tile*self.tile_size
x = max(-(width_map - self.width), x)
y = max(-(height_map - self.height), y)
self.camera.x = x
self.camera.y = y
def shake(self):
if R.random() > 0.5:
self.camera.x += 5
else:
self.camera.x -= 5
if R.random() > 0.5:
self.camera.y += 5
else:
self.camera.y -= 5