5
\$\begingroup\$

I was playing around with the pygame module, and decided I should try to make a game. I like to play Gomoku, so that was the game I decided to make.

It is my first experience with pygame, thus I'm guessing there is plenty to improve on. Any stylistic review or more pygame specific would be helpfull.

import pygame
from pygame.locals import *


# Define some colors
BLACK  = (0, 0, 0)
WHITE  = (245, 245, 245)
RED    = (133, 42, 44)
YELLOW = (208, 176, 144)
GREEN  = (26, 81, 79)
PLAYER = False

# Define grid globals
WIDTH = 20
MARGIN = 1
PADDING = 20
DOT = 4
BOARD = (WIDTH + MARGIN) * 14 + MARGIN
GAME_WIDTH = BOARD + PADDING * 2
GAME_HIGHT = GAME_WIDTH + 100


class Gomoku:
    def __init__(self):
        self.grid = [[0 for x in range(15)] for y in range(15)]
        pygame.init()
        pygame.font.init()
        self._display_surf = pygame.display.set_mode((GAME_WIDTH,GAME_HIGHT), pygame.HWSURFACE | pygame.DOUBLEBUF)

        pygame.display.set_caption('Gomoku')

        self._running = True
        self._playing = False
        self._win = False
        self.lastPosition = [-1,-1]

    def on_event(self, event):
        if event.type == pygame.QUIT:
            self._running = False

        if event.type == pygame.MOUSEBUTTONUP:
            #does not update postion in python3.6, and I don't know why
            pos = pygame.mouse.get_pos()
            global PLAYER
            if self.mouse_in_botton(pos):
                if not self._playing:
                    self.start()
                    if PLAYER:
                        PLAYER = not PLAYER
                else:
                    self.surrender()
                    PLAYER = not PLAYER

            elif self._playing:
                r = (pos[0] - PADDING + WIDTH // 2) // (WIDTH + MARGIN)
                c = (pos[1] - PADDING + WIDTH // 2) // (WIDTH + MARGIN)

                if 0 <= r < 15 and 0 <= c < 15:
                    if self.grid[r][c] == 0:
                        self.lastPosition = [r,c]
                        self.grid[r][c] = 1 if PLAYER else 2

                        # check win
                        if self.check_win([r,c],PLAYER):
                            self._win = True
                            self._playing = False
                        else:
                            PLAYER = not PLAYER



    def on_render(self):
        self.render_gomoku_piece()
        self.render_last_position()
        self.render_game_info()
        self.render_button()
        pygame.display.update()


    def on_cleanup(self):
        pygame.quit()


    def on_execute(self):   
        while( self._running ):
            self.gomoku_board_init()
            for event in pygame.event.get():
                self.on_event(event)
            self.on_render()
        self.on_cleanup()


    def start(self):
        self._playing = True
        self.grid = [[0 for x in range(15)] for y in range(15)]
        self.lastPosition = [-1,-1]
        self._win = False


    def surrender(self):
        self._playing = False
        self._win = True


    def gomoku_board_init(self):
        self._display_surf.fill(YELLOW)
        # Draw background rect for game area
        pygame.draw.rect(self._display_surf, BLACK,
                         [PADDING,
                          PADDING,
                          BOARD,
                          BOARD]) 

        # Draw the grid
        for row in range(14):
            for column in range(14):
                pygame.draw.rect(self._display_surf, YELLOW,
                                 [(MARGIN + WIDTH) * column + MARGIN + PADDING,
                                  (MARGIN + WIDTH) * row + MARGIN + PADDING,
                                  WIDTH,
                                  WIDTH])

        # Five dots
        points = [(3,3),(11,3),(3,11),(11,11),(7,7)]
        for point in points:
            pygame.draw.rect(self._display_surf, BLACK,
                            (PADDING + point[0] * (MARGIN + WIDTH) - DOT // 2,
                             PADDING + point[1] * (MARGIN + WIDTH) - DOT // 2,
                             DOT,
                             DOT),0)


    def mouse_in_botton(self,pos):
        if GAME_WIDTH // 2 - 50 <= pos[0] <= GAME_WIDTH // 2 + 50 and GAME_HIGHT - 50 <= pos[1] <= GAME_HIGHT - 20:
           return True
        return False


    def render_button(self):
        color = GREEN if not self._playing else RED
        info = "Start" if not self._playing else "Surrender"

        pygame.draw.rect(self._display_surf, color, 
                         (GAME_WIDTH // 2 - 50, GAME_HIGHT - 50, 100, 30))

        info_font = pygame.font.SysFont('Helvetica', 18)
        text = info_font.render(info, True, WHITE)
        textRect = text.get_rect()
        textRect.centerx = GAME_WIDTH // 2
        textRect.centery = GAME_HIGHT - 35
        self._display_surf.blit(text, textRect)


    def render_game_info(self):
        #current player color
        color = BLACK if not PLAYER else WHITE
        center = (GAME_WIDTH // 2 - 60, BOARD + 60)
        radius = 12
        pygame.draw.circle(self._display_surf, color, center, radius, 0)


        info = "You Win" if self._win else "Your Turn"
        info_font = pygame.font.SysFont('Helvetica', 24)
        text = info_font.render(info, True, BLACK)
        textRect = text.get_rect()
        textRect.centerx = self._display_surf.get_rect().centerx + 20
        textRect.centery = center[1]
        self._display_surf.blit(text, textRect)


    def render_gomoku_piece(self):
        for r in range(15):
            for c in range(15):
                center = ((MARGIN + WIDTH) * r + MARGIN + PADDING,
                          (MARGIN + WIDTH) * c + MARGIN + PADDING)
                if self.grid[r][c] > 0:

                    color = BLACK if self.grid[r][c] == 2 else WHITE
                    pygame.draw.circle(self._display_surf, color,
                                       center,
                                       WIDTH // 2 - MARGIN,0)


    def render_last_position(self):
        if self.lastPosition[0] > 0 and self.lastPosition[1] > 0:
            pygame.draw.rect(self._display_surf,RED,
                             ((MARGIN + WIDTH) * self.lastPosition[0] + (MARGIN + WIDTH) // 2, 
                              (MARGIN + WIDTH) * self.lastPosition[1] + (MARGIN + WIDTH) // 2, 
                              (MARGIN + WIDTH), 
                              (MARGIN + WIDTH)),1)


    def check_win(self,position,player):
        target = 1 if player else 2
        if self.grid[position[0]][position[1]] != target:
            return False
        directions = [([0,1] , [0,-1]) , ([1,0] , [-1,0]) , ([-1,1] , [1,-1]) , ([1,1] , [-1,-1])]
        for direction in directions:
            continue_chess = 0
            for i in range(2):
                p = position[:]
                while 0 <= p[0] < 15 and 0 <= p[1] < 15:
                    if self.grid[p[0]][p[1]] == target:
                        continue_chess += 1
                    else:
                        break
                    p[0] += direction[i][0]
                    p[1] += direction[i][1]
            if continue_chess >= 6:
                return True
        return False


if __name__ == "__main__" :
    gomoku = Gomoku()
    gomoku.on_execute()
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Overall the code looks good to me. Here are few minor improvements you can do:

  • Make the code PEP 8 compatible. Currently you will see decent number of issues if you run it on http://pep8online.com/.
  • [[0 for x in range(15)] for y in range(15)] could be replaced with [[0] * 15 for y in range(15)]. This is fine as 0 is immutable but can be an issue with mutable objects.
  • Instead of having a global variable PLAYER see if you can move it within the class as an instance variable.
  • You have been doing nested loops at multiple places, you can replace them with itertools.product. This reduces an extra level of indentation and will probably be faster too as it removed one Python level for-loop.

    from itertools import product
    
    for row, column in product(range(14), repeat=2):
        ...
    
  • I am not sure whether from pygame.locals import * is a common practice in Pygame to import the constants, but usually import * is frowned upon because it introduces unknown variables in the current namespace. import * based imports can be helpful in shell for debugging and you may sometimes see them in a package's __init__.py to help define a clean public API.
  • It's unclear whether you're using Python 2 or 3. If it's Python 2 then make sure your class inherits from object to make it a new-style class: class Gomoku(object):. Check: New-style and classic classes.
\$\endgroup\$
1
  • \$\begingroup\$ Thank you for your review! I've updated my code with your improvements and made it PEP8 compatible which I should have done a long time ago... \$\endgroup\$
    – Ludisposed
    Aug 28, 2017 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.