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 * """ This 2-dimensional array stores whether each block in the grid is walkable, a wall, or a chip. 0 = Walkable 1 = Wall 2 = Chip & Walkable 3 = Red Key 4 = Blue Key 5 = Yellow Key 6 = Green Key 7 = Red Door 8 = Blue Door 9 = Yellow Door 10 = Green Door 11 = Takoyaki """ print "Help Pink Guy find his takoyaki to get back to his jamming party!" walls = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 3, 11, 1, 0, 0, 0, 0, 0, 0, 0, 1, 11, 6, 1], [1, 11, 11, 1, 0, 0, 0, 0, 0, 0, 0, 1, 11, 11, 1], [1, 11, 11, 11, 1, 1, 0, 0, 0, 1, 1, 11, 11, 11, 1], [1, 11, 11, 10, 0, 0, 1, 10, 1, 0, 8, 11, 11, 11, 1], [1, 1, 1, 1, 0, 0, 0, 7, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 0, 0, 0, 5, 0, 0, 0, 1, 1, 1, 1], [1, 11, 11, 11, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11, 1], [1, 1, 1, 11, 0, 0, 0, 0, 0, 0, 0, 11, 1, 1, 1], [1, 4, 11, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 6, 1], [1, 11, 11, 1, 0, 0, 0, 0, 0, 0, 0, 1, 11, 11, 1], [1, 11, 11, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 11, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] # this is the dictionary storing all data related to the # exit door exitLock = {'row': 3, 'col': 7, 'isOpen': False } # this is the dictionary storing all data related to the # exit portal exitPortal = {'row': 2, 'col': 7, 'color': [255, 158, 166], 'colorInc': [1, 1, -1] } # this is the total number of chips to be collected in this # level totalChips = 11 totalTakoyaki = 34 # this is the length of the sides of each tile side = 36 # this is the width and height of the canvas width, height = 540, 504 # the number of rows and columns in the grid numRows = height / side numCols = width / side """ This function draws the lines of the grid. """ def drawLines(): stroke(0, 0, 0) for x in range(0, width, side): line(x, 0, x, height) for y in range(0, height, side): line(0, y, width, y) """ This function goes through each row and column of our walls 2-dimensional array and draws each wall, chip, door, key, and any other game pieces you use numbers in the 2-dimensional array to represent. """ def drawPuzzle(): global chipImage, redKey, yellowKey, blueKey, greenKey, Takoyaki for y in range(len(walls)): for x in range(len(walls[y])): if walls[y][x] == 1: # it's a wall stroke(64, 64, 64) fill(255, 158, 166) rect(side * x, side * y, side, side) elif walls[y][x] == 2: #it's a chip image(chipImage, side*x, side*y, side, side) elif walls[y][x] == 3: # it's a red key image(redKey, side*x, side*y, side, side) elif walls[y][x] == 4: # it's a blue key image(blueKey, side*x, side*y, side, side) elif walls[y][x] == 5: # it's a yellow key image(yellowKey, side*x, side*y, side, side) elif walls[y][x] == 6: # it's a green key image(greenKey, side*x, side*y, side, side) elif walls[y][x] == 7: # it's a red door drawDoor(x, y, [128, 0, 0], [204, 0, 0]) elif walls[y][x] == 8: # it's a blue door drawDoor(x, y, [0, 128, 128], [0, 255, 255]) elif walls[y][x] == 9: # it's a yellow door drawDoor(x, y, [204, 163, 0], [255, 204, 0]) elif walls[y][x] == 10: # it's a green door drawDoor(x, y, [0, 128, 0], [0, 255, 0]) elif walls[y][x] == 11: #it's gold! image(Takoyaki, side*x, side*y, side, side) """ This function takes the x (column) and y (row) of the current part of the walls array and sets the stroke and fill colors to the desired colors then draws the door at that location. """ def drawDoor(x, y, strokeColor, fillColor): r1,g1,b1 = strokeColor stroke(r1, g1, b1) r2, g2, b2 = fillColor fill(r2, g2, b2) rect(x * side, y * side, side, side) stroke(0, 0, 0) fill(0, 0, 0) ellipse((x * side) + (side/2), (y * (side)) + (side/2), 10, 10) """ This function draws the exit lock and the exit portal. If the exit lock has been opened, it will not be drawn. """ def drawExit(): # draw lock global exitPortal stroke(51, 0, 51) fill(102, 0, 102) if not exitLock['isOpen']: rect(side * exitLock['col'], side * exitLock['row'], side, side) stroke(0, 0, 0) fill(0, 0, 0) ellipse((exitLock['col'] * side) + (side/2), (exitLock['row'] * side) + (side/2), 10, 10) exitPortal['color'][0] = exitPortal['color'][0] + exitPortal['colorInc'][0] exitPortal['color'][1] = exitPortal['color'][1] + exitPortal['colorInc'][1] exitPortal['color'][2] = exitPortal['color'][2] + exitPortal['colorInc'][2] if exitPortal['color'][0] > 255 or exitPortal['color'][0] < 0: exitPortal['colorInc'][0] *= -1 if exitPortal['color'][1] > 255 or exitPortal['color'][1] < 0: exitPortal['colorInc'][1] *= -1 if exitPortal['color'][2] > 255 or exitPortal['color'][2] < 0: exitPortal['colorInc'][2] *= -1 stroke(exitPortal['color'][0], exitPortal['color'][1], exitPortal['color'][2]) fill(exitPortal['color'][0], exitPortal['color'][1], exitPortal['color'][2]) rect(side * exitPortal['col'] + 4, side * exitPortal['row'] + 4, side-6, side-6) """ This function draws all the text on the screen. This includes the timer, the number of chips collected, and the number of each key collected. """ def drawData(): global timeString # draw time fill(255, 255, 255) mil = millis() sec = mil / 1000 mil = mil % 1000 timeString = "Time: %03d" % (sec) textSize(25) text(timeString, 10, 30) # draw num Gold collected text("Takoyaki: " + str(player['Takoyaki']), 390, 30) # draw num blue keys and num red keys fill(255, 255, 255) text("Keys: ", 10,485) fill(255, 0, 0) text(str(player['keys'][0]), 85, 485) fill(0, 0, 255) text(str(player['keys'][1]), 115, 485) fill(255, 204, 0) text(str(player['keys'][2]), 145, 485) fill(0, 255, 0) text(str(player['keys'][3]), 175, 485) def blackout(): for row in range(len(walls)): for column in range(len(walls[row])): if player['row'] != row and player['column'] != column: stroke(255, 255, 255) fill(0, 0, 0) rect(column * side, row * side, side, side) """ This function draws the image of the player """ def drawPlayer(): x = player['column'] * side y = player['row'] * side image(player['image'], x, y, side, side) win() """ This function will move the player depending on the direcion that they were told to go in. """ def movePlayer(direction): row = player['row'] column = player['column'] if direction == UP: up = player['row'] - 1 if up >= 0 and walls[up][column] != 1: player['row'] = up elif direction == DOWN: down = player['row'] + 1 if down < numRows and walls[down][column] != 1: player['row'] = down elif direction == LEFT: left = player['column'] - 1 if left >= 0 and walls[row][left] != 1: player['column'] = left elif direction == RIGHT: right = player['column'] + 1 if right < numCols and walls[row][right] != 1: player['column'] = right checkKeysChipsGold() checkDoors(direction) unlock() """ This function checks if the player is standing on a spot with a key or a chip. If they are, then the player collects that key or chip. """ def checkKeysChipsGold(): x = player['column'] y = player['row'] if walls[y][x] == 11: player['Takoyaki'] += 1 walls[y][x] = 0 elif walls[y][x] in [3, 4, 5, 6]: # since player['keys'][0] is red, 3 - 3 = 0 # gives us the index index = walls[y][x] - 3 player['keys'][index] += 1 walls[y][x] = 0 """ This function checks if the player is standing on a spot with a door. If they are, it checks if they have enough keys to open that door. If they do, the door opens. If they don't, then the player is pushed back to the spot next to, below, or above the door. """ def checkDoors(direction): row = player['row'] column = player['column'] opened = False door = False if walls[row][column] in [7, 8, 9, 10]: door = True # since the player['keys'][0] is red, and 7 is a # red door, 7 - 7 = 0 gives us the index of red keys index = walls[row][column] - 7 if player['keys'][index] > 0: walls[row][column] = 0 opened = True # check if not a green key since a green key # can be used over and over again if index < 3: player['keys'][index] -= 1 if door and not opened: if direction == UP: player['row'] += 1 # go down elif direction == DOWN: player['row'] -= 1 # go up elif direction == LEFT: player['column'] += 1 # go right elif direction == RIGHT: player['column'] -= 1 # go left """ This function unlocks the exit lock if the player has collected the total amount of chips needed for the level. Otherwise, it pushes them back down. """ def unlock(): if (player['row'] == exitLock['row'] and player['column'] == exitLock['col']): if player['Takoyaki'] == totalTakoyaki: exitLock['isOpen'] = True else: player['row'] += 1 """ This function checks if the player is standing on the exit portal. If they are, then the game says 'You Win!', shows the amount of time it took to win and exits the game. """ def win(): if (player['row'] == exitPortal['row'] and player['column'] == exitPortal['col']): stroke(0, 0, 0) fill(0, 0, 0) rect(150, 175, 250, 100) stroke(0, 255, 0) fill(0, 255, 0) textSize(25) text("You Win!!", 225, 200) text(timeString, 160, 245) exitp() """ This is the processing package's setup function. In it, I set the size of the map, the background color, and I create the player. """ def setup(): global player, blueKey, redKey, yellowKey, greenKey, chipImage, Takoyaki size(width, height) background(191, 191, 191) player = { 'row': 6, 'column': 7, 'image': loadImage("http://kashyyy.oyosite.com/player+alive+2.png"), 'keys': [0, 0, 0, 0], 'chips': 0, 'Takoyaki' : 0 } blueKey = loadImage("http://kashyyy.oyosite.com/blue+key.png") redKey = loadImage("http://kashyyy.oyosite.com/red+key.png") greenKey = loadImage("http://kashyyy.oyosite.com/green+key.png") yellowKey = loadImage("http://kashyyy.oyosite.com/yellow+key.png") chipImage = loadImage("http://sabrinaw.oyosite.com/chip3.png") Takoyaki = loadImage("http://kashyyy.oyosite.com/takoyaki.png") """ This is the processing package's draw function. It is called once every frame. This will re-draw the background to clear the previous frame from the canvas, then draw the lines for the grid. It then draws all the data from the walls 2-D array, the doors, the keys, the exit, the player, and then all the data (the time, keys collected and chips collected). """ def draw(): background(232, 187, 191) drawLines() drawPuzzle() drawExit() blackout() drawPlayer() drawData() """ This processing function listens for key press events. Since the player moves when the key is pressed, they are able to hold the corresponding arrow key down and continue to move in that direction until hitting a wall. The keyCode 32 is the space bar. When it is pressed, we exit the program. """ def keyPressed(): if keyboard.keyCode in [LEFT, RIGHT, UP, DOWN]: movePlayer(keyboard.keyCode) if keyboard.keyCode == 32: # space key pressed exitp() run()
TakoyakiChallenge.py
( around 327 lines python code )
Published By:
Kashyyy
Published on
2017-07-13T00:16:36Z
Python Mini
- an
OYOclass
application,
own your own class today
.
Run
Result
×
Error message shows here