I am kind of new to python, and am not the best at it, but I am kind of tearing apart a mindsweeper project to make it using pygame, and right now, it is just a square moving around every frame. The pygame window starts unfocused, and when I focus it, it crashes. Here is my code
import random
import pygame
class boardSpot(object):
value = 0
selected = False
mine = False
def __init__(self):
self.selected = False
def __str__(self):
return str(boardSpot.value)
def isMine(self):
if boardSpot.value == -1:
return True
return False
class boardClass(object):
def __init__(self, m_boardSize, m_numMines):
self.board = [[boardSpot() for i in range(m_boardSize)] for j in range(m_boardSize)]
self.boardSize = m_boardSize
self.numMines = m_numMines
self.selectableSpots = m_boardSize * m_boardSize - m_numMines
i = 0
while i < m_numMines:
x = random.randint(0, self.boardSize-1)
y = random.randint(0, self.boardSize-1)
if not self.board[x][y].mine:
self.addMine(x, y)
i += 1
else:
i -= 1
def __str__(self):
returnString = " "
divider = "n---"
for i in range(0, self.boardSize):
returnString += " | " + str(i)
divider += "----"
divider += "n"
returnString += divider
for y in range(0, self.boardSize):
returnString += str(y)
for x in range(0, self.boardSize):
if self.board[x][y].mine and self.board[x][y].selected:
returnString += " |" + str(self.board[x][y].value)
elif self.board[x][y].selected:
returnString += " | " + str(self.board[x][y].value)
else:
returnString += " | "
returnString += " |"
returnString += divider
return returnString
def addMine(self, x, y):
self.board[x][y].value = -1
self.board[x][y].mine = True
for i in range(x-1, x+2):
if i >= 0 and i < self.boardSize:
if y-1 >= 0 and not self.board[i][y-1].mine:
self.board[i][y-1].value += 1
if y+1 < self.boardSize and not self.board[i][y+1].mine:
self.board[i][y+1].value += 1
if x-1 >= 0 and not self.board[x-1][y].mine:
self.board[x-1][y].value += 1
if x+1 < self.boardSize and not self.board[x+1][y].mine:
self.board[x+1][y].value += 1
def makeMove(self, x, y):
self.board[x][y].selected = True
self.selectableSpots -= 1
if self.board[x][y].value == -1:
return False
if self.board[x][y].value == 0:
for i in range(x-1, x+2):
if i >= 0 and i < self.boardSize:
if y-1 >= 0 and not self.board[i][y-1].selected:
self.makeMove(i, y-1)
if y+1 < self.boardSize and not self.board[i][y+1].selected:
self.makeMove(i, y+1)
if x-1 >= 0 and not self.board[x-1][y].selected:
self.makeMove(x-1, y)
if x+1 < self.boardSize and not self.board[x+1][y].selected:
self.makeMove(x+1, y)
return True
else:
return True
def hitMine(self, x, y):
return self.board[x][y].value == -1
def isWinner(self):
return self.selectableSpots == 0
def PygamePrintBoard():
screen.fill((255, 255, 255))
pygame.draw.rect(screen,[255,0,255], pygame.Rect(random.randint(0,(boardSize*50)), (random.randint(0,boardSize*50)), 60, 60))
pygame.display.flip()
clock.tick(30)
boardSize = int(input("Choose the Width of the board: "))
numMines = int(input("Choose the number of mines: "))
screen = pygame.display.set_mode([boardSize * 100, boardSize * 100])
clock = pygame.time.Clock()
gameOver = False
winner = False
Board = boardClass(boardSize, numMines)
gameOver = False
while not gameOver:
PygamePrintBoard()
I tried making it 30 fps, from no fps cap, and that didn’t work and now I am really confused.
New contributor
RictalDred17 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.