|
| 1 | +# https://github.com/Wireframe-Magazine/Wireframe56 |
| 2 | +# Wireframe #56: |
| 3 | +import pgzrun |
| 4 | +import pickle |
| 5 | + |
| 6 | +editorState = True |
| 7 | +editorEnabled = True |
| 8 | + |
| 9 | +if editorState: |
| 10 | + WIDTH = 1000 |
| 11 | + |
| 12 | +gameState = count = 0 |
| 13 | +editItem = "blank" |
| 14 | +editorMessage = "" |
| 15 | +editorMessageCount = 0 |
| 16 | + |
| 17 | +blockTypes = [ |
| 18 | + Actor('blank', center=(900, 250)), |
| 19 | + Actor('soil', center=(900, 300)), |
| 20 | + Actor('rock', center=(900, 350)), |
| 21 | + Actor('gem', center=(900, 400)), |
| 22 | + Actor('wall', center=(900, 450)) |
| 23 | + ] |
| 24 | + |
| 25 | +loadButton = Actor('load', center=(850, 580)) |
| 26 | +saveButton = Actor('save', center=(950, 580)) |
| 27 | +items = [[] for _ in range(14)] |
| 28 | +gems = collected = 0 |
| 29 | +rockford = Actor('rockford-1', center=(60, 100)) |
| 30 | + |
| 31 | +def draw(): |
| 32 | + screen.fill((0,0,0)) |
| 33 | + if gems == 0 and collected > 0: infoText("YOU COLLECTED ALL THE GEMS!") |
| 34 | + else: infoText("GEMS : "+ str(collected)) |
| 35 | + for r in range(0, 14): |
| 36 | + for c in range(0, 20): |
| 37 | + if items[r][c] != "" and items[r][c] != "rockford": |
| 38 | + screen.blit(items[r][c], ((c*40), 40+(r*40))) |
| 39 | + if gameState == 0 or (gameState == 1 and count%4 == 0): rockford.draw() |
| 40 | + drawEditor() |
| 41 | + |
| 42 | +def update(): |
| 43 | + global count,gems |
| 44 | + mx = my = 0 |
| 45 | + if count%10 == 0: |
| 46 | + gems = 0 |
| 47 | + for r in range(13, -1, -1): |
| 48 | + for c in range(19, -1, -1): |
| 49 | + if items[r][c] == "gem": |
| 50 | + gems += 1 |
| 51 | + if items[r][c] == "rockford": |
| 52 | + if keyboard.left: mx = -1 |
| 53 | + if keyboard.right: mx = 1 |
| 54 | + if keyboard.up: my = -1 |
| 55 | + if keyboard.down: my = 1 |
| 56 | + if items[r][c] == "rock": testRock(r,c) |
| 57 | + rockford.image = "rockford"+str(mx) |
| 58 | + if gameState == 0 and editorState == False: moveRockford(mx,my) |
| 59 | + count += 1 |
| 60 | + |
| 61 | +def on_mouse_down(pos): |
| 62 | + global editItem |
| 63 | + if editorState: |
| 64 | + c = int(pos[0]/40) |
| 65 | + r = int((pos[1]-40)/40) |
| 66 | + if loadButton.collidepoint(pos): loadMap() |
| 67 | + if saveButton.collidepoint(pos): saveMap() |
| 68 | + if r > 0 and r < 14 and c > 0 and c < 20: |
| 69 | + if editItem != "blank": |
| 70 | + items[r][c] = editItem |
| 71 | + else : items[r][c] = "" |
| 72 | + else: |
| 73 | + for b in range(0, len(blockTypes)): |
| 74 | + if blockTypes[b].collidepoint(pos): |
| 75 | + editItem = blockTypes[b].image |
| 76 | + |
| 77 | +def on_key_down(key): |
| 78 | + global editorState, gameState, rockford, collected, gems |
| 79 | + if key == keys.SPACE and editorEnabled: |
| 80 | + editorState = not editorState |
| 81 | + if key == keys.ESCAPE: |
| 82 | + gems = collected = gameState = 0 |
| 83 | + rockford = Actor('rockford-1', center=(60, 100)) |
| 84 | + loadMap() |
| 85 | + |
| 86 | +def infoText(t): |
| 87 | + screen.draw.text(t, center = (400, 20), owidth=0.5, ocolor=(255,255,255), color=(255,0,255) , fontsize=40) |
| 88 | + |
| 89 | +def moveRockford(x,y): |
| 90 | + global collected |
| 91 | + rx, ry = int((rockford.x-20)/40), int((rockford.y-40)/40) |
| 92 | + if items[ry+y][rx+x] != "rock" and items[ry+y][rx+x] != "wall": |
| 93 | + if items[ry+y][rx+x] == "gem": collected +=1 |
| 94 | + items[ry][rx], items[ry+y][rx+x] = "", "rockford" |
| 95 | + rockford.pos = (rockford.x + (x*40), rockford.y + (y*40)) |
| 96 | + if items[ry+y][rx+x] == "rock" and y == 0: |
| 97 | + if items[ry][rx+(x*2)] == "": |
| 98 | + items[ry][rx], items[ry][rx+(x*2)], items[ry+y][rx+x] = "", "rock", "rockford" |
| 99 | + rockford.x += x*40 |
| 100 | + |
| 101 | +def testRock(r,c): |
| 102 | + if items[r+1][c] == "": |
| 103 | + moveRock(r,c,r+1,c) |
| 104 | + elif items[r+1][c] == "rock" and items[r+1][c-1] == "" and items[r][c-1] == "": |
| 105 | + moveRock(r,c,r+1,c-1) |
| 106 | + elif items[r+1][c] == "rock" and items[r+1][c+1] == "" and items[r][c+1] == "": |
| 107 | + moveRock(r,c,r+1,c+1) |
| 108 | + |
| 109 | +def moveRock(r1,c1,r2,c2): |
| 110 | + global gameState |
| 111 | + items[r1][c1], items[r2][c2] = "", items[r1][c1] |
| 112 | + if items[r2+1][c2] == "rockford": gameState = 1 |
| 113 | + |
| 114 | +def drawEditor(): |
| 115 | + global editorMessageCount |
| 116 | + screen.draw.text("EDITOR", center = (900, 20), owidth=0.5, ocolor=(255,255,255), color=(0,0,255) , fontsize=40) |
| 117 | + if editorState: screen.draw.text("ON", center = (900, 50), owidth=0.5, ocolor=(255,255,255), color=(255,0,0) , fontsize=40) |
| 118 | + for b in range(0, len(blockTypes)): |
| 119 | + blockTypes[b].draw() |
| 120 | + if editItem != "": |
| 121 | + screen.blit(editItem,(880,100)) |
| 122 | + loadButton.draw() |
| 123 | + saveButton.draw() |
| 124 | + if editorMessageCount > 0: |
| 125 | + screen.draw.text(editorMessage, center = (400, 300), owidth=0.5, ocolor=(255,255,255), color=(0,0,255) , fontsize=40) |
| 126 | + editorMessageCount -= 1 |
| 127 | + |
| 128 | +def loadMap(): |
| 129 | + global items, rockford, editorMessage, editorMessageCount |
| 130 | + try: |
| 131 | + with open ('mymap.map', 'rb') as fp: |
| 132 | + items = pickle.load(fp) |
| 133 | + editorMessage = "MAP LOADED" |
| 134 | + editorMessageCount = 200 |
| 135 | + except IOError: |
| 136 | + editorMessage = "DEFAULT MAP LOADED" |
| 137 | + editorMessageCount = 200 |
| 138 | + for r in range(0, 14): |
| 139 | + for c in range(0, 20): |
| 140 | + itype = "soil" |
| 141 | + if(r == 0 or r == 13 or c == 0 or c == 19): itype = "wall" |
| 142 | + items[r].append(itype) |
| 143 | + items[1][1] = "rockford" |
| 144 | + |
| 145 | +def saveMap(): |
| 146 | + global editorMessage, editorMessageCount |
| 147 | + try: |
| 148 | + with open('mymap.map', 'wb') as fp: |
| 149 | + pickle.dump(items, fp) |
| 150 | + editorMessage = "MAP SAVED" |
| 151 | + editorMessageCount = 200 |
| 152 | + except IOError: |
| 153 | + editorMessage = "ERROR SAVING MAP" |
| 154 | + editorMessageCount = 200 |
| 155 | + |
| 156 | +loadMap() |
| 157 | +pgzrun.go() |
0 commit comments