- from pygame import*
- #init
- init()
- grid = [[' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' '],
- [' ',' ',' ',' ',' ',' ',' ']]
- #size of the screen and create the screen
- width = 800
- height = 700
- screen = display.set_mode((width,height))
- endProgram = False
- win = ' '
- counter = 'R'
- def swapCounter(counter):
- if counter == 'R':
- counter = 'Y'
- else:
- counter = 'R'
- return counter
- def updateGrid(grid,b,counter):
- a = 5
- while a >= 0:
- if grid[a][b] == ' ':
- grid[a][b] = counter
- a = -1
- if grid[a][b] != ' ':
- a = a - 1
- return grid
- def checkDiags1(Max,row,col):
- count = 0
- while col <= 5 and row <= 6:
- if grid[col][row] == counter:
- count = count + 1
- if Max < count:
- Max = count
- else:
- count = 0
- row = row + 1
- col = col + 1
- return Max
- def checkDiags2(Max,row,col):
- count = 0
- while col >= 0 and row <= 5:
- if grid[col][row] == counter:
- count = count + 1
- if Max < count:
- Max = count
- else:
- count = 0
- row = row + 1
- col = col - 1
- return Max
- def checkWin(counter):
- row = 0
- count = 0
- Max = 0
- while row <= 6:
- col = 0
- while col <= 5:
- if grid[col][row] == counter:
- count = count + 1
- if count > Max:
- Max = count
- else:
- count = 0
- col = col + 1
- row = row + 1
- col = 0
- count = 0
- while col <= 5:
- row = 0
- while row <= 6:
- if grid[col][row] == counter:
- count = count + 1
- if count > Max:
- Max = count
- else:
- count = 0
- row = row + 1
- col = col + 1
- Max = checkDiags1(Max,2,0)
- Max = checkDiags1(Max,1,0)
- Max = checkDiags1(Max,0,0)
- Max = checkDiags1(Max,0,1)
- Max = checkDiags1(Max,0,2)
- Max = checkDiags1(Max,0,3)
- Max = checkDiags2(Max,2,5)
- Max = checkDiags2(Max,1,5)
- Max = checkDiags2(Max,0,5)
- Max = checkDiags2(Max,0,4)
- Max = checkDiags2(Max,0,3)
- Max = checkDiags2(Max,0,2)
- print(Max,counter)
- if Max >= 4:
- print('WIN')
- return counter
- #game loop - always needed. all none set up
- #stuff goes in here
- while endProgram == False:
- for e in event.get():
- if e.type == QUIT:
- endProgram = True
- if e.type == MOUSEBUTTONDOWN:
- X,Y = mouse.get_pos()
- if X >= 60 and X <= 140:
- b = 0
- grid = updateGrid(grid,b,counter)
- if X >= 160 and X <= 240:
- b = 1
- grid = updateGrid(grid,b,counter)
- if X >= 260 and X <= 340:
- b = 2
- grid = updateGrid(grid,b,counter)
- if X >= 360 and X <= 440:
- b = 3
- grid = updateGrid(grid,b,counter)
- if X >= 460 and X <= 540:
- b = 4
- grid = updateGrid(grid,b,counter)
- if X >= 560 and X <= 640:
- b = 5
- grid = updateGrid(grid,b,counter)
- if X >= 660 and X <= 740:
- b = 6
- grid = updateGrid(grid,b,counter)
- win = checkWin(counter)
- counter = swapCounter(counter)
- print (win)
- screen.fill((0,0,0))
- #screen is where we will be drawing to
- #(31,182,255) is the colour of the rectangle
- #(50,50,700,500) is the co-ordinates of the rectangle
- #x,y, width, height
- draw.rect(screen,(31,182,255),(50,50,700,600))
- x = 100
- y = 100
- r = 40
- col = 0
- row = 0
- while col <= 5:
- row = 0
- x = 100
- while row <= 6:
- if grid[col][row] == ' ':
- draw.circle(screen,(255,255,255), (x,y),r)
- elif grid[col][row] == 'R':
- draw.circle(screen,(255,0,0), (x,y),r)
- elif grid[col][row] == 'Y':
- draw.circle(screen,(250,220,22), (x,y),r)
- row = row + 1
- x = x + 100
- col = col + 1
- y = y + 100
- if win == 'R':
- draw.rect(screen,(255,0,0),(50,50,700,600))
- if win == 'Y':
- draw.rect(screen,(250,220,22),(50,50,700,600))
- #show the newly drawn screen
- display.flip()
- #short delay to show animation
- time.delay(5)
[text] connect 4
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: