Jun-29-2018, 04:32 PM
This is a connect four game, you simply try to connect the photo of your supreme leader
as you would in a normal coonect four game, I used the photo of Abd_El Fatah El SISI as a demonstration of the concept , the game was built using:
[Image: 2ikr6b.png]


- numpy
- pygame
import numpy as np from interface2 import Interface class BoardApp: def __init__(self,interface): # initialise an array equal to board size (7 columns × 6 raws) # values of all element array are set to zero self.raws = 6 self.columns = 7 self.board = np.zeros((self.raws,self.columns)) self.turn = 0 # set turn to zero self.inter = interface # Interface def run(self): # keep playing until a win condition while True: if self.turn == 0: col = self.inter.play(0) # Get the number of column from player 1 if self.valid_col(col): # Check if the column is available # Get the first available raw and drop and draw a piece(1) raw = self.next_row(col) self.drop_piece(raw,col,1) self.inter.draw_piece(col, raw, 1) if self.win(piece=1): # check if there is a win condition for player 1 self.inter.win(0) print("player 1 win") break else: col = self.inter.play(1) #Get the number of column from player 2 if self.valid_col(col): # Check if the column is available # Get the first available raw and drop and draw a piece(2) raw = self.next_row(col) self.drop_piece(raw,col,2) self.inter.draw_piece(col,raw,2) if self.win(piece=2): # check if there is a win condition for player 2 self.inter.win(1) print("palyer 2 win") break self.printboard() # print the board in the background self.change_player() # Change the player def change_player(self): self.turn += 1 # increment by 1 self.turn = self.turn % 2 # set value to remainder of the division by 2 def valid_col(self,col): # check if the last row in column is empty(valid location) return self.board [self.raws-1][col] == 0 def next_row(self,col): # check for the first empty raw of the # selected columns and return the index of the row for r in range(self.raws): if self.board [r][col] == 0: return r def drop_piece(self,raw,col,piece): # drop a piece in the empty slot (set element with this address to 1 or 2) self.board[raw][col] = piece def win(self,piece): # check for horizontal wins for col in range(self.columns - 3): for raw in range(self.raws): if self.board[raw][col] == self.board[raw][col + 1] == self.board[raw][col + 2] == self.board [raw][col + 3] == piece: return True # Check for vertical wins for col in range(self.columns): for raw in range(self.raws - 3): if self.board[raw][col] == self.board[raw + 1][col] == self.board[raw+2][col] == self.board[raw+3][col] == piece: return True # check for diagonal 1(positive) for col in range(self.columns - 3): for raw in range(self.raws - 3): if self.board[raw][col] == self.board[raw + 1][col+1] == self.board[raw+2][col+2] == self.board[raw+3][col+3] == piece: return True # check for diagonal 2(negative) for col in range(self.columns - 3): for raw in range(3,self.raws): if self.board[raw][col] == self.board[raw - 1][col+1] == self.board[raw-2][col+2] == self.board[raw-3][col+3] == piece: return True def printboard(self): # Print flipped board print(np.flip(self.board, 0)) inter = Interface() app = BoardApp(inter) app.run()The Interface
import pygame import sys import math class Interface: def __init__(self): # Initiate a pygame and set the width and height(+ margin) pygame.init() self.width = 630 self.height = 560 #480 + 80 as a margin self.screen = pygame.display.set_mode((self.width,self.height)) pygame.display.set_caption('Connect The Leader') # Change the caption of the window # Load the photos of the supreme leader sis1 and sis2 self.sis1 = pygame.image.load('sis1.png').convert_alpha() self.sis2 = pygame.image.load('sis2.png').convert_alpha() self.board() def board(self): # Draw a big black rectangle as a background # Load the blue board and render it to the display pygame.draw.rect(self.screen,(0,0,0),(0,0,self.width,self.height)) board = pygame.image.load('blue_board.png').convert_alpha() self.screen.blit(board,(0,80)) pygame.display.update() def play(self,turn): # This function get input from the user ( click or mouse cursor moving) # and return the column number 0,1,2,3,4,5,6 while True: for event in pygame.event.get(): # if the user closed the window close the game if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEMOTION: # check if the player moved the mouse cursor # draw the player piece and a black rectangle above it pygame.draw.rect(self.screen, (0, 0, 0), (0, 0, self.width, 82)) pos_x = event.pos[0] # get the x coordinate of the mouse cursor self.mouse_follow(turn,pos_x) pygame.display.update() if event.type == pygame.MOUSEBUTTONDOWN: # get the mouse clicks cor = event.pos col = self.position(cor) return col def mouse_follow(self,turn,pos_x): # Draw a circle that represent the mouse follower if turn == 0: self.screen.blit(self.sis1,(pos_x, 0)) else: self.screen.blit(self.sis2, (pos_x, 0)) def position(self, cor): # approximation of coordinates to 0,1,2,3,4,5,6 x_pos = cor[0] x_pos = math.floor(x_pos / 90) # 90 is the width of the column return x_pos def draw_piece(self,col,raw,player): # Draw/ Drop the piece in the blue board # depending on the player turn x_cor = int(col * 90) y_raw = self.height - int(raw* 80 + 80 ) if player == 1: self.screen.blit(self.sis1,(x_cor, y_raw)) pygame.display.update() else: self.screen.blit(self.sis2, (x_cor, y_raw)) pygame.display.update() def win(self, turn): # Dispaly a win message depending on the player turn clear = pygame.font.SysFont('arial', 40) pygame.draw.rect(self.screen, (0, 0, 0), (0, 0, 630, 80)) if turn == 0: print('player 1') label = clear.render("The supreme leader 1 won ", 1, (255, 255, 255)) self.screen.blit(label, (150, 10)) else: print('player 2') label = clear.render("The supreme leader 2 won", 1, (255, 255, 255)) self.screen.blit(label, (150, 10)) pygame.display.update() pygame.time.wait(3000) # wait for 3000/1000 seconds and then close the window[Image: mifj3d.png]
[Image: 2ikr6b.png]