code refactor

This commit is contained in:
matixezor 2021-04-09 22:31:39 +02:00
parent 5f1780bc2f
commit 30b8fc3fae
3 changed files with 23 additions and 26 deletions

View File

@ -1,11 +1,10 @@
from const import SAPPER_IDLE
import pygame as pg
class Agent:
def __init__(self, x: int = 0, y: int = 0):
def __init__(self, x: int = 0, y: int = 0, direction: int = 1):
self.name = "Kapitan Bomba"
self.img = pg.image.load(SAPPER_IDLE[1])
self.dir = 1
self.img = SAPPER_IDLE[direction]
self.direction = direction
self.x = x
self.y = y

View File

@ -9,7 +9,6 @@ main_path = os.path.dirname(os.getcwd())
WIDTH = 800
HEIGHT = 800
IMAGES = []
for name, val in {
'grass_01': 0,
@ -37,14 +36,18 @@ for name, val in {
)
ICON = main_path + '/images/mine_icon.png'
SAPPER_IDLE = [main_path + '/images/sapper_idle/agent0.png',
main_path + '/images/sapper_idle/agent1.png',
main_path + '/images/sapper_idle/agent2.png',
main_path + '/images/sapper_idle/agent3.png']
SAPPER_IDLE = []
for name in [
'agent0',
'agent1',
'agent2',
'agent3',
]:
SAPPER_IDLE.append(pg.image.load(
main_path + '/images/sapper_idle/' + name + '.png'
))
#pg.image.load(main_path + '/images/sapper_idle/agent1.png')
#empty space
DEFAULT_FIELD = [

View File

@ -1,6 +1,4 @@
import pygame as pg
import os
main_path = os.path.dirname(os.getcwd())
from agent import Agent
from environment import Environment
@ -15,16 +13,16 @@ class GameUi:
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
def move(self):
coord = 'x' if self.agent.dir in (1, 3) else 'y'
shift = -1 if self.agent.dir in (2, 3) else 1
# print(coord, shift)
test = int(getattr(self.agent, coord) + shift)
coord = 'x' if self.agent.direction in (1, 3) else 'y'
shift = -1 if self.agent.direction in (2, 3) else 1
if coord == 'x':
if self.env.field[self.agent.y][test].number in (2, 3):
if self.env.field[self.agent.y][self.agent.x+shift].number in (2, 3):
return
elif coord == 'y':
if self.env.field[test][self.agent.x].number in (2, 3):
if self.env.field[self.agent.y+shift][self.agent.x].number in (2, 3):
return
for x in range(8):
setattr(
self.agent,
@ -33,7 +31,7 @@ class GameUi:
else int(getattr(self.agent, coord) + (shift/8))
)
self.update()
self.clock.tick(15)
self.clock.tick(20)
self.update()
def update(self):
@ -43,10 +41,7 @@ class GameUi:
self.screen.blit(self.agent.img, (self.agent.x * 80, self.agent.y * 80))
pg.display.update()
def rotate(self, dir):
self.agent.dir = (self.agent.dir + dir) % 4
#print(self.agent.dir)
self.agent.img = pg.image.load(SAPPER_IDLE[self.agent.dir])
def rotate(self, direction: int):
self.agent.direction = (self.agent.direction + direction) % 4
self.agent.img = SAPPER_IDLE[self.agent.direction]
self.update()