added menu; changed tiles textures;

This commit is contained in:
Angelika Iskra 2022-03-08 12:11:22 +01:00
parent e81dd05024
commit 358107a046
20 changed files with 161 additions and 21 deletions

View File

@ -1,9 +1,31 @@
GAME_TITLE = 'WMICraft'
WINDOW_HEIGHT = 900
WINDOW_WIDTH = 900
GRID_CELL_PADDING = 3
GRID_CELL_WIDTH = 42
GRID_CELL_HEIGHT = 42
ROWS = 20
COLUMNS = 20
WINDOW_HEIGHT = 800
WINDOW_WIDTH = 1360
GRID_CELL_PADDING = 5
GRID_CELL_WIDTH = 54
GRID_CELL_HEIGHT = 54
ROWS = 13
COLUMNS = 16
BORDER_WIDTH = 15
FPS_COUNT = 60
TILES = [
'grass1.png',
'grass2.png',
'grass3.png',
# 'grass4.png',
# 'grass5.png',
# 'grass6.png',
'sand.png',
'water.png',
# 'grass_with_tree.jpg',
]
OBJECTS = [
{
'name': 'tree',
'location': 'tree1.png'
},
{
'name': 'knight',
'location': 'knight.png'
}
]

114
game.py
View File

@ -1,8 +1,10 @@
import pygame
import pygame, sys
from glob import glob
from grid import Grid
from constants import GAME_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT, FPS_COUNT
from constants import GAME_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT, FPS_COUNT, TILES
from stats import Stats
from helpers import draw_text
class Game:
@ -11,22 +13,114 @@ class Game:
pygame.display.set_caption(GAME_TITLE)
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_WIDTH, WINDOW_HEIGHT))
self.font = pygame.font.SysFont(None, 30)
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))
self.tiles = []
for tile_path in TILES:
converted_tile = pygame.image.load('resources/textures/' + tile_path).convert_alpha()
self.tiles.append((tile_path, converted_tile))
def start(self):
self.bg = pygame.image.load("resources/textures/menu_bg2.jpg")
click = False
def main_menu(self):
while True:
self.screen.blit(self.bg, (0, 0))
pygame.draw.rect(self.screen, (255, 255, 255), pygame.Rect(800, 100, 400, 500), 0, 5)
draw_text('MAIN MENU', self.font, (0, 0, 0), self.screen, 850, 150)
mx, my = pygame.mouse.get_pos()
button_1 = pygame.Rect(850, 250, 300, 50)
button_2 = pygame.Rect(850, 350, 300, 50)
button_3 = pygame.Rect(850, 450, 300, 50)
if button_1.collidepoint((mx, my)):
if click:
self.game()
if button_2.collidepoint((mx, my)):
if click:
self.options()
if button_3.collidepoint((mx, my)):
if click:
self.credits()
pygame.draw.rect(self.screen, (0, 191, 255), button_1, 0, 4)
draw_text('PLAY', self.font, (255, 255, 255), self.screen, 870, 265)
pygame.draw.rect(self.screen, (0, 191, 255), button_2, 0, 4)
draw_text('OPTIONS', self.font, (255, 255, 255), self.screen, 870, 365)
pygame.draw.rect(self.screen, (0, 191, 255), button_3, 0, 4)
draw_text('CREDITS', self.font, (255, 255, 255), self.screen, 870, 465)
click = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
click = True
pygame.display.update()
self.clock.tick(60)
def options(self):
running = True
grid = Grid(self.textures)
while running:
self.screen.fill((0, 0, 0))
draw_text('options', self.font, (255, 255, 255), self.screen, 20, 20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
pygame.display.update()
self.clock.tick(60)
def credits(self):
running = True
while running:
self.screen.fill((0, 0, 0))
draw_text('credits', self.font, (255, 255, 255), self.screen, 20, 20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
pygame.display.update()
self.clock.tick(60)
def game(self):
running = True
grid = Grid(self.tiles)
stats = Stats()
while running:
self.screen.blit(self.bg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
grid.draw(self.screen)
stats.draw(self.screen, self.font)
pygame.display.update()
self.clock.tick(FPS_COUNT)
pygame.quit()

10
grid.py
View File

@ -2,7 +2,7 @@ import pygame
import random
from field import Field
from constants import ROWS, COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT
from constants import ROWS, COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT, BORDER_WIDTH
class Grid:
@ -21,10 +21,14 @@ class Grid:
return self.textures[texture_index]
def draw(self, screen):
bg_width = (GRID_CELL_PADDING + GRID_CELL_WIDTH) * COLUMNS + BORDER_WIDTH
bg_height = (GRID_CELL_PADDING + GRID_CELL_HEIGHT) * ROWS + BORDER_WIDTH
pygame.draw.rect(screen, (255, 255, 255), pygame.Rect(10, 8, bg_width, bg_height))
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,
box_rect = [(GRID_CELL_PADDING + GRID_CELL_WIDTH) * column + GRID_CELL_PADDING + 15,
(GRID_CELL_PADDING + GRID_CELL_HEIGHT) * row + GRID_CELL_PADDING + 13,
GRID_CELL_WIDTH,
GRID_CELL_HEIGHT]
image = self.grid[row][column].converted_texture

5
helpers.py Normal file
View File

@ -0,0 +1,5 @@
def draw_text(text, font, color, surface, x, y):
textobj = font.render(text, 1, color)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)

View File

@ -2,4 +2,4 @@ from game import Game
if __name__ == '__main__':
game = Game()
game.start()
game.main_menu()

Binary file not shown.

After

Width:  |  Height:  |  Size: 814 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 820 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 789 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 969 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 KiB

BIN
resources/textures/sand.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 760 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1019 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 810 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 725 B

15
stats.py Normal file
View File

@ -0,0 +1,15 @@
import pygame
from constants import COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, BORDER_WIDTH
from helpers import draw_text
class Stats:
def __init__(self):
self.grid = []
def draw(self, screen, font):
x = (GRID_CELL_PADDING + GRID_CELL_WIDTH) * COLUMNS + BORDER_WIDTH + 20
y = 8
pygame.draw.rect(screen, (255, 255, 255), pygame.Rect(x, y, 370, 782))
draw_text('GAME STATS', font, (0, 0, 0), screen, x + 120, y + 30)