Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eng V #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added box.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added done.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added floor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions peli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __init__(self):

self.korkeus = len(self.kartta)
self.leveys = len(self.kartta[0])
self.skaala = self.kuvat[0].get_width()
self.skaala = self.kuvat[0].get_width()#skaala = scale

nayton_korkeus = self.skaala * self.korkeus
nayton_leveys = self.skaala * self.leveys
Expand All @@ -19,7 +19,7 @@ def __init__(self):

pygame.display.set_caption("Sokoban")

self.silmukka()
self.silmukka()#self.main_loop()

def lataa_kuvat(self):
self.kuvat = []
Expand Down
124 changes: 124 additions & 0 deletions peliEnglish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import pygame

class Sokoban:
def __init__(self):
pygame.init()

self.load_images()
self.new_game()

self.height = len(self.map)
self.width = len(self.map[0])
self.scale = self.images[0].get_width()

window_height = self.scale * self.height
window_width = self.scale * self.width
self.window = pygame.display.set_mode((window_width, window_height + self.scale))

self.game_font = pygame.font.SysFont("Arial", 24)

pygame.display.set_caption("Sokoban")

self.main_loop()

def load_images(self):
self.images = []
for name in ["floor", "wall", "target", "box", "robot", "done", "target_robot"]:
self.images.append(pygame.image.load(name + ".png"))

def new_game(self):
#each matrix pos represents an actrual object from self.images

self.map = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],
[1, 2, 3, 0, 0, 0, 1, 0, 0, 1, 2, 3, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 2, 3, 0, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1],
[1, 0, 4, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

self.moves = 0

def main_loop(self):
#keep adding executions going next level
while True:
self.check_events()
self.draw_window()

def check_events(self):#decides real game moves
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.move(0, -1)
if event.key == pygame.K_RIGHT:
self.move(0, 1)
if event.key == pygame.K_UP:
self.move(-1, 0)
if event.key == pygame.K_DOWN:
self.move(1, 0)

if event.type == pygame.QUIT:
exit()
def move(self, move_y, move_x):
robot_old_y, robot_old_x = self.find_robot()
robot_new_y = robot_old_y + move_y
robot_new_x = robot_old_x + move_x

if self.map[robot_new_y][robot_new_x] == 1:
return

if self.map[robot_new_y][robot_new_x] in [3, 5]:
box_new_y = robot_new_y + move_y
box_new_x = robot_new_x + move_x

if self.map[box_new_y][box_new_x] in [1, 3, 5]:
return

self.map[robot_new_y][robot_new_x] -= 3
self.map[box_new_y][box_new_x] += 3

self.map[robot_old_y][robot_old_x] -= 4
self.map[robot_new_y][robot_new_x] += 4

self.moves += 1

def find_robot(self ):
for y in range(self.height):
for x in range(self.width):
if self.map[y][x] in [4, 6]:
return (y, x)

def draw_window(self):
self.window.fill((0, 0, 0))

for y in range(self.height):
for x in range(self.width):
square = self.map[y][x]
self.window.blit(self.images[square], (x * self.scale, y * self.scale))

game_text = self.game_font.render("Moves: " + str(self.moves), True, (255, 0, 0))
self.window.blit(game_text, (25, self.height * self.scale + 10))

game_text = self.game_font.render("F2 = new game", True, (255, 0, 0))
self.window.blit(game_text, (200, self.height * self.scale + 10))

game_text = self.game_font.render("Esc = exit game", True, (255, 0, 0))
self.window.blit(game_text, (400, self.height * self.scale + 10))

if self.game_solved():
game_text = self.game_font.render("Congratulations, you solved the game!", True, (255, 0, 0))
game_text_x = self.scale * self.width / 2 - game_text.get_width() / 2
game_text_y = self.scale * self.height / 2 - game_text.get_height() / 2
pygame.draw.rect(self.window, (0, 0, 0), (game_text_x, game_text_y, game_text.get_width(), game_text.get_height()))
self.window.blit(game_text, (game_text_x, game_text_y))

pygame.display.flip()

def game_solved(self):
for y in range(self.height):
for x in range(self.width):
if self.map[y][x] in [2, 6]:
return False
return True

if __name__ == "__main__":
Sokoban()
Binary file added robot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added target.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added target_robot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wall.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.