SzybciorSmartTraktor/main.py

100 lines
3.2 KiB
Python
Raw Normal View History

import pygame
import random
WIDTH, HEIGHT = 800, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Sztuczna Inteligencja")
2022-03-08 22:30:31 +01:00
FPS = 30
AGENT_IMG = pygame.image.load("dot-22-433567.png")
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)
2022-03-09 18:59:50 +01:00
def agent_movement(keys_pressed, agent):
if keys_pressed[pygame.K_LEFT] and agent.x > 0:
agent.x -= 50
return False
elif keys_pressed[pygame.K_RIGHT] and agent.x < 750:
2022-03-09 18:59:50 +01:00
agent.x += 50
return False
elif keys_pressed[pygame.K_UP] and agent.y > 0:
2022-03-09 18:59:50 +01:00
agent.y -= 50
return False
elif keys_pressed[pygame.K_DOWN] and agent.y < 750:
2022-03-09 18:59:50 +01:00
agent.y += 50
return False
else:
return True
2022-03-09 18:59:50 +01:00
def main():
2022-03-08 22:30:31 +01:00
clock = pygame.time.Clock()
run = True
2022-03-08 22:30:31 +01:00
agent = pygame.Rect(0, 0, 50, 50)
fields = randomize_map()
image_shown = True
while run:
2022-03-09 18:59:50 +01:00
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
2022-03-09 18:59:50 +01:00
keys_pressed = pygame.key.get_pressed()
draw_window(agent, fields, image_shown)
image_shown = agent_movement(keys_pressed, agent)
pygame.quit()
if __name__ == "__main__":
2022-03-09 18:59:50 +01:00
main()