Antworten |
Thema | maze game |
Autor | Jarka |
Eingangsdatum | 2021-01-04 23:43:13.0 |
Mitteilung | I'll send you a labyrinth with TigerJython:
# Labyrinth.py
from gamegrid import *
def keyCallback(e): global isGameOver if isGameOver: return next = None keyCode = e.getKeyCode() if keyCode == KeyEvent.VK_LEFT: if bug.getLocation() == startLocation: return True next = bug.getLocation().getNeighbourLocation(180) bug.setDirection(180) if keyCode == KeyEvent.VK_UP: next = bug.getLocation().getNeighbourLocation(270) bug.setDirection(270) if keyCode == KeyEvent.VK_RIGHT: next = bug.getLocation().getNeighbourLocation(0) bug.setDirection(0) if keyCode == KeyEvent.VK_DOWN: next = bug.getLocation().getNeighbourLocation(90) bug.setDirection(90)
if next != None and canMove(next): bug.setLocation(next) refresh() if next != None and next == exitLocation: isGameOver = True addActor(Actor("sprites/gameover.gif"), Location(15, 15)) refresh()
def canMove(location): c = bg.getColor(location) return c != makeColor("black")
def drawMaze(): for x in range(nbHorzCells): for y in range(nbVertCells): location = Location(x, y) if maze.isWall(location): bg.fillCell(location, Color.black) else: bg.fillCell(location, Color.white) return maze # --------------------- main ----------------------------------------- nbHorzCells = 31 # must be odd nbVertCells = 31 # ditto cellSize = 18 isGameOver = False makeGameGrid(nbHorzCells, nbVertCells, cellSize, False, keyPressed = keyCallback) bg = getBg() exitLocation = Location(nbHorzCells - 1, nbVertCells - 2) maze = GGMaze(nbHorzCells, nbVertCells) startLocation = maze.getStartLocation() drawMaze() bug = Actor(True, "sprites/ladybug.gif") addActor(bug, startLocation) show()
|
|
Thema | Maze Game |
Autor | Jarka |
Eingangsdatum | 2021-01-04 23:47:26.0 |
Mitteilung | Here a second maze Game:
from gamegrid import *
def createMaze(): global maze maze = GGMaze(11, 11) for x in range(11): for y in range(11): loc = Location(x, y) if maze.isWall(loc): getBg().fillCell(loc, Color(0, 50, 0)) else: getBg().fillCell(loc, Color(255, 228, 196)) refresh()
def getNeighbours(node): neighbours = [] for loc in node.getNeighbourLocations(0.5): if isInGrid(loc) and not maze.isWall(loc): neighbours.append(loc) return neighbours def search(node): global targetFound, manual if targetFound: return visited.append(node) # push alien.setLocation(node) refresh() delay(500) if manual: if getKeyCodeWait(True) == 10: #Enter setTitle("Finding target...") manual = False
# Check for termination if node == exitLocation: targetFound = True for neighbour in getNeighbours(node): if neighbour not in visited: backSteps = [node] backStepsList.append(backSteps) backSteps.append(neighbour)
search(neighbour) # recursive call
backSteps.reverse() if not targetFound: for loc in backSteps[1:]: setTitle("Must go back...") alien.setLocation(loc) refresh() delay(500) if manual: setTitle("Went back. Press key...") else: setTitle("Went back. Find target...") backStepsList.pop() visited.pop() # pop
manual = True targetFound = False visited = [] backStepsList = [] makeGameGrid(11, 11, 40, False) setTitle("Press a key. (<Enter> for automatic") show() createMaze() startLocation = maze.getStartLocation() exitLocation = maze.getExitLocation() alien = Actor("sprites/alieng.png") addActor(alien, startLocation) search(startLocation) setTitle("Target found")
|
|
|