extract static variables to separate file | introduce camera

This commit is contained in:
= 2021-03-15 13:31:25 +01:00
parent 050deeae96
commit a1ede76b6b
7 changed files with 47 additions and 28 deletions

View File

@ -1,31 +1,31 @@
import pygame
from settings import SCREEN_WIDTH, SCREEN_HEIGHT
from survival.camera import Camera
from survival.game_map import GameMap
window_width = 1280
window_height = 720
def draw_game(delta):
game_map.draw(delta, win)
win.fill((0, 0, 0))
game_map.draw(camera)
pygame.display.update()
def update_game(delta, pressed_keys):
game_map.update(delta, pressed_keys)
game_map.update(camera, delta, pressed_keys)
pass
if __name__ == '__main__':
pygame.init()
win = pygame.display.set_mode((window_width, window_height))
win = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("AI Project")
clock = pygame.time.Clock()
game_map = GameMap(int(window_width/32), int(window_height/32) + 1)
game_map = GameMap(int(SCREEN_WIDTH / 32) * 2, 2 * int(SCREEN_HEIGHT / 32) + 1)
camera = Camera(game_map.width * 32, game_map.height * 32, win)
run = True
while run:

View File

@ -1,16 +1,30 @@
from pygame.rect import Rect
from survival import SCREEN_WIDTH, SCREEN_HEIGHT
class Camera:
def __init__(self, width, height):
def __init__(self, width, height, window):
self.camera = Rect(0, 0, width, height)
self.width = width
self.height = height
self.window = window
def apply(self, game_object):
return game_object.get_rect().move(self.camera.topleft)
def draw(self, image):
image.draw(self.window, self)
def update(self, target_object):
x = -target_object.get_rect().x + int(self.width / 2)
y = -target_object.get_rect().y + int(self.height / 2)
def apply(self, pos):
return pos[0] + self.camera.left, pos[1] + self.camera.top
def get_visible_area(self):
pass
def update(self, target):
x = -target.pos[0] + int(SCREEN_WIDTH / 2)
y = -target.pos[1] + int(SCREEN_HEIGHT / 2)
x = min(0, x)
y = min(0, y)
x = max(-(self.width - SCREEN_WIDTH), x)
y = max(-(self.height - SCREEN_HEIGHT), y)
self.camera = Rect(x, y, self.width, self.height)

View File

@ -7,11 +7,14 @@ class GameMap:
self.width = width
self.height = height
self.player = Player()
self.tiles_layer = TileLayer(width, height)
self.layers = []
self.layers.append(TileLayer(width, height))
def draw(self, delta, window):
self.tiles_layer.draw(window)
self.player.draw(window)
def draw(self, camera):
for layer in self.layers:
layer.draw(camera)
self.player.draw(camera)
def update(self, delta, pressed_keys):
def update(self, camera, delta, pressed_keys):
self.player.update(delta, pressed_keys)
camera.update(self.player)

View File

@ -16,7 +16,7 @@ class Image:
(self.texture.get_width() * scale, self.texture.get_height() * scale))
self.scale = scale
def draw(self, window):
window.blit(self.image, self.pos,
def draw(self, window, camera):
window.blit(self.image, camera.apply(self.pos),
pygame.Rect(self.origin[0] * self.scale, self.origin[1] * self.scale, 32 * self.scale,
32 * self.scale))

View File

@ -12,11 +12,11 @@ class Player:
self.image = Image('stevenson.png')
self.image.set_scale(2)
self.origin = (0, 0)
self.speed = 3
self.speed = 30
self.movement_target = [self.pos[0], self.pos[1]]
self.timer = 0
def draw(self, window):
def draw(self, camera):
if self.is_moving():
if self.velocity[0] == 1:
self.image.origin = (96, 0)
@ -27,12 +27,12 @@ class Player:
else:
self.image.origin = (32, 0)
self.image.pos = self.pos
self.image.draw(window)
camera.draw(self.image)
def is_moving(self):
return self.pos != self.movement_target
def get_random_direction(self):
def move_in_random_direction(self):
value = randint(0, 3)
random_movement = {
0: self.move_up,
@ -59,7 +59,7 @@ class Player:
self.timer += delta
if self.timer > 1000:
self.get_random_direction()
#self.move_in_random_direction()
self.timer = 0
if pressed_keys[pygame.K_LEFT]:

2
survival/settings.py Normal file
View File

@ -0,0 +1,2 @@
SCREEN_WIDTH = 1920
SCREEN_HEIGHT = 1080

View File

@ -9,9 +9,9 @@ class TileLayer:
self.tiles = [[Tile() for x in range(self.width)] for y in range(self.height)]
self.image = Image('atlas.png')
def draw(self, window):
def draw(self, camera):
for y in range(self.height):
for x in range(self.width):
self.image.pos = (x*32, y*32)
self.image.origin = self.tiles[y][x].origin
self.image.draw(window)
camera.draw(self.image)