2015-09-03 85 views
-1
import pygame 
from random import* 

def minas(n,bombas): 
    tablero=crear_tablero(n) 
    tablero=agregar_bombas(tablero,bombas) 
    tablero=cambiar_tablero(tablero) 
    return tablero 


def crear_tablero(n): 
    a=[] 
    for i in range(n): 
     a.append([0]*n) 

    return a 

def agregar_bombas(tablero,bombas): 
    for i in range(bombas): 
     es_bomba=False 
     while not es_bomba: 
      x=randint(0,len(tablero)-1) 
      y=randint(0,len(tablero[0])-1) 
      if tablero[x][y] !=9: 
       tablero[x][y]=9 
       es_bomba=True 
    return tablero 

def cambiar_tablero(tablero): 
    for x in range(len(tablero)): 
     for y in range(len(tablero[x])): 
      if tablero[x][y]==9: 
       tablero = abajoIzquierda(tablero,x,y) 
       tablero = abajoDerecha(tablero,x,y) 
       tablero = abajo(tablero,x,y) 
       tablero = arribaIzquierda(tablero,x,y) 
       tablero = arribaDerecha(tablero,x,y) 
       tablero = arriba(tablero,x,y) 
       tablero = Izquierda(tablero,x,y) 
       tablero = Derecha(tablero,x,y) 
    return tablero 

def abajoIzquierda(tablero,x,y): 
    if (x+1)<len(tablero[x]) and (y-1)>=0: #verifica la esquina derecha 
     if tablero[x+1][y-1] !=9: #no hace cambio si hay una bomba 
      tablero[x+1][y-1]+=1 
    return tablero 

def abajoDerecha(tablero,x,y): 
    if (x+1)<len(tablero[0]) and (y+1)<len(tablero): 
     if tablero[x+1][y+1] !=9: 
      tablero[x+1][y+1]+=1 
    return tablero 

def abajo(tablero,x,y): 
    if (x+1)<len(tablero[0]): 
     if tablero[x+1][y] !=9: 
      tablero[x+1][y]+=1 
    return tablero 

def arribaIzquierda(tablero,x,y): 
    if (x-1)>=0 and (y-1)>=0: 
     if tablero[x-1][y-1] !=9: 
      tablero[x-1][y-1]+=1 
    return tablero 

def arribaDerecha(tablero,x,y): 
    if (x-1)>=0 and (y+1)<len(tablero): 
     if tablero[x-1][y+1] !=9: 
      tablero[x-1][y+1]+=1 
    return tablero 

def arriba(tablero,x,y): 
    if (x-1)>=0: 
     if tablero[x-1][y] !=9: 
      tablero[x-1][y]+=1 
    return tablero 

def Derecha(tablero,x,y): 
    if (y+1)<len(tablero): 
     if tablero[x][y+1] !=9: 
      tablero[x][y+1]+=1 
    return tablero 

def Izquierda(tablero,x,y): 
    if (x+1)<len(tablero[x]) and (y-1)>=0: 
     if tablero[x][y-1] !=9: 
      tablero[x][y-1]+=1 
    return tablero 

def pr(tablero): 
    for i in tablero: 
     print(i) 


class Board: 
    def _init_(self,board): 
     self.board=board 
    def _repr_(self): 
     pr(self.board) 
     return "IS YOUR TABLE" 

class Square: 
    def _init_(self,x,y,w,h,board,ij): 
     self.rect=pygame.rect.Rect(x,y,w,h) 
     i,j=ij 
     self.val=board[i][j] 
     self.x=x 
     self.y=y 
     self.visible=False 
     self.flag=False 

def restart(size,bombas): 
    game=(size,bombas) 



def open_game(size,bombas): 
    square.visible=True 
    i,j=square.x//40,square.y//40 
    if i+1< len(lista): 
     #chequeamos la casilla alrededor de la que estamos parados si esta visible o no 
     #y si tiene bandera o no 
     if lista[i+1][j].visible== False and lista[i+1][j].flag== False: 
      lista[i+1][j].visible=True 
      if lista[i+1][j].val==0: 
       open_game(lista,lista[i+1][j]) 

     if j+1 < len(lista): 
      if lista[i+1][j+1].visible==False and lista[i+1][j+1].flag==False: 
       lista[i+1][j+1].visible=True 
       if lista[i+1][j+1].val==0: 
        open_game(lista,lista[i+1][j+1]) 

     if j-1>=0: 
      if lista[i+1][j-1].visible==False and lista[i+1][j-1].flag==False: 
       lista[i+1][j-1].visible=True 
       if lista[i+1][j-1].val==0: 
        open_game(lista,lista[i+1][j-1]) 


    if i-1>=0: 
     if lista[i-1][j].visible==False and lista[i-1][j].flag==False: 
      lista[i-1][j].visible=True 
      if lista[i-1][j].val==0: 
       open_game(lista,lista[i-1][j]) 

     if j+1 < len(lista): 
      if lista[i-1][j+1].visible==False and lista[i+1][j+1].flag==False: 
       lista[i+1][j+1]=True 
       if lista[i+1][j+1].val==0: 
        open_game(lista,lista[i+1][j+1]) 

     if j-1 >=0: 
      if lista[i-1][j-1].visible==False and lista[i-1][j-1].flag==False: 
       lista[i-1][j-1]=True 
       if lista[i-1][j-1].val==0: 
        open_game(lista,lista[i-1][j-1]) 


    if j-1 >=0: 
     if lista[i1][j-1].visible==False and lista[i][j-1].flag==False: 
      lista[i][j-1]=True 
      if lista[i1][j-1].val==0: 
       open_game(lista,lista[i][j-1]) 

    if j+1 < len(lista): 
     if lista[i][j+1].visible==False and lista[i][j+1].flag==False: 
      lista[i][j+1]=True 
      if lista[i][j+1].val==0: 
       open_game(lista,lista[i][j+1]) 


