Added images to map and displaying selected ones #6

Merged
s459307 merged 1 commits from map-with-images into master 2022-03-09 20:59:27 +01:00
9 changed files with 68 additions and 12 deletions

BIN
cobble.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

BIN
cobble_block.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
dirt.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

BIN
dirt_block.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

BIN
grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

BIN
grass_block.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

80
main.py
View File

@ -1,4 +1,5 @@
import pygame import pygame
import random
WIDTH, HEIGHT = 800, 800 WIDTH, HEIGHT = 800, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT)) WIN = pygame.display.set_mode((WIDTH, HEIGHT))
@ -6,37 +7,92 @@ pygame.display.set_caption("Sztuczna Inteligencja")
FPS = 30 FPS = 30
AGENT_IMG = pygame.image.load("dot-22-433567.png") AGENT_IMG = pygame.image.load("dot-22-433567.png")
AGENT = pygame.transform.scale(AGENT_IMG, (50, 50)) AGENT = pygame.transform.scale(AGENT_IMG, (50, 50))
DIRT_IMG = pygame.image.load("dirt.jpg")
DIRT = pygame.transform.scale(DIRT_IMG, (50, 50))
GRASS_IMG = pygame.image.load("grass.png")
GRASS = pygame.transform.scale(GRASS_IMG, (50, 50))
SAND_IMG = pygame.image.load("sand.jpeg")
SAND = pygame.transform.scale(SAND_IMG, (50, 50))
COBBLE_IMG = pygame.image.load("cobble.jpeg")
COBBLE = pygame.transform.scale(COBBLE_IMG, (50, 50))
DIRT_BLOCK_IMG = pygame.image.load("dirt_block.jpg")
DIRT_BLOCK = pygame.transform.scale(DIRT_BLOCK_IMG, (800, 800))
GRASS_BLOCK_IMG = pygame.image.load("grass_block.jpeg")
GRASS_BLOCK = pygame.transform.scale(GRASS_BLOCK_IMG, (800, 800))
SAND_BLOCK_IMG = pygame.image.load("sand_block.jpg")
SAND_BLOCK = pygame.transform.scale(SAND_BLOCK_IMG, (800, 800))
COBBLE_BLOCK_IMG = pygame.image.load("cobble_block.jpeg")
COBBLE_BLOCK = pygame.transform.scale(COBBLE_BLOCK_IMG, (800, 800))
def randomize_map():
possible_fields = [DIRT, GRASS, SAND, COBBLE]
field_array_big = []
field_array_small = []
for i in range(16):
for j in range(16):
field_array_small.append(random.choice(possible_fields))
field_array_big.append(field_array_small)
field_array_small = []
return field_array_big
def read_img(agent, fields):
if fields[int(agent.x / 50)][int(agent.y / 50)] == GRASS:
WIN.blit(GRASS_BLOCK, (0, 0))
elif fields[int(agent.x / 50)][int(agent.y / 50)] == DIRT:
WIN.blit(DIRT_BLOCK, (0, 0))
elif fields[int(agent.x / 50)][int(agent.y / 50)] == SAND:
WIN.blit(SAND_BLOCK, (0, 0))
elif fields[int(agent.x / 50)][int(agent.y / 50)] == COBBLE:
WIN.blit(COBBLE_BLOCK, (0, 0))
pygame.display.update()
pygame.time.delay(2000)
def draw_window(agent, fields, image_shown):
if image_shown:
for i in range(16):
for j in range(16):
WIN.blit(fields[i][j], (i * 50, j * 50))
WIN.blit(AGENT, (agent.x, agent.y))
pygame.display.update()
else:
read_img(agent, fields)
def agent_movement(keys_pressed, agent): def agent_movement(keys_pressed, agent):
if keys_pressed[pygame.K_LEFT] and agent.x > 0: if keys_pressed[pygame.K_LEFT] and agent.x > 0:
agent.x -= 50 agent.x -= 50
if keys_pressed[pygame.K_RIGHT] and agent.x < 750: return False
elif keys_pressed[pygame.K_RIGHT] and agent.x < 750:
agent.x += 50 agent.x += 50
if keys_pressed[pygame.K_UP] and agent.y > 0: return False
elif keys_pressed[pygame.K_UP] and agent.y > 0:
agent.y -= 50 agent.y -= 50
if keys_pressed[pygame.K_DOWN] and agent.y < 750: return False
elif keys_pressed[pygame.K_DOWN] and agent.y < 750:
agent.y += 50 agent.y += 50
return False
else:
return True
def draw_window(agent):
WIN.fill((255,255,255))
WIN.blit(AGENT, (agent.x, agent.y))
pygame.display.update()
def main(): def main():
clock = pygame.time.Clock() clock = pygame.time.Clock()
run = True run = True
agent = pygame.Rect(0, 0, 50, 50) agent = pygame.Rect(0, 0, 50, 50)
fields = randomize_map()
image_shown = True
while run: while run:
clock.tick(FPS) clock.tick(FPS)
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
run = False run = False
draw_window(agent)
keys_pressed = pygame.key.get_pressed() keys_pressed = pygame.key.get_pressed()
agent_movement(keys_pressed, agent) draw_window(agent, fields, image_shown)
image_shown = agent_movement(keys_pressed, agent)
pygame.quit() pygame.quit()

BIN
sand.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

BIN
sand_block.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB