Skip to content

Instantly share code, notes, and snippets.

@studentbrad
Created April 27, 2020 18:46
Show Gist options
  • Save studentbrad/66c693b050029fdb16c850cd44c5344f to your computer and use it in GitHub Desktop.
Save studentbrad/66c693b050029fdb16c850cd44c5344f to your computer and use it in GitHub Desktop.
the game inkspill implemented in pygame
# imports
import pygame
import sys
import random
from pygame.locals import *
# setting display variables
WINDOWWIDTH = 600 # the width of the game window
WINDOWHEIGHT = WINDOWWIDTH # the height of the game window
GAPSIZE = 0 # gap size between boxes
MARGIN = 0 # game margin
# setting game variables
GAMECAPTION = 'Ink Spill' # the caption of the game
NUMBEROFROWS = 30 # the number of rows in game
NUMBEROFCOLUMNS = NUMBEROFROWS # the number of columns in game
NUMBEROFSQUARES = NUMBEROFROWS*NUMBEROFCOLUMNS # the number of squares in game
BOXWIDTH = float(WINDOWWIDTH)/(NUMBEROFCOLUMNS) # individual box width
BOXHEIGHT = float(WINDOWHEIGHT)/(NUMBEROFROWS) # individual box height
# setting colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
VIOLET = (255, 0, 255)
AQUA = (0, 255, 255)
BROWN = (255, 255, 0)
LISTOFGAMECOLORS = [RED, GREEN, BLUE, VIOLET, AQUA, BROWN]
# box class
class box(object):
def __init__(self, boxWidth, boxHeight, row, column):
self.color = LISTOFGAMECOLORS[random.randint(
0, len(LISTOFGAMECOLORS)-1)] # initial color chosen randomly
self.ownership = False # initial ownership by default
self.boxWidth = BOXWIDTH # the object box width
self.boxHeight = BOXHEIGHT # the object box height
self.xCoord = (column+1)*BOXWIDTH-BOXWIDTH/2 # the object x coordinate
self.yCoord = (row+1)*BOXHEIGHT-BOXHEIGHT/2 # the object y coordinate
self.topLeftXCoord = column*BOXWIDTH
self.topLeftYCoord = row*BOXHEIGHT
self.boxLeft = None # the box to the left by default
self.boxRight = None # the box to the right by default
self.boxUp = None # the box above by default
self.boxDown = None # the box below by default
self.row = row # the box row number
self.column = column # the box column number
def getColor(self): # return the color oof the block
return self.color
def getOwnership(self): # return the ownership of the block (True/False)
return self.ownership
def getBoxWidth(self): # return the width of the box
return self.boxWidth
def getBoxHeight(self): # return the height of the box
return self.boxHeight
def getXCoord(self): # return the x cooordinate that represents the centre of the box x postion
return self.xCoord
def getYCoord(self): # return the y coordinate that represents the centre of the box y postion
return self.yCoord
def getTopLeftXCoord(self):
return self.topLeftXCoord
def getTopLeftYCoord(self):
return self.topLeftYCoord
def getLeft(self): # return the box object to the left
return self.boxLeft
def getRight(self): # return the box object to the right
return self.boxRight
def getUp(self): # return the box object above
return self.boxUp
def getDown(self): # return the box object below
return self.boxDown
def setColor(self, newColor): # set the color to a new color
self.color = newColor
return
def changeOwnership(self): # change the ownership boolean
self.ownership = True
def setBoxRight(self, boxRight): # place in the object that is located right
self.boxRight = boxRight
def setBoxLeft(self, boxLeft): # place in the object that is located left
self.boxLeft = boxLeft
def setBoxUp(self, boxUp): # place in the object that is located up
self.boxUp = boxUp
def setBoxDown(self, boxDown): # place in the object that is located down
self.boxDown = boxDown
# creating function to generate a matrix filled with objects that are of type 'box'
def createMatrix(rows, columns, objectType):
# making sure the objects passed are boxes
assert objectType == 'box', "This is not a box object"
matrix = [] # creating a blank matrix
for i in range(0, rows): # creating rows
matrix.append([])
for j in range(0, columns): # creating columns
# putting a new box object in every row i column j
matrix[i].append(box(BOXWIDTH, BOXHEIGHT, i, j))
for i in range(0, rows):
for j in range(0, columns):
try:
matrix[i][j].setBoxRight(matrix[i][j+1])
except(IndexError):
print "IGNORE"
try:
if j > 0:
matrix[i][j].setBoxLeft(matrix[i][j-1])
except(IndexError):
print "IGNORE"
try:
matrix[i][j].setBoxDown(matrix[i+1][j])
except(IndexError):
print "IGNORE"
try:
if i > 0:
matrix[i][j].setBoxUp(matrix[i-1][j])
except(IndexError):
print "IGNORE"
return matrix
def findMouseObject(matrix, mouseX, mouseY):
for i in range(0, len(matrix)):
for j in range(0, len(matrix[i])):
if mouseX > matrix[i][j].getTopLeftXCoord() and mouseY > matrix[i][j].getTopLeftYCoord() and mouseX < matrix[i][j].getTopLeftXCoord()+matrix[i][j].getBoxWidth() and mouseY < matrix[i][j].getTopLeftYCoord()+matrix[i][j].getBoxHeight():
return matrix[i][j]
def drawToBoard(gameMatrix, DISPLAYSURF):
for i in range(0, len(gameMatrix)):
for j in range(0, len(gameMatrix[i])):
pygame.draw.rect(DISPLAYSURF, gameMatrix[i][j].getColor(), (gameMatrix[i][j].getTopLeftXCoord(
), gameMatrix[i][j].getTopLeftYCoord(), gameMatrix[i][j].getBoxWidth(), gameMatrix[i][j].getBoxHeight()))
return
def boxesToChange(gameMatrix, color):
for i in gameMatrix:
for j in i:
if j.getOwnership() == True:
if not j.getRight() == None:
if j.getRight().getOwnership() == False and j.getRight().getColor() == color:
j.getRight().changeOwnership()
if not j.getLeft() == None:
if j.getLeft().getOwnership() == False and j.getLeft().getColor() == color:
j.getLeft().changeOwnership()
if not j.getUp() == None:
if j.getUp().getOwnership() == False and j.getUp().getColor() == color:
j.getUp().changeOwnership()
if not j.getDown() == None:
if j.getDown().getOwnership() == False and j.getDown().getColor() == color:
j.getDown().changeOwnership()
return gameMatrix
# main() function to be called when running the program
def main():
pygame.init()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
DISPLAYSURF.fill(WHITE)
MOUSEX = 0
MOUSEY = 0
pygame.display.set_caption(GAMECAPTION)
gameMatrix = createMatrix(NUMBEROFROWS, NUMBEROFCOLUMNS, 'box')
gameMatrix[0][0].changeOwnership()
drawToBoard(gameMatrix, DISPLAYSURF)
# main game loop
while True:
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
print "EVENT!"
MOUSEX, MOUSEY = event.pos
print "x:%s" % str(MOUSEX)
print "y:%s" % str(MOUSEY)
tempColor = findMouseObject(
gameMatrix, MOUSEX, MOUSEY).getColor()
print tempColor
for i in gameMatrix:
for j in i:
if j.getOwnership() == True:
j.setColor(tempColor)
gameMatrix = boxesToChange(gameMatrix, tempColor)
drawToBoard(gameMatrix, DISPLAYSURF)
elif event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment