Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Y1 Final Project Skeleton
- import pygame
- from pygame import *
- import random
- from random import *
- #####################################################################################
- ##### Variables that will be used throughout the program
- #####################################################################################
- gridHeight = 10 # how many balls down
- gridWidth = 6 # how many balls accross
- cellSize = 70 # both x- and y-dimensions, in pixels
- windowHeight = cellSize*gridHeight # y
- windowWidth = cellSize*gridWidth # x
- ballRadius = 30 # each ball will therefore fill all but 10 pixels on either side of each cell
- grid = [] # this will get filled up with more lists that will be filled up with ball tuples
- colors = {"blue":[0,0,255], "red":[220,20,60], "yellow":[255,255,0],
- "green":[0,205,0], "orange":[255,165,0], "white":[255,255,255]}
- # animate is a boolean that keeps track of the 'state' of the game
- # it is True when the balls are sliding down and False when they're not moving
- animate = False
- screen = display.set_mode((windowWidth, windowHeight))
- #####################################################################################
- ##### Functions for you to fill in
- #####################################################################################
- def pixelsToGrid((x,y)):
- # takes tuple of pixel coordinates, returns a tuple with grid coordinates
- # uses python's int rounding to our advantage
- a,b = (x,y)
- a=int (a)
- b=int(b)
- answer = (a/70 , b/70)
- return answer
- ##print "**pixelsToGrid Tests**"
- print pixelsToGrid((0,0)) == (0,0)
- print pixelsToGrid((0,80)) == (0,1)
- print pixelsToGrid((90,90)) == (1,1)
- print ''
- def initialFillGrid():
- # initially fills the grid with randomly colored balls in each cell
- for i in range (6):
- grid.append ([])
- for j in range (10):
- keys = colors.keys()
- keys.remove("white")
- color = choice (keys)
- grid[i].append((35+cellSize*i,35+j*cellSize,color,False))
- def nextTo((a,b),(c,d)):
- # takes two tuples of grid points
- # returns True if both points are next to each other
- # neighbors are only up/down/side, no diagonals
- if a-c==1 or a-c==-1 or b-d==1 or b-d==-1:
- if a-c==b-d or a-c==-(b-d):
- return False
- else:
- return True
- else:
- return False
- ##print "**nextTo Tests**"
- print nextTo((0,0),(0,1)) == True
- print nextTo((0,0),(1,0)) == True
- print nextTo((0,0),(1,1)) == False
- print ''
- def generateNeighbors((i,j)):
- # returns a list of all neighbors of (i,j)
- if i!=0 and j!=0 and j!=9 and i!=5:
- answer1=(i-1,j)
- answer2=(i+1,j)
- answer3=(i,j-1)
- answer4=(i,j+1)
- return [answer1,answer2,answer3,answer4]
- else:
- if i==0 and j!=0 and j!=9 and i!=5:
- answer2=(i+1,j)
- answer3=(i,j-1)
- answer4=(i,j+1)
- return [answer2,answer3,answer4]
- if j==0 and i!=0 and j!=9 and i!=5:
- answer1=(i-1,j)
- answer2=(i+1,j)
- answer4=(i,j+1)
- return [answer1,answer2,answer4]
- if j==9 and i!=0 and j!=0 and i!=5:
- answer1=(i-1,j)
- answer2=(i+1,j)
- answer3=(i,j-1)
- return [answer1,answer2,answer3]
- if i==5 and i!=0 and j!=0 and j!=9:
- answer1=(i-1,j)
- answer3=(i,j-1)
- answer4=(i,j+1)
- return [answer1,answer3,answer4]
- if i==5 and j==0:
- answer1=(i-1,j)
- answer3=(i,j+1)
- return [answer1,answer3]
- if i==5 and j==9:
- answer2=(i-1,j)
- answer4=(i,j-1)
- return [answer2,answer4]
- if i==0 and j==0:
- answer2=(i+1,j)
- answer4=(i,j+1)
- return [answer2,answer4]
- if i==0 and j==9:
- answer2=(i+1,j)
- answer4=(i,j-1)
- return [answer2,answer4]
- ##print "**generateNeighbors Tests**"
- print set(generateNeighbors((0,0))) == set([(1,0),(0,1)])
- print set(generateNeighbors((1,1))) == set([(1,0),(0,1),(1,2),(2,1)])
- print set(generateNeighbors((0,1))) == set([(0,0),(1,1),(0,2)])
- print ''
- def findNeighbors((i,j),color):
- # finds all of the balls connected to (i,j) that are color, and marks them True
- if grid[i][j][3]==True:
- return False
- else:
- color2=grid[i][j][2]
- if color!=color2:
- return False
- else:
- checkList=generateNeighbors((i,j))
- for point in checkList:
- a,b=point
- color3=grid[a][b][2]
- if color2==color3:
- d,e,f,g=(i,j)
- g=True
- (i,j)=d,e,f,g
- findNeighbors((a,b))
- else:
- return False
- def replace():
- # goes through every ball, checks to see if it is True (ready to be removed)
- # and then removes it and puts an extra ball in that list
- newCol=[]
- howMany=0
- for column in range(6):
- for ball in grid[column]:
- if ball[3]==False:
- newCol.append(ball)
- else:
- howMany+=1
- for num in range(howMany):
- color=choice(colors.keys())
- newCol.append((35+cellSize*column,newCol[0][1]+70,color,False))
- grid[column]=newCol
- def anyToReplace():
- for i in range(gridWidth):
- for j in range(gridHeight):
- if grid[i][j][3]:
- return True
- return False
- ##print "**anyToReplace Tests**"
- grid1 = [[(35, 35, 'red', False), (35, 105, 'blue', False), (35, 175, 'blue', False),
- (35, 245, 'yellow', False), (35, 315, 'green', False), (35, 385, 'red', False),
- (35, 455, 'yellow', False), (35, 525, 'blue', False), (35, 595, 'green', False),
- (35, 665, 'blue', False)], [(105, 35, 'red', False), (105, 105, 'red', False),
- (105, 175, 'yellow', False), (105, 245, 'red', False), (105, 315, 'green', False),
- (105, 385, 'orange', False), (105, 455, 'blue', False), (105, 525, 'green', False),
- (105, 595, 'red', False), (105, 665, 'blue', False)], [(175, 35, 'orange', False),
- (175, 105, 'yellow', False), (175, 175, 'yellow', False), (175, 245, 'green', False),
- (175, 315, 'blue', False), (175, 385, 'orange', False), (175, 455, 'red', False),
- (175, 525, 'orange', False), (175, 595, 'red', False), (175, 665, 'orange', False)],
- [(245, 35, 'green', False), (245, 105, 'blue', False), (245, 175, 'orange', False),
- (245, 245, 'green', False), (245, 315, 'blue', False), (245, 385, 'yellow', False),
- (245, 455, 'yellow', False), (245, 525, 'green', False), (245, 595, 'yellow', False),
- (245, 665, 'red', False)], [(315, 35, 'red', False), (315, 105, 'red', False),
- (315, 175, 'orange', False), (315, 245, 'blue', False), (315, 315, 'blue', False),
- (315, 385, 'red', False), (315, 455, 'orange', False), (315, 525, 'red', False),
- (315, 595, 'orange', False), (315, 665, 'red', False)], [(385, 35, 'red', False),
- (385, 105, 'blue', False), (385, 175, 'orange', False), (385, 245, 'blue', False),
- (385, 315, 'yellow', False), (385, 385, 'blue', False), (385, 455, 'blue', False),
- (385, 525, 'blue', False), (385, 595, 'green', False), (385, 665, 'orange', False)]]
- grid2 = [[(35, 35, 'red', False), (35, 105, 'blue', False), (35, 175, 'blue', False),
- (35, 245, 'yellow', False), (35, 315, 'green', False), (35, 385, 'red', False),
- (35, 455, 'yellow', False), (35, 525, 'blue', False), (35, 595, 'green', False),
- (35, 665, 'blue', False)], [(105, 35, 'red', False), (105, 105, 'red', False),
- (105, 175, 'yellow', False), (105, 245, 'red', False), (105, 315, 'green', False),
- (105, 385, 'orange', False), (105, 455, 'blue', False), (105, 525, 'green', False),
- (105, 595, 'red', False), (105, 665, 'blue', False)], [(175, 35, 'orange', False),
- (175, 105, 'yellow', True), (175, 175, 'yellow', False), (175, 245, 'green', False),
- (175, 315, 'blue', False), (175, 385, 'orange', False), (175, 455, 'red', False),
- (175, 525, 'orange', False), (175, 595, 'red', False), (175, 665, 'orange', False)],
- [(245, 35, 'green', False), (245, 105, 'blue', False), (245, 175, 'orange', False),
- (245, 245, 'green', False), (245, 315, 'blue', False), (245, 385, 'yellow', False),
- (245, 455, 'yellow', False), (245, 525, 'green', False), (245, 595, 'yellow', False),
- (245, 665, 'red', False)], [(315, 35, 'red', False), (315, 105, 'red', False),
- (315, 175, 'orange', False), (315, 245, 'blue', False), (315, 315, 'blue', False),
- (315, 385, 'red', False), (315, 455, 'orange', False), (315, 525, 'red', False),
- (315, 595, 'orange', False), (315, 665, 'red', False)], [(385, 35, 'red', False),
- (385, 105, 'blue', False), (385, 175, 'orange', False), (385, 245, 'blue', False),
- (385, 315, 'yellow', False), (385, 385, 'blue', False), (385, 455, 'blue', False),
- (385, 525, 'blue', False), (385, 595, 'green', False), (385, 665, 'orange', False)]]
- grid3 = [[(35, 35, 'red', False), (35, 105, 'blue', False), (35, 175, 'blue', False),
- (35, 245, 'yellow', False), (35, 315, 'green', False), (35, 385, 'red', False),
- (35, 455, 'yellow', False), (35, 525, 'blue', True), (35, 595, 'green', False),
- (35, 665, 'blue', False)], [(105, 35, 'red', False), (105, 105, 'red', False),
- (105, 175, 'yellow', False), (105, 245, 'red', False), (105, 315, 'green', False),
- (105, 385, 'orange', False), (105, 455, 'blue', False), (105, 525, 'green', False),
- (105, 595, 'red', False), (105, 665, 'blue', False)], [(175, 35, 'orange', False),
- (175, 105, 'yellow', False), (175, 175, 'yellow', False), (175, 245, 'green', False),
- (175, 315, 'blue', False), (175, 385, 'orange', False), (175, 455, 'red', True),
- (175, 525, 'orange', False), (175, 595, 'red', False), (175, 665, 'orange', False)],
- [(245, 35, 'green', True), (245, 105, 'blue', False), (245, 175, 'orange', False),
- (245, 245, 'green', False), (245, 315, 'blue', True), (245, 385, 'yellow', False),
- (245, 455, 'yellow', False), (245, 525, 'green', False), (245, 595, 'yellow', False),
- (245, 665, 'red', False)], [(315, 35, 'red', False), (315, 105, 'red', False),
- (315, 175, 'orange', False), (315, 245, 'blue', False), (315, 315, 'blue', False),
- (315, 385, 'red', False), (315, 455, 'orange', False), (315, 525, 'red', False),
- (315, 595, 'orange', False), (315, 665, 'red', False)], [(385, 35, 'red', False),
- (385, 105, 'blue', False), (385, 175, 'orange', False), (385, 245, 'blue', False),
- (385, 315, 'yellow', False), (385, 385, 'blue', False), (385, 455, 'blue', False),
- (385, 525, 'blue', False), (385, 595, 'green', False), (385, 665, 'orange', True)]]
- grid = grid1[:]
- print anyToReplace() == False
- grid = grid2[:]
- print anyToReplace() == True
- grid = grid3[:]
- print anyToReplace() == True
- print ''
- def slideDownNicely():
- # the animation!!
- notDone = False
- reference = [35,105,175, 245, 315, 385, 455, 525, 595, 665]
- y+=5
- if y in reference:
- return notDone
- def anyLeft():
- # retuns False if there are no adjacent balls of the same color left (game over)
- for i in range (6):
- for j in range (10):
- checkList=generateNeighbors((i,j))
- color=grid[i][j][2]
- for point in checkList:
- a,b=point
- color2=grid[a][b][2]
- if color==color2:
- return True
- return False
- ##print "**anyLeft Tests**"
- grid4 = [[(35, 35, 0, False), (35, 105, 1, False), (35, 175, 2, False), (35, 245, 3, False),
- (35, 315, 4, False), (35, 385, 5, False), (35, 455, 6, False), (35, 525, 7, False),
- (35, 595, 8, False), (35, 665, 9, False)], [(105, 35, 10, False), (105, 105, 11, False),
- (105, 175, 12, False), (105, 245, 13, False), (105, 315, 14, False), (105, 385, 15, False),
- (105, 455, 16, False), (105, 525, 17, False), (105, 595, 18, False), (105, 665, 19, False)],
- [(175, 35, 20, False), (175, 105, 21, False), (175, 175, 22, False), (175, 245, 23, False),
- (175, 315, 24, False), (175, 385, 25, False), (175, 455, 26, False), (175, 525, 27, False),
- (175, 595, 28, False), (175, 665, 29, False)], [(245, 35, 30, False), (245, 105, 31, False),
- (245, 175, 32, False), (245, 245, 33, False), (245, 315, 34, False), (245, 385, 35, False),
- (245, 455, 36, False), (245, 525, 37, False), (245, 595, 38, False), (245, 665, 39, False)],
- [(315, 35, 40, False), (315, 105, 41, False), (315, 175, 42, False), (315, 245, 43, False),
- (315, 315, 44, False), (315, 385, 45, False), (315, 455, 46, False), (315, 525, 47, False),
- (315, 595, 48, False), (315, 665, 49, False)], [(385, 35, 50, False), (385, 105, 51, False),
- (385, 175, 52, False), (385, 245, 53, False), (385, 315, 54, False), (385, 385, 55, False),
- (385, 455, 56, False), (385, 525, 57, False), (385, 595, 58, False), (385, 665, 59, False)]]
- grid5 = [[(35, 35, 0, False), (35, 105, 0, False), (35, 175, 2, False), (35, 245, 3, False),
- (35, 315, 4, False), (35, 385, 5, False), (35, 455, 6, False), (35, 525, 7, False),
- (35, 595, 8, False), (35, 665, 9, False)], [(105, 35, 10, False), (105, 105, 11, False),
- (105, 175, 12, False), (105, 245, 13, False), (105, 315, 14, False), (105, 385, 15, False),
- (105, 455, 16, False), (105, 525, 17, False), (105, 595, 18, False), (105, 665, 19, False)],
- [(175, 35, 20, False), (175, 105, 21, False), (175, 175, 22, False), (175, 245, 23, False),
- (175, 315, 24, False), (175, 385, 25, False), (175, 455, 26, False), (175, 525, 27, False),
- (175, 595, 28, False), (175, 665, 29, False)], [(245, 35, 30, False), (245, 105, 31, False),
- (245, 175, 32, False), (245, 245, 33, False), (245, 315, 34, False), (245, 385, 35, False),
- (245, 455, 36, False), (245, 525, 37, False), (245, 595, 38, False), (245, 665, 39, False)],
- [(315, 35, 40, False), (315, 105, 41, False), (315, 175, 42, False), (315, 245, 43, False),
- (315, 315, 44, False), (315, 385, 45, False), (315, 455, 46, False), (315, 525, 47, False),
- (315, 595, 48, False), (315, 665, 49, False)], [(385, 35, 50, False), (385, 105, 51, False),
- (385, 175, 52, False), (385, 245, 53, False), (385, 315, 54, False), (385, 385, 55, False),
- (385, 455, 56, False), (385, 525, 57, False), (385, 595, 58, False), (385, 665, 59, False)]]
- grid6 = [[(35, 35, 0, False), (35, 105, 1, False), (35, 175, 2, False), (35, 245, 3, False),
- (35, 315, 4, False), (35, 385, 5, False), (35, 455, 6, False), (35, 525, 7, False),
- (35, 595, 8, False), (35, 665, 9, False)], [(105, 35, 0, False), (105, 105, 11, False),
- (105, 175, 12, False), (105, 245, 13, False), (105, 315, 14, False), (105, 385, 15, False),
- (105, 455, 16, False), (105, 525, 17, False), (105, 595, 18, False), (105, 665, 19, False)],
- [(175, 35, 0, False), (175, 105, 21, False), (175, 175, 22, False), (175, 245, 23, False),
- (175, 315, 24, False), (175, 385, 25, False), (175, 455, 26, False), (175, 525, 27, False),
- (175, 595, 28, False), (175, 665, 29, False)], [(245, 35, 30, False), (245, 105, 31, False),
- (245, 175, 32, False), (245, 245, 33, False), (245, 315, 34, False), (245, 385, 35, False),
- (245, 455, 36, False), (245, 525, 37, False), (245, 595, 38, False), (245, 665, 39, False)],
- [(315, 35, 40, False), (315, 105, 41, False), (315, 175, 42, False), (315, 245, 43, False),
- (315, 315, 44, False), (315, 385, 45, False), (315, 455, 46, False), (315, 525, 47, False),
- (315, 595, 48, False), (315, 665, 49, False)], [(385, 35, 50, False), (385, 105, 51, False),
- (385, 175, 52, False), (385, 245, 53, False), (385, 315, 54, False), (385, 385, 55, False),
- (385, 455, 56, False), (385, 525, 57, False), (385, 595, 58, False), (385, 665, 59, False)]]
- grid = grid4[:]
- print anyLeft() == False
- grid = grid5[:]
- print anyLeft() == True
- grid = grid6[:]
- print anyLeft() == True
- print ''
- #####################################################################################
- ##### Drawing things onto the screen
- #####################################################################################
- #####
- ##### Comment out everything below this when checking:
- ##### pixelsToGrid, nextTo, generateNeighbors, anyLeft, and anyToReplace
- #####
- ##### Make sure it is UNcommented when checking initialFillGrid
- #####
- #####################################################################################
- init() # do all of this only once
- screen = display.set_mode((windowWidth, windowHeight))
- screen.fill(colors['white'])
- initialFillGrid()
- while True: #keep doing this forever
- screen.fill(colors['white']) # wipe clean from last time
- if animate == True: #if the balls are sliding don't let the user click
- animate = slideDownNicely()
- else: #the user is looking for balls to click
- if anyLeft(): # before the game is over.....
- pass ##### YOU WILL REPLACE THIS WITH CODE LATER
- else: # if the game is over
- pass ##### YOU WILL REPLACE THIS WITH CODE LATER
- # now draw all of the balls on the screen!!
- for row in grid:
- for ball in row:
- x,y,color,b = ball
- if b == True:
- color = 'white'
- draw.circle(screen, colors[color], [x,y], ballRadius)
- display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement