This commit is contained in:
karoel2 2020-04-06 21:22:55 +02:00
parent d1fe4bdc5a
commit f68cbff936

259
nauka2.py
View File

@ -1,12 +1,9 @@
ALPHA = (0, 255, 0)
# Import the pygame module
import pygame import pygame
import sys import sys
import os # new code below import os
background_image = pygame.image.load("images/tile.jpg") import time
# Import pygame.locals for easier access to key coordinates background_image = pygame.image.load("./images/tile.jpg")
# Updated to conform to flake8 and black standards
from pygame.locals import ( from pygame.locals import (
K_UP, K_UP,
K_DOWN, K_DOWN,
@ -17,64 +14,14 @@ from pygame.locals import (
QUIT, QUIT,
) )
# Define constants for the screen width and height
SCREEN_WIDTH = 564 SCREEN_WIDTH = 564
SCREEN_HEIGHT = 564 SCREEN_HEIGHT = 564
# Define a player object by extending pygame.sprite.Sprite def draw():
# The surface drawn on the screen is now an attribute of 'player' screen.blit(background_image, [0, 0])
# class Player(pygame.sprite.Sprite): screen.blit(player.surf, player.rect)
# def __init__(self): player_list.draw(screen)
# super(Player, self).__init__() pygame.display.flip()
# self.surf = pygame.Surface((56, 56))#75, 25
# self.surf.fill((255, 255, 255))
# self.rect = self.surf.get_rect()
class Dishes:
#to do
x=0
y=0
def __int__(self, x, y):
self.x = x
self.y = y
def draw(self, surface, image):
surface.blit(image, (self.x, self.y))
class Clients:
#to do
x = 0
y = 0
def __int__(self, x, y):
self.x = x
self.y = y
def draw(self, surface, image):
surface.blit(image, (self.x, self.y))
class Kitchen:
#to do or delete
pass
class Order:
#to do
x = 0
y = 0
def __int__(self, x, y, id):
self.x = x
self.y = y
self.id = id
class Player(pygame.sprite.Sprite): class Player(pygame.sprite.Sprite):
''' '''
@ -90,6 +37,7 @@ class Player(pygame.sprite.Sprite):
self.images2 = [] self.images2 = []
self.images3 = [] self.images3 = []
self.images4 = [] self.images4 = []
self.imgList=[]
for i in range(1,5): for i in range(1,5):
img = pygame.image.load(os.path.join('images','row-4-col-' + str(i) + '.jpg')).convert() img = pygame.image.load(os.path.join('images','row-4-col-' + str(i) + '.jpg')).convert()
@ -109,154 +57,89 @@ class Player(pygame.sprite.Sprite):
self.image = self.images1[0] self.image = self.images1[0]
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
def onScreen(self):
# Move the sprite based on user keypresses
#60
def update(self, pressed_keys):
#when the update method is called, we will increment the index
#finally we will update the image that will be displayed
# self.image = self.images[self.index]
nkey = ''
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, 56)
nkey = 'up'
if self.pkey == nkey:
self.index += 1
if self.index >= 4:
self.index = 0
else:
self.pkey = nkey
self.index = 0
self.image = self.images1[self.index]
elif pressed_keys[K_UP]:
self.rect.move_ip(0, -56)
nkey = 'down'
if self.pkey == nkey:
self.index += 1
if self.index >= 4:
self.index = 0
else:
self.pkey = nkey
self.index = 0
self.image = self.images4[self.index]
elif pressed_keys[K_LEFT]:
self.rect.move_ip(-56, 0)
nkey = 'left'
if self.pkey == nkey:
self.image = self.images3[self.index]
self.index += 1
if self.index >= 4:
self.index = 0
else:
self.pkey = nkey
self.index = 0
elif pressed_keys[K_RIGHT]:
self.rect.move_ip(56, 0)
nkey = 'right'
if self.pkey == nkey:
self.image = self.images2[self.index]
self.index += 1
if self.index >= 4:
self.index = 0
else:
self.pkey = nkey
self.index = 0
else:
return False
#
# if self.index >= len(self.images):
# self.index = 0
# if self.pkey == nkey:
# self.image = self.images[self.index]
# self.index += 1
# else:
# self.pkey = nkey
# Keep player on the screen
if self.rect.left < 0: if self.rect.left < 0:
self.rect.left = 0 self.rect.left = 27
if self.rect.right > SCREEN_WIDTH: if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH self.rect.right = SCREEN_WIDTH-28
if self.rect.top <= 0: if self.rect.top <= 0:
self.rect.top = 0 self.rect.top = 27
if self.rect.bottom >= SCREEN_HEIGHT: if self.rect.bottom >= SCREEN_HEIGHT:
self.rect.bottom = SCREEN_HEIGHT self.rect.bottom = SCREEN_HEIGHT-28
def step(self, index, newKey):
self.image = self.imgList[index]
if newKey == "down":
self.rect.move_ip(0,7)
elif newKey == 'up':
self.rect.move_ip(0, -7)
elif newKey == 'left':
self.rect.move_ip(-7, 0)
elif newKey =='right':
self.rect.move_ip(7, 0)
def same(self, newKey, img):
print(self.pkey, " ", newKey)
if self.pkey == newKey:
self.move(newKey)
else:
self.pkey = newKey
self.index = 0
self.imgList = img
self.image = self.imgList[0]
def move(self, newKey):
nextFrame = time.clock()
frame = 0
i = 0
while True:
if time.clock() > nextFrame:
frame = (frame + 1) % 4
nextFrame += 0.1
player.step(frame, newKey)
draw()
i += 1
if i >= 8:
self.onScreen()
break
def update(self, pressed_keys):
newKey = ''
if pressed_keys[K_DOWN]:
newKey = 'down'
self.same(newKey, self.images1)
elif pressed_keys[K_UP]:
newKey = 'up'
self.same(newKey, self.images4)
elif pressed_keys[K_LEFT]:
newKey = 'left'
self.same(newKey, self.images3)
elif pressed_keys[K_RIGHT]:
newKey = 'right'
self.same(newKey, self.images2)
#if the index is larger than the total images
return True
# Initialize pygame
pygame.init() pygame.init()
clock = pygame.time.Clock() clock = pygame.time.Clock()
# Create the screen object
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Instantiate player. Right now, this is just a rectangle.
player = Player() player = Player()
player.rect.x = 25 # go to x player.rect.x = 22
player.rect.y = 25 # go to y player.rect.y = 22
player_list = pygame.sprite.Group() player_list = pygame.sprite.Group()
player_list.add(player) player_list.add(player)
# Variable to keep the main loop running
running = True running = True
# Main loop
while running: while running:
# for loop through the event queue
for event in pygame.event.get(): for event in pygame.event.get():
# Check for KEYDOWN event
if event.type == KEYDOWN: if event.type == KEYDOWN:
# If the Esc key is pressed, then exit the main loop
if event.key == K_ESCAPE: if event.key == K_ESCAPE:
running = False running = False
# Check for QUIT event. If QUIT, then set running to false.
elif event.type == QUIT: elif event.type == QUIT:
running = False running = False
# guard = False
# while guard is False:
# Get the set of keys pressed and check for user input
pressed_keys = pygame.key.get_pressed() pressed_keys = pygame.key.get_pressed()
# Update the player sprite based on user keypresses player.update(pressed_keys)
guard = player.update(pressed_keys)
# Fill the screen with black draw()
screen.blit(background_image, [0, 0])
#screen.fill((0, 0, 0))
# Draw the player on the screen
screen.blit(player.surf, player.rect)
player_list.draw(screen) # draw player
# Update the display
pygame.display.flip()
clock.tick(30)