first commit

This commit is contained in:
Alessio Rondelli 2024-07-10 20:08:18 +02:00
parent d62c56d947
commit 4b2a7fc721
16 changed files with 149 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 B

21
data/maps/map_1.csv Normal file
View File

@ -0,0 +1,21 @@
3,1,1,1,1,1,1,1,1,1,1,1,1,1,4
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
1,2,2,2,2,2,2,2,2,2,2,2,2,2,1
5,1,1,1,1,1,1,1,1,1,1,1,1,1,6
1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 4
2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
3 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
4 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
5 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
6 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
7 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
8 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
9 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
10 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
11 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
12 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
13 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
14 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
15 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
16 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
17 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
18 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
19 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
20 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
21 5 1 1 1 1 1 1 1 1 1 1 1 1 1 6

53
game.py Normal file
View File

@ -0,0 +1,53 @@
import pygame
import numpy as np
from scripts.tiles import Tilemap
from scripts.entities import Entity
from scripts.utils import load_image, load_images
# SETTINGS
WIDTH = 1280
HEIGHT = 720
TITLE = 'YuGiOh Tag Force Clone'
class Game:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.display = pygame.Surface((WIDTH//2, HEIGHT//2))
pygame.display.set_caption(TITLE)
self.clock = pygame.time.Clock()
self.player = Entity(self, 'player', (50.0,200.0), (8, 15))
self.assets = {
'player' : load_image('entities/player.png'),
'grass' : load_images('tiles/grass/'),
'stone' : load_images('tiles/stone/')
}
self.tilemap = Tilemap(self, (32,32), 'map_1.csv')
def run(self):
self.status = True
while self.status:
self.display.fill('white')
delta = np.array([0.0,0.0])
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.status = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
delta[0] -= 1
if keys[pygame.K_RIGHT]:
delta[0] += 1
if keys[pygame.K_UP]:
delta[1] -= 1
if keys[pygame.K_DOWN]:
delta[1] += 1
self.player.update(delta)
self.tilemap.render(self.display)
self.player.render(self.display)
self.screen.blit(pygame.transform.scale_by(self.display,2), (0,0))
pygame.display.update()
self.clock.tick(60)
pygame.quit()
Game().run()

Binary file not shown.

Binary file not shown.

Binary file not shown.

15
scripts/entities.py Normal file
View File

@ -0,0 +1,15 @@
import pygame
import numpy as np
class Entity:
def __init__(self, game, e_type, pos, size):
self.game = game
self.type = e_type
self.pos = np.array(pos)
self.size = size
self.velocity = np.array([0,0], dtype=np.float32)
def update(self, movement = np.array([0,0])):
frame_movement = self.velocity + movement
self.pos += frame_movement
def render(self, surface):
surface.blit(self.game.assets[self.type], self.pos)

27
scripts/tiles.py Normal file
View File

@ -0,0 +1,27 @@
import pygame
import numpy as np
from scripts.utils import TILE_DICT
BASE_MAP_PATH = 'data/maps/'
class Tilemap:
def __init__(self, game, tile_size, map_path):
self.game = game
self.tile_size = np.array(tile_size)
self.offgrid_tiles = []
self.load_map(map_path)
def load_map(self, map_path):
print(map_path)
tile_arr = np.transpose(np.loadtxt(BASE_MAP_PATH + map_path, delimiter=',', dtype=np.int8))
self.tilemap = np.empty_like(tile_arr, dtype = object)
for row in range(tile_arr.shape[0]):
for col in range(tile_arr.shape[1]):
type_var = TILE_DICT[tile_arr[row,col]]
self.tilemap[row,col] = {'type' : type_var[0], 'variant' : type_var[1], 'pos': np.array([row,col])}
def render(self, surface):
for x in range(self.tilemap.shape[0]):
for y in range(self.tilemap.shape[1]):
tile = self.tilemap[x,y]
surface.blit(self.game.assets[tile['type']][tile['variant']],tile['pos'] * self.tile_size)
for tile in self.offgrid_tiles:
surface.blit(self.game.assets[tile['type']][tile['variant']],tile.pos) #this tile is written offgrid so no mult by tile_size

21
scripts/utils.py Normal file
View File

@ -0,0 +1,21 @@
import pygame
import os
TILE_DICT = {
1 : ('grass',0),
2 : ('stone',0),
3 : ('grass',1),
4 : ('stone',1),
5 : ('grass',2),
6 : ('stone',2)
}
BASE_IMG_PATH = 'data/images/'
def load_image(path):
img = pygame.image.load(BASE_IMG_PATH + path).convert()
img.set_colorkey((0,0,0))
return img
def load_images(path):
images = []
for img_name in sorted(os.listdir(BASE_IMG_PATH + path)):
images.append(load_image(path + '/' + img_name))
return images

12
shell.nix Normal file
View File

@ -0,0 +1,12 @@
{ pkgs ? import <nixpkgs> {config = { allowUnfree = false; cudaSupport = false;}; }}:
pkgs.mkShell {
buildInputs = with pkgs; [
(python311.withPackages(ps: with ps; [ pip numpy notebook tkinter pygame-ce ]))
];
#cudaPackages.cudatoolkit
shellHook = ''
echo "You are now using a NIX environment"
idle3.11
'';
}