feat: rendering textures

This commit is contained in:
korzepadawid 2022-03-04 20:57:34 +01:00
parent 9f1ca750f1
commit 9c23ea4a3d
13 changed files with 50 additions and 10 deletions

4
field.py Normal file
View File

@ -0,0 +1,4 @@
class Field:
def __init__(self, texture_path, converted_texture):
self.texture_path = texture_path
self.converted_texture = converted_texture

21
game.py
View File

@ -1,4 +1,7 @@
import pygame import pygame
from glob import glob
from grid import Grid
GAME_TITLE = 'WMICraft' GAME_TITLE = 'WMICraft'
WINDOW_HEIGHT = 900 WINDOW_HEIGHT = 900
@ -17,25 +20,23 @@ class Game:
pygame.init() pygame.init()
pygame.display.set_caption(GAME_TITLE) pygame.display.set_caption(GAME_TITLE)
pygame.display.set_icon(pygame.image.load('resources/icons/sword.png')) pygame.display.set_icon(pygame.image.load('resources/icons/sword.png'))
self.screen = pygame.display.set_mode((WINDOW_HEIGHT, WINDOW_WIDTH)) self.screen = pygame.display.set_mode((WINDOW_HEIGHT, WINDOW_WIDTH))
self.clock = pygame.time.Clock() self.clock = pygame.time.Clock()
self.textures = []
for texture_path in glob('resources/textures/*.jpg'):
converted_texture = pygame.image.load(texture_path).convert_alpha()
self.textures.append((texture_path, converted_texture))
def start(self): def start(self):
running = True running = True
grid = Grid(self.textures)
while running: while running:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
running = False running = False
self.draw_grid() grid.draw(self.screen)
pygame.display.update() pygame.display.update()
self.clock.tick(FPS_COUNT) self.clock.tick(FPS_COUNT)
pygame.quit() pygame.quit()
def draw_grid(self):
for row in range(ROWS):
for column in range(COLUMNS):
box_rect = [(GRID_CELL_PADDING + GRID_CELL_WIDTH) * column + GRID_CELL_PADDING,
(GRID_CELL_PADDING + GRID_CELL_HEIGHT) * row + GRID_CELL_PADDING,
GRID_CELL_WIDTH,
GRID_CELL_HEIGHT]
pygame.draw.rect(self.screen, GREEN, box_rect)

35
grid.py Normal file
View File

@ -0,0 +1,35 @@
import pygame
import random
from field import Field
GRID_CELL_PADDING = 3
GRID_CELL_WIDTH = 42
GRID_CELL_HEIGHT = 42
ROWS = 20
COLUMNS = 20
class Grid:
def __init__(self, textures):
self.textures = textures
self.grid = []
for row in range(ROWS):
self.grid.append([])
for _ in range(COLUMNS):
texture_path, converted_texture = self.get_random_texture()
field = Field(texture_path, converted_texture)
self.grid[row].append(field)
def get_random_texture(self):
texture_index = random.randint(0, len(self.textures) - 1)
return self.textures[texture_index]
def draw(self, screen):
for row in range(ROWS):
for column in range(COLUMNS):
box_rect = [(GRID_CELL_PADDING + GRID_CELL_WIDTH) * column + GRID_CELL_PADDING,
(GRID_CELL_PADDING + GRID_CELL_HEIGHT) * row + GRID_CELL_PADDING,
GRID_CELL_WIDTH,
GRID_CELL_HEIGHT]
image = self.grid[row][column].converted_texture
screen.blit(pygame.transform.scale(image, (GRID_CELL_WIDTH, GRID_CELL_HEIGHT)), box_rect)

BIN
resources/textures/t1.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

BIN
resources/textures/t10.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

BIN
resources/textures/t2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 218 KiB

BIN
resources/textures/t3.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 161 KiB

BIN
resources/textures/t4.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 381 KiB

BIN
resources/textures/t5.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 470 KiB

BIN
resources/textures/t6.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

BIN
resources/textures/t7.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

BIN
resources/textures/t8.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

BIN
resources/textures/t9.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB