Text Size
Text Size
Exit Full Screen
Python Mini
Show
Source Code
View Source Code in Full Screen
Open in New Tab
from processing import * class Tile: instances = [] def __init__(self,obj,gridx,gridy,facing=DOWN,x=None,y=None): self.obj = obj self.gridx = gridx self.gridy = gridy self.facing = facing self.tx = float(gridx*50) self.ty = float(gridx*50) if x == None: self.x = float(gridx*50) else: self.x = x if y == None: self.y = float(gridy*50) else: self.y = y Tile.instances.append(self) def moved(self,direction): block = 0 for i in Tile.instances: if direction == UP: check = self.gridx == i.gridx and self.gridy == i.gridy+1 elif direction == DOWN: check = self.gridx == i.gridx and self.gridy == i.gridy-1 elif direction == LEFT: check = self.gridx == i.gridx+1 and self.gridy == i.gridy elif direction == RIGHT: check = self.gridx == i.gridx-1 and self.gridy == i.gridy if check: if 'push' in prop[i.obj]: block = i.moved(direction) elif 'stop' in prop[i.obj]: block = 1 if block == 0: if direction == UP: movequeue.append([self,UP]) self.facing = UP elif direction == DOWN: movequeue.append([self,DOWN]) self.facing = DOWN elif direction == LEFT: movequeue.append([self,LEFT]) self.facing = LEFT elif direction == RIGHT: movequeue.append([self,RIGHT]) self.facing = RIGHT return 0 else: return 1 ''' grid = [ [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ','w',' ','w',' ',' ',' '], [' ',' ',' ',' ',' ',' ','k',' ',' ',' '], [' ',' ',' ',' ','r',' ','r',' ',' ',' '], [' ',' ','r',' ',' ',' ',' ',' ',' ',' '], [' ',' ','r',' ','b',' ',' ',' ',' ',' '], [' ',' ','r',' ',' ',' ',' ',' ',' ',' '], [' ',' ','r',' ',' ',' ','r',' ',' ',' '], [' ',' ',' ',' ',' ',' ','k',' ',' ',' '], [' ',' ',' ',' ',' ',' ','w',' ',' ',' '],] ''' grid = [ ['w','w','w','w','w','w','w','w','w','w'], ['w',' ',' ',' ',' ',' ',' ',' ',' ','w'], ['w',' ',' ',' ','f',' ',' ',' ',' ','w'], ['w',' ',' ',' ',' ',' ',' ',' ',' ','w'], ['w','w','w','w','w','w','w','w','w','w'], [' ',' ',' ',' ',' ',' ',' ',' ',' ','w'], [' ',' ','r',' ','r',' ','r',' ',' ','w'], ['w','k>',' ',' ',' ',' ',' ','k<','w','w'], [' ',' ',' ',' ','b',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],] play = [] order = ['empty','wall','flag','keke','rock','baba'] global movequeue movequeue = [] names = { 'b':'baba', 'r':'rock', 'w':'wall', 'k':'keke', ' ':'empty', 'f':'flag', } prop = { 'baba':['you'], 'rock':['push'], 'wall':['stop'], 'keke':['move'], 'empty':[], 'flag':[], } color = { 'baba':0xffffffff, 'rock':0xff553300, 'wall':0xff444444, 'keke':0xffff7700, 'empty':0x00000000, 'flag':0xffffff00, } directions = { '^':UP, 'v':DOWN, '>':RIGHT, '<':LEFT } def exphone(var,target,mult,snap): if var < snap+target and var > target-snap and var != target: var = target else: var += (target-var)/mult return var def outline(texta,px,py,weight=2,outcolor=0,fillcolor=255): fill(outcolor); for x in range(weight): for y in range(weight): text(texta, px-x+1,py-y+1) fill(fillcolor); text(texta, px,py); def clearQueue(): for i in movequeue: if i[1] == UP: i[0].gridy -= 1 elif i[1] == DOWN: i[0].gridy += 1 elif i[1] == LEFT: i[0].gridx -= 1 elif i[1] == RIGHT: i[0].gridx += 1 del movequeue[:] def updEmpty(): for it in Tile.instances: if it.obj != 'empty': continue delete = False for other in Tile.instances: if other.obj == 'empty': continue if it.gridx == other.gridx and it.gridy == other.gridy: delete = True if delete: Tile.instances.remove(it) def step(): if keyboard.keyCode in [UP,DOWN,LEFT,RIGHT]: for it in Tile.instances: if 'you' in prop[it.obj]: it.moved(keyboard.keyCode) clearQueue() if keyboard.keyCode in [UP,DOWN,LEFT,RIGHT,32]: for it in Tile.instances: if 'move' in prop[it.obj]: k = it.moved(it.facing) if k == 1: if it.facing == UP: it.facing = DOWN elif it.facing == DOWN: it.facing = UP elif it.facing == LEFT: it.facing = RIGHT elif it.facing == RIGHT: it.facing = LEFT it.moved(it.facing) clearQueue() def setup(): size(500,500) noStroke() textSize(20) for y in range(len(grid)): for x in range(len(grid[y])): current = grid[y][x] current = current.split(',') if current == [' ']: pass else: for i in current: if i[0] in names.keys() and i not in names.values(): if len(i) == 1: Tile(names[i[0]],x,y) else: Tile(names[i[0]],x,y,directions[i[1]]) else: Tile(i,x,y) def keyPressed(): step() def drawGrid(): occupied = [] for target in order: for it in Tile.instances: if it.obj == target: fill(color[it.obj]) if [it.gridx,it.gridy] not in occupied: it.tx = it.gridx*50 it.ty = it.gridy*50 occupied.append([it.gridx,it.gridy]) else: k = 0 while True: k += 1 if [it.gridx,it.gridy,k] not in occupied: occupied.append([it.gridx,it.gridy,k]) it.tx = it.gridx*50+2.5*k it.ty = it.gridy*50-5*k temp = color[it.obj] for i in range(k): temp += 0x00111111 if temp > 0xffffffff: temp -= 0x00111111 break fill(temp) break rect(it.x,it.y,50,50) #if it.x != it.gridx*50 or it.y != it.gridy*50: # outline("{n:.2f}".format(n = it.x-it.gridx*50),it.x+10,it.y+20,4) # outline("{n:.2f}".format(n = it.y-it.gridy*50),it.x+10,it.y+40,4) def updateAnim(): for it in Tile.instances: it.x = exphone(it.x,it.tx,3.0,1) it.y = exphone(it.y,it.ty,3.0,1) def draw(): background(0) updateAnim() drawGrid() run()
sokobanlike.py
( around 224 lines python code )
Published By:
luyfru
Published on
2023-09-21T18:53:12Z
Python Mini
- an
OYOclass
application,
own your own class today
.
Run
Result
×
Error message shows here