DSZI_2020_Projekt/Restaurant/main.py
Sara Kowalska beec67e2ff Środowisko v.1.0
Wyświetlanie kraty, stworzenie obiektu bota (Waiter) wraz z atrybutami i metodami (razem z testowym poruszaniem się przy pomocy klawiszy) oraz obiektu stół (Table).
Wyświetlony testowo 1 stół oraz bot na środku planszy.
2020-03-23 10:49:27 +01:00

119 lines
3.2 KiB
Python

import pygame
class Table(object):
def __init__(self, pos):
self.pos = pos
def move(self, newx, newy):
self.pos[0] = newx
self.pos[1] = newy
def draw(self, surface):
image = pygame.image.load(r'Table-croped.png')
image = pygame.transform.scale(image, (sizeBetween - 1, sizeBetween - 1))
i = self.pos[0]
j = self.pos[1]
surface.blit(image, (i*sizeBetween+1, j*sizeBetween+1))
class Waiter(object):
def __init__(self, color, pos):
self.color = color
self.pos = pos
self.dirnx = 0
self.dirny = 1
def move(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
keys = pygame.key.get_pressed()
for key in keys:
if keys[pygame.K_LEFT]:
self.dirnx = -1
self.dirny = 0
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
break
# te break musiałam dodać, bo w przeciwnym wypadku zamiast jednego kroku robił 303 - jeden za drugim
# nie wiem dlaczego tak było, po zmianie sterowania z klawiatury na bota samego w sobie nie powinno
# być z tym problemów, to jest na razie tylko, żeby pokazać, że ten ruch jest
elif keys[pygame.K_RIGHT]:
self.dirnx = 1
self.dirny = 0
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
break
elif keys[pygame.K_UP]:
self.dirnx = 0
self.dirny = -1
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
break
elif keys[pygame.K_DOWN]:
self.dirnx = 0
self.dirny = 1
self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny)
break
def reset(self, pos):
self.pos = pos
def draw(self, surface):
i = self.pos[0]
j = self.pos[1]
pygame.draw.rect(surface, self.color, (i*sizeBetween+1, j*sizeBetween+1, sizeBetween-1, sizeBetween-1))
def drawGrid(width, rows, surface):
x = 0
y = 0
for i in range(rows):
x = x + sizeBetween
y = y + sizeBetween
pygame.draw.line(surface, (255, 255, 255), (x, 0), (x, width))
pygame.draw.line(surface, (255, 255, 255), (0, y), (width, y))
def redrawWindow(surface):
surface.fill(beige)
bot.draw(surface)
table1.draw(surface)
drawGrid(width, rows, surface)
pygame.display.update()
def main():
global width, rows, bot, beige, white, black, sizeBetween, table1
# skróty do kolorów
beige = (255, 205, 178)
white = (255, 255, 255)
black = (0, 0, 0)
width = 600
rows = 15
sizeBetween = width // rows
window = pygame.display.set_mode((width, width))
bot = Waiter((255, 0, 0), [7, 7])
table1 = Table([5, 5])
flag = True
clock = pygame.time.Clock()
while flag:
pygame.time.delay(50)
clock.tick(10)
bot.move()
redrawWindow(window)
main()