![]() |
#1 | ||
![]() ![]() ![]() ![]() Join Date: Aug 2005
Location: Ofverby, Finland
Posts: 12
|
![]() So, I just got the idea to practice my Python skills a bit and try the Console module there is for Python.
I wanted to get it under 4kB, so that's why the variable names are so strange. I wanted to keep the structure readable, though. (I could make it much shorter, but it was just a strange idea.) The Console module (http://effbot.org/downloads/#console) is for Windows only, but I'm sure that this can be converted quite easily for *nix etc. There are certainly some very strange things in the code. Also, it's a known bug that the game crashes with too big width and/or height. Probably something in reveal(). Code:
import Console import sys import random import string import time def game(w,h,m): ****def setm(): ********x = random.randint(1,w-2) ********y = random.randint(1,h-2) ********if r[x][y] != ' ': setm() ********else: r[x][y] = '*' **** ****def sett(x,y,s,o): ********c.text(x,y,s,o) ********k[x][y] = s ******** ****def mcur(x,y): ********cx = cur[0] ********cy = cur[1] ********if cx+x < 1 or cx+x > w-2 or cy+y < 1 or cy+y > h-2: return cur ********c.text(cx,cy,k[cx][cy],col[k[cx][cy]]) ********c.text(cx+x,cy+y,k[cx+x][cy+y],112)******** ********return (cx+x,cy+y) ******** ****def flag(x,y): ********if k[x][y] == 'F': ************sett(x,y,'.',127) ************return f-1 ********if k[x][y] != '.': return f**** ********sett(x,y,'F',124) ********if (f+1 == m): checkvictory(1) ********return f+1 ******** ****def checkvictory(m=0): ********if m == 1: ************v = 1; ************for y in range(h): ****************for x in range(w): ********************if k[x][y] == 'F' and r[x][y] != '*': v = 0 ************if (v != 0): victory() ************ ****def victory(): ********c.text(1,h+1,"You win",13) ********c.text(1,h+2,"Press a key",5) ********while 1: ************if c.peek() is not None and c.get().type == 'KeyPress': sys.exit() **** ****def lose(): ********c.text(1,h+1,"You lose",12) ********c.text(1,h+2,"Press a key",4) ********while 1: ************if c.peek() is not None and c.get().type == 'KeyPress': sys.exit() **** ****def reveal(x,y,m=0): ********if k[x][y] in string.digits or k[x][y] != '.': return ********at = str(r[x][y]) ********if at == '*': ************sett(x,y,'*',124) ************lose() ********if at in string.digits: ************color = col[at] ************if m<1: color = color+112 ************sett(x,y,str(at),color) ********if at == ' ': ************color = 15 ************if (m < 1): color = color+112 ************sett(x,y,' ',color) ************for z in range(-1,2): ****************for a in range(-1,2): ********************if z == 0 and a == 0: continue ********************reveal(x+z,y+a,1) **** ****col = {'1':9,'2':2,'3':14,'4':3,'5':6,'6':10,'7':11,'8':13,'.':15,' ':15,'*':12,'0':11,'F':12}**** ****r = [ [' ' for e in range(h)] for i in range(w)] ****k = [ ['.' for e in range(h)] for i in range(w)] **************** ****for x in range(m): setm() **** ****for y in range(h): ********for x in range(w): ************if x == 0 or y == 0 or x == w - 1 or y == h - 1: ****************c.text(x,y,'#',8) ****************r[x][y] = '#' ****************k[x][y] = '#' **************** ************elif r[x][y] == '*': ****************k[x][y] == '.' ****************c.text(x,y,'.',15) **************** ************elif r[x][y] == ' ': ****************num = 0 ****************for z in range(-1,2): ********************for a in range(-1,2): ************************if z == 0 and a == 0: continue ************************if r[x+z][y+a] == '*': num = num+1 ****************if num > 0: r[x][y] = num; ****************k[x][y] == '.' ****************c.text(x,y,'.',15) ****cur = (1,1) ****c.text(1,1,'.',112) ****f = 0 **** ****while 1: ********if c.peek() is not None: ************e = c.get() ************if e.type == 'KeyPress': ****************if e.char == 'w': cur = mcur(0,-1) ****************elif e.char == 's': cur = mcur(0,1) ****************elif e.char == 'a': cur = mcur(-1,0) ****************elif e.char == 'd': cur = mcur(1,0) ****************elif e.char == 'z': reveal(cur[0],cur[1]) ****************elif e.char == 'x': f = flag(cur[0],cur[1])**************** ****************elif e.char == 'q': break def menu(w=20,h=20,m=20): ****c.page(15,' ') ****c.text(9,6,"Minesweeper",13) ****c.text(1,8,"Q - Quit") ****c.text(1,9,"U/Y - Adjust level width") ****c.text(1,10,"H/J - Adjust level height") ****c.text(1,11,"N/M - Adjust mines") ****c.text(1,13,"Keys in game:") ****c.text(2,14,"WASD: move") ****c.text(2,15,"Z: Reveal tile") ****c.text(2,16,"X: (un)Flag tile") ****c.text(25,14," Width: ",11) ****c.text(25,15,"Height: ",11) ****c.text(25,16," Mines: ",11) ****c.text(33,14,str(w),11) ****c.text(33,15,str(h),11) ****c.text(33,16,str(m),11) ****def sm(): ********c.text(33,16,"**") ********c.text(33,16,str(m),11) ****def sw(): ********c.text(33,14,"**") ********c.text(33,14,str(w),11) ****def sh(): ********c.text(33,15,"**") ********c.text(33,15,str(h),11) ****while 1: ********if c.peek() is not None: ************e = c.get() ************if e.type == 'KeyPress': ****************if e.char == 'q': sys.exit() ****************elif e.char == 'u': ********************if w<60: w=w+1; sw() ****************elif e.char == 'y': ********************if w>3: w=w-1; sw() ********************if m>h*w-2: m=h*w-2; sm() ****************elif e.char == 'j': ********************if h<60: h=h+1; sh() ****************elif e.char == 'h': ********************if h>3: h=h-1; sh() ********************if m>h*w-2: m=h*w-2; sm() ****************elif e.char == 'm': ********************if m<h*w-2: m=m+1; sm() ****************elif e.char == 'n': ********************if m>7: m=m-1; sm() ****************else: ********************c.page(15,' ') ********************game(w+2,h+2,m) ******************** c = Console.getconsole() c.title("Minesweeper") c.cursor(0) menu() |
||
![]() ![]() |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Console Wars! | Iowa | Blah, blah, blah... | 23 | 12-07-2007 07:12 AM |
Console Alliances | TheGiantMidgit | Gaming Zone | 20 | 19-05-2006 09:08 PM |
Which Game Console Is Better | win98 | Gaming Zone | 56 | 27-03-2006 12:31 PM |
Console Games | Rogue | Blah, blah, blah... | 5 | 08-06-2005 04:57 PM |
Console Idea | Sn0w | Blah, blah, blah... | 16 | 23-05-2005 11:21 AM |
|
|
||
  |