Skip to content

Commit 1a9bf41

Browse files
authored
You or Me
1 parent 567ca38 commit 1a9bf41

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

Diff for: PyGamesScripts/You or Me/Images/game 1.png

36.5 KB
Loading

Diff for: PyGamesScripts/You or Me/Images/game 2.png

50 KB
Loading

Diff for: PyGamesScripts/You or Me/Images/game _you or me.gif

1.12 MB
Loading

Diff for: PyGamesScripts/You or Me/youorme.py

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import numpy as np
2+
import pygame
3+
import sys
4+
import math
5+
6+
BLUE=(0,255,255)
7+
BLACK=(0,0,0)
8+
RED=(255,0,0)
9+
YELLOW=(255,255,0)
10+
11+
ROW_COUNT=6
12+
COLUMN_COUNT=7
13+
14+
def create_board():
15+
board=np.zeros((ROW_COUNT,COLUMN_COUNT))
16+
return board
17+
18+
def drop_piece(board,row,col,piece):
19+
board[row][col]=piece
20+
21+
def is_valid_location(board,col):
22+
return board[ROW_COUNT-1][col]==0
23+
24+
def get_next_open_row(board,col):
25+
for r in range(ROW_COUNT):
26+
if board[r][col]==0:
27+
return r
28+
29+
def print_board(board):
30+
print(np.flip(board,0))
31+
32+
def winning_move(board,piece):
33+
#check horizontal locations to win
34+
for c in range(COLUMN_COUNT-3):
35+
for r in range(ROW_COUNT):
36+
if board[r][c]==piece and board[r][c+1]==piece and board[r][c+2] and board[r][c+3]==piece:
37+
return True
38+
#check vertical locations to win
39+
for c in range(COLUMN_COUNT):
40+
for r in range(ROW_COUNT-3):
41+
if board[r][c]==piece and board[r+1][c]==piece and board[r+2][c] and board[r+3][c]==piece:
42+
return True
43+
44+
#CHECK POSITIVE SLOPE DIAGONALS
45+
for c in range(COLUMN_COUNT-3):
46+
for r in range(ROW_COUNT-3):
47+
if board[r][c]==piece and board[r+1][c+1]==piece and board[r+2][c+2] and board[r+3][c+3]==piece:
48+
return True
49+
50+
51+
#CHECK NEGATIVE DIAGONALS
52+
for c in range(COLUMN_COUNT-3):
53+
for r in range(3,ROW_COUNT):
54+
if board[r][c]==piece and board[r-1][c+1]==piece and board[r-2][c+2] and board[r-3][c+3]==piece:
55+
return True
56+
57+
def draw_board(board):
58+
for c in range(COLUMN_COUNT):
59+
for r in range(ROW_COUNT):
60+
pygame.draw.rect(screen,BLUE,(c*SQUARESIZE,r*SQUARESIZE+SQUARESIZE,SQUARESIZE,SQUARESIZE))
61+
pygame.draw.circle(screen,BLACK,(int(c*SQUARESIZE+SQUARESIZE/2),int(r*SQUARESIZE+SQUARESIZE+SQUARESIZE/2)),RADIUS)
62+
63+
for c in range(COLUMN_COUNT):
64+
for r in range(ROW_COUNT):
65+
if board[r][c]==1:
66+
pygame.draw.circle(screen,RED,(int(c*SQUARESIZE+SQUARESIZE/2),height-int(r*SQUARESIZE+SQUARESIZE/2)),RADIUS)
67+
68+
elif board[r][c]==2:
69+
pygame.draw.circle(screen,YELLOW,(int(c*SQUARESIZE+SQUARESIZE/2),height-int(r*SQUARESIZE+SQUARESIZE/2)),RADIUS)
70+
71+
pygame.display.update()
72+
73+
74+
board=create_board()
75+
print_board(board)
76+
game_over=False
77+
turn=0
78+
79+
pygame.init()
80+
SQUARESIZE=100
81+
82+
width=COLUMN_COUNT*SQUARESIZE
83+
height=(ROW_COUNT+1)*SQUARESIZE
84+
85+
size=(width,height)
86+
87+
RADIUS=int(SQUARESIZE/2 -5)
88+
89+
screen=pygame.display.set_mode(size)
90+
draw_board(board)
91+
pygame.display.update()
92+
93+
myfont=pygame.font.SysFont("monosoace",75)
94+
95+
while not game_over:
96+
for event in pygame.event.get():
97+
if event.type==pygame.QUIT:
98+
sys.exit()
99+
100+
if event.type==pygame.MOUSEMOTION:
101+
pygame.draw.rect(screen,BLUE,(0,0,width,SQUARESIZE))
102+
posx=event.pos[0]
103+
if turn==0:
104+
pygame.draw.circle(screen,RED,(posx,int(SQUARESIZE/2)),RADIUS)
105+
else:
106+
pygame.draw.circle(screen,YELLOW,(posx,int(SQUARESIZE/2)),RADIUS)
107+
pygame.display.update()
108+
109+
if event.type==pygame.MOUSEBUTTONDOWN:
110+
#print(event.pos)
111+
pygame.draw.rect(screen,BLACK,(0,0,width,SQUARESIZE))
112+
113+
if turn==0:
114+
posx=event.pos[0]
115+
col=int(math.floor(posx/SQUARESIZE))
116+
117+
118+
if is_valid_location(board,col):
119+
row=get_next_open_row(board,col)
120+
drop_piece(board,row,col,1)
121+
122+
if winning_move(board,1):
123+
#print("Player 1 wins CONGRATULATIONS!!")
124+
label=myfont.render("Player 1 wins!!",1,RED)
125+
screen.blit(label,(40,10))
126+
game_over=True
127+
128+
129+
else:
130+
posx=event.pos[0]
131+
col=int(math.floor(posx/SQUARESIZE))
132+
133+
134+
if is_valid_location(board,col):
135+
row=get_next_open_row(board,col)
136+
drop_piece(board,row,col,2)
137+
138+
if winning_move(board,2):
139+
#print("Player 2 wins CONGRATULATIONS!!")
140+
label=myfont.render("Player 2 wins!!",1,YELLOW)
141+
screen.blit(label,(40,10))
142+
game_over=True
143+
144+
145+
146+
147+
print_board(board)
148+
draw_board(board)
149+
150+
turn+=1
151+
turn=turn%2
152+
153+
if game_over:
154+
pygame.time.wait(3000)

0 commit comments

Comments
 (0)