def game(size,bombas): 

    #40x40 pixeles 
    grey=pygame.image.load("grey.jpg") 
    white=pygame.image.load("blanco.jpg") 

    #20x20 pixeles 
    cero=pygame.image.load("cero.png") 
    uno=pygame.image.load("uno.jpg") 
    dos=pygame.image.load("dos.jpg") 
    tres=pygame.image.load("tres.jpg") 
    cuatro=pygame.image.load("cuatro.png") 
    cinco=pygame.image.load("cinco.jpg") 
    seis=pygame.image.load("seis.png") 
    siete=pygame.image.load("siete.png") 
    ocho=pygame.image.load("ocho.png") 
    nueve=pygame.image.load("nueve.png") 
    bandera=pygame.image.load("bandera.jpg") 

    numero=[cero,uno,dos,tres,cuatro,cinco,seis,siete,ocho,nueve] 

    c=Board(minas(size,bombas)) 
    w=h=len(c.board)*40 
    pantalla=pygame.display.set_mode(w,h) 

    #crea una lista de los cuadros organizados 

    lista=[[] for i in range(size)] 
    for i in range(0,size*40,40): 
     for j in range(0,size*40,40): 
      lista[i//40]+=[Square(i,j,40,40,c.board,(i//40),(j//40))] 
      screen.blit(grey,(i,j)) 


    run=True 
    while run: 
     for event in pygame.event.get(): 
      if event.type==pygame.QUIT: 
       run=False 
       pygame.quit() 

      elif event.type==pygame.KEYDOWN: 
       if evet.key==pygamee.K_r: 
        run=False 
        restart(size,bombas) 

      elif event.type==pygmae.MOUSEBUTTONDOWN and event.button==1: 
       #verificar con clic izquierdo 
       for i in lista: 
        for j in i: 
         r=pygame.rect.Rect(pygame.mouse.get_pos(),(1,1)) 
         if j.rect.colliderect(r): 
          if j.flag==False: #si esta con bandera 
           if j.val==9: #si es una bomba 
            print("GAME OVER") 
            run=False 
           j.visible=True 
           if j.val==0: 
            j.visible=open_game(lista,j) 
            j.visible=True 

      elif event.type==pygame.MOUSEBUTTONDOWN and even.button==3: 
       #si el jugdor presiono el clic derecho, se pondra o se quitara la bander 
       for i in lista: 
        for j in i: 
         r=pygame.rect.Rect(pygame.mouse.get_pos(),(1,1)) 
         if j.rect.colliderect(r): 
          if j.visible==False: 
           if j.flag==False: 
            j.flag==True 
           elif j.visible==True: 
            j.flag=True 


     #ahora abriremos cada cuadro 

     for i in lista: 
      for j in i: 
       if j.visible==True: 
        screen.blit(white,(j.x,j.y)) 
        screen.blit(numero[j.val],(j.x+10,j.y+10)) 
       if j.flag==True: 
        screen.blit(flag,(j.x+10,j.y+10)) 
       if j.flag==False and j.visible==False: 
        screen.blit(grey,(j.x,j.y)) 

     cont=0 
     for i in lista: 
      for j in i: 
       if j.visible==True and j.val !=9: 
        cont+=1 
        #cuenta los cuadro visibles 
      if cont== size*size - bombas: 
       run=False 
       print("YOU WIN") 
     pygame.display.update() 

    #si ganamos o perdimos muestra los lugares de las bombas 
    for i in lista: 
     for j in i: 
      if j.val==9: 
       screen.blit(nine,(j.x+10,j.y+10)) 
    pygame.display.update() 

    #espera por salida o reincio 
    run=True 

    while run: 
     for event in pygame.event.get(): 
      if event.type==pygame.QUIT: 
       run=False 
       pygame.quit() 
      elif event.type==pygame.KEYDOWN: 
       if event.type==pygame.K_r: 
        run=False 
        restart(size,bombas) 




print("presione 'r' para reinicio") 
size=int(input("Ingrese tamaño de la matriz")) 
bombas=int(input("Ingrese el numero de bombas")) 
game(size,bombas) 

presione 'R' 對reinicio
Ingrese玉野德拉matriz10
Ingrese EL NUMERO德bombas5我的代碼生成以下錯誤:類型錯誤:對象()沒有參數

Traceback (most recent call last): 
    File "C:\Documents and Settings\Administrador\Escritorio\buscaminas.py", line 297, in <module> 
    game(size,bombas) 
    File "C:\Documents and Settings\Administrador\Escritorio\buscaminas.py", line 195, in game 
    c=Board(minas(size,bombas)) 
TypeError: object() takes no parameters 
pygame.error: Couldn't open grey.jpg 

回答

1
class Board: 
    def _init_(self,board): 
     self.board=board 

__init__每邊需要兩個下劃線,總共四個。 __repr__也是如此。

class Board: 
    def __init__(self,board): 
     self.board=board 
相關問題