Advertisement
Guest User

Untitled

a guest
Jul 15th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.12 KB | None | 0 0
  1. # Y1 Final Project Skeleton
  2.  
  3. import pygame
  4. from pygame import *
  5. import random
  6. from random import *
  7.  
  8.  
  9.  
  10. #####################################################################################
  11. #####  Variables that will be used throughout the program
  12. #####################################################################################
  13.  
  14.  
  15. gridHeight = 10 # how many balls down
  16. gridWidth = 6 # how many balls accross
  17.  
  18. cellSize = 70 # both x- and y-dimensions, in pixels
  19.  
  20. windowHeight = cellSize*gridHeight # y
  21. windowWidth = cellSize*gridWidth # x
  22.  
  23. ballRadius = 30 # each ball will therefore fill all but 10 pixels on either side of each cell
  24.  
  25. grid = [] # this will get filled up with more lists that will be filled up with ball tuples
  26.  
  27. colors = {"blue":[0,0,255], "red":[220,20,60], "yellow":[255,255,0],
  28.           "green":[0,205,0], "orange":[255,165,0], "white":[255,255,255]}
  29.  
  30. # animate is a boolean that keeps track of the 'state' of the game
  31. # it is True when the balls are sliding down and False when they're not moving
  32. animate = False
  33.  
  34. screen = display.set_mode((windowWidth, windowHeight))
  35.  
  36.  
  37.  
  38. #####################################################################################
  39. #####  Functions for you to fill in
  40. #####################################################################################
  41.  
  42.  
  43. def pixelsToGrid((x,y)):
  44.     # takes tuple of pixel coordinates, returns a tuple with grid coordinates
  45.     # uses python's int rounding to our advantage
  46.     a,b = (x,y)
  47.     a=int (a)
  48.     b=int(b)
  49.     answer = (a/70 , b/70)
  50.     return answer
  51.  
  52.  
  53. ##print "**pixelsToGrid Tests**"
  54. print pixelsToGrid((0,0)) == (0,0)
  55. print pixelsToGrid((0,80)) == (0,1)
  56. print pixelsToGrid((90,90)) == (1,1)
  57. print ''
  58.  
  59. def initialFillGrid():
  60.     # initially fills the grid with randomly colored balls in each cell
  61.     for i in range (6):
  62.         grid.append ([])
  63.         for j in range (10):
  64.             keys = colors.keys()
  65.             keys.remove("white")
  66.             color = choice (keys)
  67.             grid[i].append((35+cellSize*i,35+j*cellSize,color,False))
  68.  
  69.                
  70. def nextTo((a,b),(c,d)):
  71.     # takes two tuples of grid points
  72.     # returns True if both points are next to each other
  73.     # neighbors are only up/down/side, no diagonals
  74.     if a-c==1 or a-c==-1 or  b-d==1 or b-d==-1:
  75.         if a-c==b-d or a-c==-(b-d):
  76.             return False
  77.         else:
  78.             return True
  79.     else:
  80.         return False
  81.    
  82.  
  83.  
  84. ##print "**nextTo Tests**"
  85. print nextTo((0,0),(0,1)) == True
  86. print nextTo((0,0),(1,0)) == True
  87. print nextTo((0,0),(1,1)) == False
  88. print ''
  89.  
  90.  
  91.  
  92. def generateNeighbors((i,j)):
  93.     # returns a list of all neighbors of (i,j)
  94.     if i!=0 and j!=0 and j!=9 and i!=5:
  95.         answer1=(i-1,j)
  96.         answer2=(i+1,j)
  97.         answer3=(i,j-1)
  98.         answer4=(i,j+1)
  99.         return [answer1,answer2,answer3,answer4]
  100.     else:
  101.         if i==0 and j!=0 and j!=9 and i!=5:
  102.             answer2=(i+1,j)
  103.             answer3=(i,j-1)
  104.             answer4=(i,j+1)
  105.             return [answer2,answer3,answer4]
  106.         if j==0 and i!=0 and j!=9 and i!=5:
  107.             answer1=(i-1,j)
  108.             answer2=(i+1,j)
  109.             answer4=(i,j+1)
  110.             return [answer1,answer2,answer4]
  111.         if j==9 and i!=0 and j!=0 and i!=5:
  112.             answer1=(i-1,j)
  113.             answer2=(i+1,j)
  114.             answer3=(i,j-1)
  115.             return [answer1,answer2,answer3]
  116.         if i==5 and i!=0 and j!=0 and j!=9:
  117.             answer1=(i-1,j)
  118.             answer3=(i,j-1)      
  119.             answer4=(i,j+1)
  120.             return [answer1,answer3,answer4]
  121.         if i==5 and j==0:
  122.             answer1=(i-1,j)
  123.             answer3=(i,j+1)
  124.             return [answer1,answer3]
  125.         if i==5 and j==9:
  126.             answer2=(i-1,j)
  127.             answer4=(i,j-1)
  128.             return [answer2,answer4]
  129.         if i==0 and j==0:
  130.             answer2=(i+1,j)
  131.             answer4=(i,j+1)
  132.             return [answer2,answer4]
  133.         if i==0 and j==9:
  134.             answer2=(i+1,j)
  135.             answer4=(i,j-1)
  136.             return [answer2,answer4]
  137.  
  138.  
  139.  
  140. ##print "**generateNeighbors Tests**"
  141. print set(generateNeighbors((0,0))) == set([(1,0),(0,1)])
  142. print set(generateNeighbors((1,1))) == set([(1,0),(0,1),(1,2),(2,1)])
  143. print set(generateNeighbors((0,1))) == set([(0,0),(1,1),(0,2)])
  144. print ''
  145.  
  146.  
  147.  
  148. def findNeighbors((i,j),color):
  149.     # finds all of the balls connected to (i,j) that are color, and marks them True
  150.     if grid[i][j][3]==True:
  151.         return False
  152.     else:
  153.         color2=grid[i][j][2]
  154.         if color!=color2:
  155.             return False
  156.         else:
  157.             checkList=generateNeighbors((i,j))
  158.             for point in checkList:
  159.                 a,b=point
  160.                 color3=grid[a][b][2]
  161.                 if color2==color3:
  162.                     d,e,f,g=(i,j)
  163.                     g=True
  164.                     (i,j)=d,e,f,g
  165.                     findNeighbors((a,b))
  166.                 else:
  167.                     return False
  168.                      
  169.  
  170. def replace():
  171.     # goes through every ball, checks to see if it is True (ready to be removed)
  172.     # and then removes it and puts an extra ball in that list
  173.     newCol=[]
  174.     howMany=0
  175.     for column in range(6):
  176.         for ball in grid[column]:
  177.             if ball[3]==False:
  178.                 newCol.append(ball)
  179.             else:
  180.                 howMany+=1
  181.         for num in range(howMany):
  182.             color=choice(colors.keys())
  183.             newCol.append((35+cellSize*column,newCol[0][1]+70,color,False))
  184.         grid[column]=newCol
  185.  
  186.  
  187.  
  188. def anyToReplace():
  189.     for i in range(gridWidth):
  190.         for j in range(gridHeight):
  191.             if grid[i][j][3]:
  192.                 return True
  193.     return False
  194.            
  195.  
  196.  
  197. ##print "**anyToReplace Tests**"
  198. grid1 = [[(35, 35, 'red', False), (35, 105, 'blue', False), (35, 175, 'blue', False),
  199.            (35, 245, 'yellow', False), (35, 315, 'green', False), (35, 385, 'red', False),
  200.            (35, 455, 'yellow', False), (35, 525, 'blue', False), (35, 595, 'green', False),
  201.            (35, 665, 'blue', False)], [(105, 35, 'red', False), (105, 105, 'red', False),
  202.             (105, 175, 'yellow', False), (105, 245, 'red', False), (105, 315, 'green', False),
  203.             (105, 385, 'orange', False), (105, 455, 'blue', False), (105, 525, 'green', False),
  204.             (105, 595, 'red', False), (105, 665, 'blue', False)], [(175, 35, 'orange', False),
  205.             (175, 105, 'yellow', False), (175, 175, 'yellow', False), (175, 245, 'green', False),
  206.             (175, 315, 'blue', False), (175, 385, 'orange', False), (175, 455, 'red', False),
  207.             (175, 525, 'orange', False), (175, 595, 'red', False), (175, 665, 'orange', False)],
  208.           [(245, 35, 'green', False), (245, 105, 'blue', False), (245, 175, 'orange', False),
  209.            (245, 245, 'green', False), (245, 315, 'blue', False), (245, 385, 'yellow', False),
  210.            (245, 455, 'yellow', False), (245, 525, 'green', False), (245, 595, 'yellow', False),
  211.            (245, 665, 'red', False)], [(315, 35, 'red', False), (315, 105, 'red', False),
  212.             (315, 175, 'orange', False), (315, 245, 'blue', False), (315, 315, 'blue', False),
  213.             (315, 385, 'red', False), (315, 455, 'orange', False), (315, 525, 'red', False),
  214.             (315, 595, 'orange', False), (315, 665, 'red', False)], [(385, 35, 'red', False),
  215.             (385, 105, 'blue', False), (385, 175, 'orange', False), (385, 245, 'blue', False),
  216.             (385, 315, 'yellow', False), (385, 385, 'blue', False), (385, 455, 'blue', False),
  217.             (385, 525, 'blue', False), (385, 595, 'green', False), (385, 665, 'orange', False)]]
  218. grid2 = [[(35, 35, 'red', False), (35, 105, 'blue', False), (35, 175, 'blue', False),
  219.            (35, 245, 'yellow', False), (35, 315, 'green', False), (35, 385, 'red', False),
  220.            (35, 455, 'yellow', False), (35, 525, 'blue', False), (35, 595, 'green', False),
  221.            (35, 665, 'blue', False)], [(105, 35, 'red', False), (105, 105, 'red', False),
  222.             (105, 175, 'yellow', False), (105, 245, 'red', False), (105, 315, 'green', False),
  223.             (105, 385, 'orange', False), (105, 455, 'blue', False), (105, 525, 'green', False),
  224.             (105, 595, 'red', False), (105, 665, 'blue', False)], [(175, 35, 'orange', False),
  225.             (175, 105, 'yellow', True), (175, 175, 'yellow', False), (175, 245, 'green', False),
  226.             (175, 315, 'blue', False), (175, 385, 'orange', False), (175, 455, 'red', False),
  227.             (175, 525, 'orange', False), (175, 595, 'red', False), (175, 665, 'orange', False)],
  228.           [(245, 35, 'green', False), (245, 105, 'blue', False), (245, 175, 'orange', False),
  229.            (245, 245, 'green', False), (245, 315, 'blue', False), (245, 385, 'yellow', False),
  230.            (245, 455, 'yellow', False), (245, 525, 'green', False), (245, 595, 'yellow', False),
  231.            (245, 665, 'red', False)], [(315, 35, 'red', False), (315, 105, 'red', False),
  232.             (315, 175, 'orange', False), (315, 245, 'blue', False), (315, 315, 'blue', False),
  233.             (315, 385, 'red', False), (315, 455, 'orange', False), (315, 525, 'red', False),
  234.             (315, 595, 'orange', False), (315, 665, 'red', False)], [(385, 35, 'red', False),
  235.             (385, 105, 'blue', False), (385, 175, 'orange', False), (385, 245, 'blue', False),
  236.             (385, 315, 'yellow', False), (385, 385, 'blue', False), (385, 455, 'blue', False),
  237.             (385, 525, 'blue', False), (385, 595, 'green', False), (385, 665, 'orange', False)]]
  238. grid3 = [[(35, 35, 'red', False), (35, 105, 'blue', False), (35, 175, 'blue', False),
  239.            (35, 245, 'yellow', False), (35, 315, 'green', False), (35, 385, 'red', False),
  240.            (35, 455, 'yellow', False), (35, 525, 'blue', True), (35, 595, 'green', False),
  241.            (35, 665, 'blue', False)], [(105, 35, 'red', False), (105, 105, 'red', False),
  242.             (105, 175, 'yellow', False), (105, 245, 'red', False), (105, 315, 'green', False),
  243.             (105, 385, 'orange', False), (105, 455, 'blue', False), (105, 525, 'green', False),
  244.             (105, 595, 'red', False), (105, 665, 'blue', False)], [(175, 35, 'orange', False),
  245.             (175, 105, 'yellow', False), (175, 175, 'yellow', False), (175, 245, 'green', False),
  246.             (175, 315, 'blue', False), (175, 385, 'orange', False), (175, 455, 'red', True),
  247.             (175, 525, 'orange', False), (175, 595, 'red', False), (175, 665, 'orange', False)],
  248.           [(245, 35, 'green', True), (245, 105, 'blue', False), (245, 175, 'orange', False),
  249.            (245, 245, 'green', False), (245, 315, 'blue', True), (245, 385, 'yellow', False),
  250.            (245, 455, 'yellow', False), (245, 525, 'green', False), (245, 595, 'yellow', False),
  251.            (245, 665, 'red', False)], [(315, 35, 'red', False), (315, 105, 'red', False),
  252.             (315, 175, 'orange', False), (315, 245, 'blue', False), (315, 315, 'blue', False),
  253.             (315, 385, 'red', False), (315, 455, 'orange', False), (315, 525, 'red', False),
  254.             (315, 595, 'orange', False), (315, 665, 'red', False)], [(385, 35, 'red', False),
  255.             (385, 105, 'blue', False), (385, 175, 'orange', False), (385, 245, 'blue', False),
  256.             (385, 315, 'yellow', False), (385, 385, 'blue', False), (385, 455, 'blue', False),
  257.             (385, 525, 'blue', False), (385, 595, 'green', False), (385, 665, 'orange', True)]]
  258. grid = grid1[:]
  259. print anyToReplace() == False
  260. grid = grid2[:]
  261. print anyToReplace() == True
  262. grid = grid3[:]
  263. print anyToReplace() == True
  264. print ''
  265.  
  266.  
  267.  
  268.  
  269. def slideDownNicely():
  270.     # the animation!!
  271.     notDone = False
  272.     reference = [35,105,175, 245, 315, 385, 455, 525, 595, 665]
  273.     y+=5
  274.     if y in reference:
  275.         return notDone
  276.  
  277.  
  278.  
  279.  
  280. def anyLeft():
  281.     # retuns False if there are no adjacent balls of the same color left (game over)
  282.     for i in range (6):
  283.         for j in range (10):
  284.             checkList=generateNeighbors((i,j))
  285.             color=grid[i][j][2]
  286.             for point in checkList:
  287.                 a,b=point
  288.                 color2=grid[a][b][2]
  289.                 if color==color2:
  290.                     return True
  291.     return False
  292.            
  293.  
  294.  
  295. ##print "**anyLeft Tests**"
  296. grid4 = [[(35, 35, 0, False), (35, 105, 1, False), (35, 175, 2, False), (35, 245, 3, False),
  297.           (35, 315, 4, False), (35, 385, 5, False), (35, 455, 6, False), (35, 525, 7, False),
  298.           (35, 595, 8, False), (35, 665, 9, False)], [(105, 35, 10, False), (105, 105, 11, False),
  299.         (105, 175, 12, False), (105, 245, 13, False), (105, 315, 14, False), (105, 385, 15, False),
  300.         (105, 455, 16, False), (105, 525, 17, False), (105, 595, 18, False), (105, 665, 19, False)],
  301.          [(175, 35, 20, False), (175, 105, 21, False), (175, 175, 22, False), (175, 245, 23, False),
  302.           (175, 315, 24, False), (175, 385, 25, False), (175, 455, 26, False), (175, 525, 27, False),
  303.           (175, 595, 28, False), (175, 665, 29, False)], [(245, 35, 30, False), (245, 105, 31, False),
  304.         (245, 175, 32, False), (245, 245, 33, False), (245, 315, 34, False), (245, 385, 35, False),
  305.         (245, 455, 36, False), (245, 525, 37, False), (245, 595, 38, False), (245, 665, 39, False)],
  306.          [(315, 35, 40, False), (315, 105, 41, False), (315, 175, 42, False), (315, 245, 43, False),
  307.           (315, 315, 44, False), (315, 385, 45, False), (315, 455, 46, False), (315, 525, 47, False),
  308.           (315, 595, 48, False), (315, 665, 49, False)], [(385, 35, 50, False), (385, 105, 51, False),
  309.         (385, 175, 52, False), (385, 245, 53, False), (385, 315, 54, False), (385, 385, 55, False),
  310.         (385, 455, 56, False), (385, 525, 57, False), (385, 595, 58, False), (385, 665, 59, False)]]
  311. grid5 = [[(35, 35, 0, False), (35, 105, 0, False), (35, 175, 2, False), (35, 245, 3, False),
  312.           (35, 315, 4, False), (35, 385, 5, False), (35, 455, 6, False), (35, 525, 7, False),
  313.           (35, 595, 8, False), (35, 665, 9, False)], [(105, 35, 10, False), (105, 105, 11, False),
  314.         (105, 175, 12, False), (105, 245, 13, False), (105, 315, 14, False), (105, 385, 15, False),
  315.         (105, 455, 16, False), (105, 525, 17, False), (105, 595, 18, False), (105, 665, 19, False)],
  316.          [(175, 35, 20, False), (175, 105, 21, False), (175, 175, 22, False), (175, 245, 23, False),
  317.           (175, 315, 24, False), (175, 385, 25, False), (175, 455, 26, False), (175, 525, 27, False),
  318.           (175, 595, 28, False), (175, 665, 29, False)], [(245, 35, 30, False), (245, 105, 31, False),
  319.         (245, 175, 32, False), (245, 245, 33, False), (245, 315, 34, False), (245, 385, 35, False),
  320.         (245, 455, 36, False), (245, 525, 37, False), (245, 595, 38, False), (245, 665, 39, False)],
  321.          [(315, 35, 40, False), (315, 105, 41, False), (315, 175, 42, False), (315, 245, 43, False),
  322.           (315, 315, 44, False), (315, 385, 45, False), (315, 455, 46, False), (315, 525, 47, False),
  323.           (315, 595, 48, False), (315, 665, 49, False)], [(385, 35, 50, False), (385, 105, 51, False),
  324.         (385, 175, 52, False), (385, 245, 53, False), (385, 315, 54, False), (385, 385, 55, False),
  325.         (385, 455, 56, False), (385, 525, 57, False), (385, 595, 58, False), (385, 665, 59, False)]]
  326. grid6 = [[(35, 35, 0, False), (35, 105, 1, False), (35, 175, 2, False), (35, 245, 3, False),
  327.           (35, 315, 4, False), (35, 385, 5, False), (35, 455, 6, False), (35, 525, 7, False),
  328.           (35, 595, 8, False), (35, 665, 9, False)], [(105, 35, 0, False), (105, 105, 11, False),
  329.         (105, 175, 12, False), (105, 245, 13, False), (105, 315, 14, False), (105, 385, 15, False),
  330.         (105, 455, 16, False), (105, 525, 17, False), (105, 595, 18, False), (105, 665, 19, False)],
  331.          [(175, 35, 0, False), (175, 105, 21, False), (175, 175, 22, False), (175, 245, 23, False),
  332.           (175, 315, 24, False), (175, 385, 25, False), (175, 455, 26, False), (175, 525, 27, False),
  333.           (175, 595, 28, False), (175, 665, 29, False)], [(245, 35, 30, False), (245, 105, 31, False),
  334.         (245, 175, 32, False), (245, 245, 33, False), (245, 315, 34, False), (245, 385, 35, False),
  335.         (245, 455, 36, False), (245, 525, 37, False), (245, 595, 38, False), (245, 665, 39, False)],
  336.          [(315, 35, 40, False), (315, 105, 41, False), (315, 175, 42, False), (315, 245, 43, False),
  337.           (315, 315, 44, False), (315, 385, 45, False), (315, 455, 46, False), (315, 525, 47, False),
  338.           (315, 595, 48, False), (315, 665, 49, False)], [(385, 35, 50, False), (385, 105, 51, False),
  339.         (385, 175, 52, False), (385, 245, 53, False), (385, 315, 54, False), (385, 385, 55, False),
  340.         (385, 455, 56, False), (385, 525, 57, False), (385, 595, 58, False), (385, 665, 59, False)]]
  341.  
  342. grid = grid4[:]
  343. print anyLeft() == False
  344. grid = grid5[:]
  345. print anyLeft() == True
  346. grid = grid6[:]
  347. print anyLeft() == True
  348. print ''
  349.  
  350.  
  351.  
  352.  
  353. #####################################################################################
  354. #####  Drawing things onto the screen
  355. #####################################################################################
  356. #####
  357. #####  Comment out everything below this when checking:
  358. #####  pixelsToGrid, nextTo, generateNeighbors, anyLeft, and anyToReplace
  359. #####
  360. #####  Make sure it is UNcommented when checking initialFillGrid
  361. #####
  362. #####################################################################################
  363.  
  364.  
  365. init() # do all of this only once
  366. screen = display.set_mode((windowWidth, windowHeight))
  367. screen.fill(colors['white'])
  368. initialFillGrid()
  369.  
  370.  
  371. while True: #keep doing this forever
  372.     screen.fill(colors['white']) # wipe clean from last time
  373.    
  374.     if animate == True: #if the balls are sliding don't let the user click
  375.         animate = slideDownNicely()
  376.        
  377.     else: #the user is looking for balls to click
  378.         if anyLeft(): # before the game is over.....
  379.             pass ##### YOU WILL REPLACE THIS WITH CODE LATER
  380.        
  381.         else: # if the game is over
  382.             pass ##### YOU WILL REPLACE THIS WITH CODE LATER
  383.  
  384.  
  385.     # now draw all of the balls on the screen!!
  386.     for row in grid:
  387.         for ball in row:
  388.             x,y,color,b = ball
  389.             if b == True:
  390.                   color = 'white'
  391.                   draw.circle(screen, colors[color], [x,y], ballRadius)
  392.            
  393.     display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement