dev-jakklu #3
BIN
assets/atlas.png
Normal file
BIN
assets/atlas.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 10 KiB |
BIN
assets/stevenson.png
Normal file
BIN
assets/stevenson.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.0 KiB |
@ -1,36 +1,36 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
|
from settings import SCREEN_WIDTH, SCREEN_HEIGHT
|
||||||
|
from survival.camera import Camera
|
||||||
from survival.game_map import GameMap
|
from survival.game_map import GameMap
|
||||||
|
|
||||||
window_width = 1280
|
|
||||||
window_height = 720
|
|
||||||
|
|
||||||
|
def draw_game(delta):
|
||||||
def draw_game():
|
win.fill((0, 0, 0))
|
||||||
game_map.draw(win)
|
game_map.draw(camera)
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
|
|
||||||
def update_game(pressed_keys):
|
def update_game(delta, pressed_keys):
|
||||||
game_map.update(pressed_keys)
|
game_map.update(camera, delta, pressed_keys)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
pygame.init()
|
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")
|
pygame.display.set_caption("AI Project")
|
||||||
|
|
||||||
clock = pygame.time.Clock()
|
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
|
run = True
|
||||||
|
|
||||||
while run:
|
while run:
|
||||||
# Set the framerate
|
# Set the framerate
|
||||||
clock.tick(60)
|
ms = clock.tick(60)
|
||||||
|
|
||||||
events = pygame.event.get()
|
events = pygame.event.get()
|
||||||
|
|
||||||
@ -40,5 +40,5 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
|
|
||||||
draw_game()
|
draw_game(ms)
|
||||||
update_game(keys)
|
update_game(ms, keys)
|
||||||
|
@ -1,16 +1,31 @@
|
|||||||
from pygame.rect import Rect
|
from pygame.rect import Rect
|
||||||
|
|
||||||
|
from survival import SCREEN_WIDTH, SCREEN_HEIGHT
|
||||||
|
|
||||||
|
|
||||||
class Camera:
|
class Camera:
|
||||||
def __init__(self, width, height):
|
def __init__(self, width, height, window):
|
||||||
self.camera = Rect(0, 0, width, height)
|
self.camera = Rect(0, 0, width, height)
|
||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
|
self.window = window
|
||||||
|
|
||||||
def apply(self, game_object):
|
def draw(self, image):
|
||||||
return game_object.get_rect().move(self.camera.topleft)
|
image.draw(self.window, self)
|
||||||
|
|
||||||
def update(self, target_object):
|
def apply(self, pos):
|
||||||
x = -target_object.get_rect().x + int(self.width / 2)
|
return pos[0] + self.camera.left, pos[1] + self.camera.top
|
||||||
y = -target_object.get_rect().y + int(self.height / 2)
|
|
||||||
|
def get_visible_area(self):
|
||||||
|
return Rect(-self.camera.left, -self.camera.top,
|
||||||
|
SCREEN_WIDTH - self.camera.left, SCREEN_HEIGHT - self.camera.top)
|
||||||
|
|
||||||
|
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)
|
self.camera = Rect(x, y, self.width, self.height)
|
||||||
|
@ -1,50 +1,21 @@
|
|||||||
import os
|
|
||||||
|
|
||||||
import pygame
|
|
||||||
from pygame.rect import Rect
|
|
||||||
|
|
||||||
from survival.player import Player
|
from survival.player import Player
|
||||||
from survival.quad_tree import QuadTree
|
from survival.tile_layer import TileLayer
|
||||||
from survival.stone import Stone
|
|
||||||
from survival.tile import Tile
|
|
||||||
|
|
||||||
|
|
||||||
class GameMap:
|
class GameMap:
|
||||||
|
|
||||||
def __init__(self, width, height):
|
def __init__(self, width, height):
|
||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
self.game_objects = []
|
|
||||||
self.player = Player()
|
self.player = Player()
|
||||||
self.game_objects.append(self.player)
|
self.layers = []
|
||||||
self.tiles = [[Tile() for x in range(width)] for y in range(height)]
|
self.layers.append(TileLayer(width, height))
|
||||||
self.textures = [pygame.image.load(os.path.join('..', 'assets', 'map1.png')),
|
|
||||||
pygame.image.load(os.path.join('..', 'assets', 'map2.png')),
|
|
||||||
pygame.image.load(os.path.join('..', 'assets', 'map3.png')),
|
|
||||||
pygame.image.load(os.path.join('..', 'assets', 'map4.png'))]
|
|
||||||
self.game_objects.append(Stone([100, 200]))
|
|
||||||
self.quad_tree = QuadTree(0, Rect(0, 0, width * 32, height * 32))
|
|
||||||
|
|
||||||
def draw(self, window):
|
def draw(self, camera):
|
||||||
for y in range(self.height):
|
visible_area = camera.get_visible_area()
|
||||||
for x in range(self.width):
|
for layer in self.layers:
|
||||||
window.blit(self.textures[self.tiles[y][x].background_id], (x * 32, y * 32))
|
layer.draw(camera, visible_area)
|
||||||
|
self.player.draw(camera)
|
||||||
|
|
||||||
for game_object in self.game_objects:
|
def update(self, camera, delta, pressed_keys):
|
||||||
game_object.draw(window)
|
self.player.update(delta, pressed_keys)
|
||||||
|
camera.update(self.player)
|
||||||
def update(self, pressed_keys):
|
|
||||||
self.quad_tree.clear()
|
|
||||||
|
|
||||||
for game_object in self.game_objects:
|
|
||||||
self.quad_tree.insert(game_object)
|
|
||||||
|
|
||||||
self.player.update(pressed_keys)
|
|
||||||
|
|
||||||
for game_object in self.game_objects:
|
|
||||||
possible_colliders = []
|
|
||||||
self.quad_tree.retrieve(possible_colliders, game_object)
|
|
||||||
for collider in possible_colliders:
|
|
||||||
if game_object.get_rect().colliderect(collider.get_rect()) and game_object != collider:
|
|
||||||
game_object.velocity = [0, 0]
|
|
||||||
game_object.pos = game_object.last_pos
|
|
||||||
|
22
survival/image.py
Normal file
22
survival/image.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
|
||||||
|
class Image:
|
||||||
|
def __init__(self, filename):
|
||||||
|
self.texture = pygame.image.load(os.path.join('..', 'assets', filename)).convert_alpha()
|
||||||
|
self.image = self.texture
|
||||||
|
self.origin = (0, 0)
|
||||||
|
self.pos = (0, 0)
|
||||||
|
self.scale = 1
|
||||||
|
|
||||||
|
def set_scale(self, scale):
|
||||||
|
self.image = pygame.transform.scale(self.texture,
|
||||||
|
(self.texture.get_width() * scale, self.texture.get_height() * scale))
|
||||||
|
self.scale = scale
|
||||||
|
|
||||||
|
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))
|
@ -1,34 +1,88 @@
|
|||||||
import os
|
from random import randint
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
from survival.game_object import GameObject
|
from survival.image import Image
|
||||||
|
|
||||||
|
|
||||||
class Player(GameObject):
|
class Player:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__([0, 0], os.path.join('..', 'assets', 'player.png'))
|
self.pos = [0, 0]
|
||||||
|
self.velocity = [0, 0]
|
||||||
|
self.image = Image('stevenson.png')
|
||||||
|
self.image.set_scale(2)
|
||||||
|
self.origin = (0, 0)
|
||||||
|
self.speed = 30
|
||||||
|
self.movement_target = [self.pos[0], self.pos[1]]
|
||||||
|
self.timer = 0
|
||||||
|
|
||||||
def draw(self, window):
|
def draw(self, camera):
|
||||||
super().draw(window)
|
if self.is_moving():
|
||||||
|
if self.velocity[0] == 1:
|
||||||
|
self.image.origin = (96, 0)
|
||||||
|
elif self.velocity[0] == -1:
|
||||||
|
self.image.origin = (64, 0)
|
||||||
|
elif self.velocity[1] == 1:
|
||||||
|
self.image.origin = (0, 0)
|
||||||
|
else:
|
||||||
|
self.image.origin = (32, 0)
|
||||||
|
self.image.pos = self.pos
|
||||||
|
camera.draw(self.image)
|
||||||
|
|
||||||
|
def is_moving(self):
|
||||||
|
return self.pos != self.movement_target
|
||||||
|
|
||||||
|
def move_in_random_direction(self):
|
||||||
|
value = randint(0, 3)
|
||||||
|
random_movement = {
|
||||||
|
0: self.move_up,
|
||||||
|
1: self.move_down,
|
||||||
|
2: self.move_left,
|
||||||
|
3: self.move_right
|
||||||
|
}
|
||||||
|
random_movement[value]()
|
||||||
|
|
||||||
|
def update(self, delta, pressed_keys):
|
||||||
|
if self.is_moving():
|
||||||
|
if self.velocity[0] != 0:
|
||||||
|
self.pos[0] += self.velocity[0] * self.speed * delta / 100
|
||||||
|
if abs(self.movement_target[0] - self.pos[0]) < 0.1 * self.speed:
|
||||||
|
self.velocity = [0, 0]
|
||||||
|
self.pos = self.movement_target
|
||||||
|
else:
|
||||||
|
self.pos[1] += self.velocity[1] * self.speed * delta / 100
|
||||||
|
if abs(self.pos[1] - self.movement_target[1]) < 0.1 * self.speed:
|
||||||
|
self.velocity = [0, 0]
|
||||||
|
self.pos = self.movement_target
|
||||||
|
return
|
||||||
|
|
||||||
|
self.timer += delta
|
||||||
|
|
||||||
|
if self.timer > 1000:
|
||||||
|
#self.move_in_random_direction()
|
||||||
|
self.timer = 0
|
||||||
|
|
||||||
def update(self, pressed_keys):
|
|
||||||
if pressed_keys[pygame.K_LEFT]:
|
if pressed_keys[pygame.K_LEFT]:
|
||||||
self.velocity[0] = -1
|
self.move_left()
|
||||||
elif pressed_keys[pygame.K_RIGHT]:
|
elif pressed_keys[pygame.K_RIGHT]:
|
||||||
self.velocity[0] = 1
|
self.move_right()
|
||||||
else:
|
elif pressed_keys[pygame.K_DOWN]:
|
||||||
self.velocity[0] = 0
|
self.move_down()
|
||||||
|
|
||||||
if pressed_keys[pygame.K_DOWN]:
|
|
||||||
self.velocity[1] = 1
|
|
||||||
elif pressed_keys[pygame.K_UP]:
|
elif pressed_keys[pygame.K_UP]:
|
||||||
self.velocity[1] = -1
|
self.move_up()
|
||||||
else:
|
|
||||||
self.velocity[1] = 0
|
|
||||||
|
|
||||||
self.last_pos = [self.pos[0], self.pos[1]]
|
def move_left(self):
|
||||||
|
self.velocity = [-1, 0]
|
||||||
|
self.movement_target = [self.pos[0] - 32, self.pos[1]]
|
||||||
|
|
||||||
self.pos[0] += self.velocity[0]
|
def move_right(self):
|
||||||
self.pos[1] += self.velocity[1]
|
self.velocity = [1, 0]
|
||||||
|
self.movement_target = [self.pos[0] + 32, self.pos[1]]
|
||||||
|
|
||||||
|
def move_up(self):
|
||||||
|
self.velocity = [0, -1]
|
||||||
|
self.movement_target = [self.pos[0], self.pos[1] - 32]
|
||||||
|
|
||||||
|
def move_down(self):
|
||||||
|
self.velocity = [0, 1]
|
||||||
|
self.movement_target = [self.pos[0], self.pos[1] + 32]
|
||||||
|
2
survival/settings.py
Normal file
2
survival/settings.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
SCREEN_WIDTH = 1920
|
||||||
|
SCREEN_HEIGHT = 1080
|
@ -1,9 +0,0 @@
|
|||||||
import os
|
|
||||||
|
|
||||||
from survival.game_object import GameObject
|
|
||||||
|
|
||||||
|
|
||||||
class Stone(GameObject):
|
|
||||||
|
|
||||||
def __init__(self, pos):
|
|
||||||
super().__init__(pos, os.path.join('..', 'assets', 'stone.png'))
|
|
@ -1,24 +0,0 @@
|
|||||||
import pygame
|
|
||||||
|
|
||||||
|
|
||||||
class TextureAtlas(object):
|
|
||||||
def __init__(self, filename):
|
|
||||||
self.atlas = pygame.image.load(filename).convert()
|
|
||||||
|
|
||||||
def image_at(self, rectangle, color_key=None):
|
|
||||||
rect = pygame.Rect(rectangle)
|
|
||||||
image = pygame.Surface(rect.size).convert()
|
|
||||||
image.blit(self.atlas, (0, 0), rect)
|
|
||||||
if color_key is not None:
|
|
||||||
if color_key is -1:
|
|
||||||
color_key = image.get_at((0, 0))
|
|
||||||
image.set_colorkey(color_key, pygame.RLEACCEL)
|
|
||||||
return image
|
|
||||||
|
|
||||||
def images_at(self, rects, color_key=None):
|
|
||||||
return [self.image_at(rect, color_key) for rect in rects]
|
|
||||||
|
|
||||||
def load_row(self, rect, image_count, color_key=None):
|
|
||||||
images = [(rect[0] + rect[2] * x, rect[1], rect[2], rect[3])
|
|
||||||
for x in range(image_count)]
|
|
||||||
return self.images_at(images, color_key)
|
|
@ -1,7 +1,9 @@
|
|||||||
import pygame
|
import random
|
||||||
from random import randrange
|
|
||||||
|
|
||||||
|
|
||||||
class Tile:
|
class Tile:
|
||||||
def __init__(self):
|
origins = [(0, 0), (32, 0), (64, 0), (96, 0)]
|
||||||
self.background_id = randrange(4)
|
|
||||||
|
def __init__(self, origin=(0, 0)):
|
||||||
|
self.origin = random.choice(Tile.origins)
|
||||||
|
self.image = None
|
||||||
|
17
survival/tile_layer.py
Normal file
17
survival/tile_layer.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
from survival.image import Image
|
||||||
|
from survival.tile import Tile
|
||||||
|
|
||||||
|
|
||||||
|
class TileLayer:
|
||||||
|
def __init__(self, width, height):
|
||||||
|
self.width = width
|
||||||
|
self.height = height
|
||||||
|
self.tiles = [[Tile() for x in range(self.width)] for y in range(self.height)]
|
||||||
|
self.image = Image('atlas.png')
|
||||||
|
|
||||||
|
def draw(self, camera, visible_area):
|
||||||
|
for y in range(int(visible_area.top/32), int(visible_area.height/32) + 1):
|
||||||
|
for x in range(int(visible_area.left/32), int(visible_area.width/32) + 1):
|
||||||
|
self.image.pos = (x * 32, y * 32)
|
||||||
|
self.image.origin = self.tiles[y][x].origin
|
||||||
|
camera.draw(self.image)
|
Loading…
Reference in New Issue
Block a user