From e4771ca0778818aa317d3cf363617b89bf3bb4e5 Mon Sep 17 00:00:00 2001 From: Angel ILIEV Date: Fri, 26 Apr 2019 09:36:29 +0300 Subject: [PATCH] - Tidying up style violations --- gui.py | 121 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 64 insertions(+), 57 deletions(-) diff --git a/gui.py b/gui.py index 53697ff..30f6e2f 100644 --- a/gui.py +++ b/gui.py @@ -1,10 +1,5 @@ # tutorial from https://pythonprogramming.net/drawing-objects-pygame-tutorial/?completed=/displaying-text-pygame-screen/ -from functools import partial - import pygame -import time -import random - # RED_RECT = 'ff5e58' # RED_RECT_HIGHLIGHT = 'ffdbd4' @@ -23,6 +18,7 @@ red = (250, 0, 0) green = (0, 200, 0) blue = (0, 0, 250) +yellow = (0, 250, 250) bright_red = (255, 0, 0) bright_green = (0, 255, 0) @@ -37,13 +33,13 @@ } player = PLAYER_X - # car_width = 73 gameDisplay = pygame.display.set_mode((display_width, display_height)) pygame.display.set_caption('A bit Racey') clock = pygame.time.Clock() + # carImg = pygame.image.load('racecar.png') @@ -62,8 +58,10 @@ # # def text_objects(text, font): - textSurface = font.render(text, True, black) - return textSurface, textSurface.get_rect() + text_surface = font.render(text, True, black) + return text_surface, text_surface.get_rect() + + # # # def message_display(text): @@ -91,26 +89,28 @@ def button(msg, x, y, w, h, ic, ac, action=None): else: pygame.draw.rect(gameDisplay, ic, (x, y, w, h)) - smallText = pygame.font.SysFont("comicsansms", 20) - textSurf, textRect = text_objects(msg, smallText) - textRect.center = ((x + (w / 2)), (y + (h / 2))) - gameDisplay.blit(textSurf, textRect) + small_text = pygame.font.SysFont("comicsansms", 20) + text_surf, text_rect = text_objects(msg, small_text) + text_rect.center = ((x + (w / 2)), (y + (h / 2))) + gameDisplay.blit(text_surf, text_rect) + def quitgame(): pygame.quit() quit() + border_thickness = 2 borders = { 'top_left': (border_thickness, border_thickness, -border_thickness, -border_thickness), 'top_right': (0, border_thickness, -border_thickness, -border_thickness), 'bottom_left': (border_thickness, 0, -border_thickness, -border_thickness), 'bottom_right': (0, 0, -border_thickness, -border_thickness), - 'u_shape': (border_thickness, 0, -2*border_thickness, -border_thickness), - 'n_shape': (border_thickness, border_thickness, -2*border_thickness, -border_thickness), - 'o_shape': (border_thickness, border_thickness, -2*border_thickness, -2*border_thickness), - ']_shape': (0, border_thickness, -border_thickness, -2*border_thickness), - '[_shape': (border_thickness, border_thickness, -border_thickness, -2*border_thickness), + 'u_shape': (border_thickness, 0, -2 * border_thickness, -border_thickness), + 'n_shape': (border_thickness, border_thickness, -2 * border_thickness, -border_thickness), + 'o_shape': (border_thickness, border_thickness, -2 * border_thickness, -2 * border_thickness), + ']_shape': (0, border_thickness, -border_thickness, -2 * border_thickness), + '[_shape': (border_thickness, border_thickness, -border_thickness, -2 * border_thickness), } @@ -128,15 +128,15 @@ def draw_subcell(border, border_colour, box_colour, highlight_colour, x, y, w, h # print(click) # print(mouse, x, y, w, h) if x + w > mouse[0] > x and y + h > mouse[1] > y: - pygame.draw.rect(gameDisplay, highlight_colour, (x+mod_x, y+mod_y, w+mod_w, h+mod_h)) + pygame.draw.rect(gameDisplay, highlight_colour, (x + mod_x, y + mod_y, w + mod_w, h + mod_h)) if click[0] == 1 and action is not None: - action(x+mod_x, y+mod_y, w+mod_w, h+mod_h) + action(x + mod_x, y + mod_y, w + mod_w, h + mod_h) else: - pygame.draw.rect(gameDisplay, box_colour, (x+mod_x, y+mod_y, w+mod_w, h+mod_h)) + pygame.draw.rect(gameDisplay, box_colour, (x + mod_x, y + mod_y, w + mod_w, h + mod_h)) -def draw_sub_grid(border, border_colour, box_colour, highlight_colour, x, y, w, h, action=None): +def draw_sub_grid(border, border_colour, box_colour, highlight_colour, x, y, w, h): try: mod_x, mod_y, mod_w, mod_h = borders[border] except KeyError: @@ -144,58 +144,64 @@ def draw_sub_grid(border, border_colour, box_colour, highlight_colour, x, y, w, else: pygame.draw.rect(gameDisplay, border_colour, (x, y, w, h)) # update position and size values for inner rectangle - x, y = x+mod_x, y+mod_y - w, h = w+mod_w, h+mod_h + x, y = x + mod_x, y + mod_y + w, h = w + mod_w, h + mod_h pygame.draw.rect(gameDisplay, box_colour, (x, y, w, h)) cell_size = min(w, h) - x, y = x + 2*sub_grid_padding, y + 2*sub_grid_padding - w = h = cell_size - 4*sub_grid_padding - cell_width = w / 3 - cell_height = h / 3 + x, y = x + 2 * sub_grid_padding, y + 2 * sub_grid_padding + w = h = cell_size - 4 * sub_grid_padding + cell_width_ = w / 3 + cell_height_ = h / 3 - draw_subcell('bottom_right', black, white, green, x, y, cell_width, cell_height, subcell_clicked) - draw_subcell('u_shape', black, white, green, x + (w / 3), y, cell_width, cell_height, + draw_subcell('bottom_right', border_colour, box_colour, highlight_colour, x, y, cell_width_, cell_height_, + subcell_clicked) + draw_subcell('u_shape', border_colour, box_colour, highlight_colour, x + (w / 3), y, cell_width_, cell_height_, subcell_clicked) - draw_subcell('bottom_left', black, white, green, x + w * (2 / 3), y, cell_width, cell_height, + draw_subcell('bottom_left', border_colour, box_colour, highlight_colour, x + w * (2 / 3), y, cell_width_, + cell_height_, subcell_clicked) # middle row - draw_subcell(']_shape', black, white, green, x, y + (h / 3), cell_width, cell_height, + draw_subcell(']_shape', border_colour, box_colour, highlight_colour, x, y + (h / 3), cell_width_, cell_height_, subcell_clicked) - draw_subcell('o_shape', black, white, green, x + w / 3, y + h / 3, cell_width, cell_height, + draw_subcell('o_shape', border_colour, box_colour, highlight_colour, x + w / 3, y + h / 3, cell_width_, + cell_height_, subcell_clicked) - draw_subcell('[_shape', black, white, green, x + w * (2 / 3), y + h / 3, cell_width, - cell_height, subcell_clicked) + draw_subcell('[_shape', border_colour, box_colour, highlight_colour, x + w * (2 / 3), y + h / 3, cell_width_, + cell_height_, subcell_clicked) # bottom row - draw_subcell('top_right', black, white, green, x, y + h * (2 / 3), cell_width, cell_height, + draw_subcell('top_right', border_colour, box_colour, highlight_colour, x, y + h * (2 / 3), cell_width_, + cell_height_, subcell_clicked) - draw_subcell('n_shape', black, white, green, x + w / 3, y + h * (2 / 3), cell_width, - cell_height, subcell_clicked) - draw_subcell('top_left', black, white, green, x + w * (2 / 3), y + h * (2 / 3), cell_width, - cell_height, subcell_clicked) + draw_subcell('n_shape', border_colour, box_colour, highlight_colour, x + w / 3, y + h * (2 / 3), cell_width_, + cell_height_, subcell_clicked) + draw_subcell('top_left', border_colour, box_colour, highlight_colour, x + w * (2 / 3), y + h * (2 / 3), cell_width_, + cell_height_, subcell_clicked) def draw_main_grid(pos_x, pos_y): # top row - draw_sub_grid('bottom_right', black, white, green, pos_x, pos_y, main_box_width, main_box_height, partial(print, ('hello',))) - draw_sub_grid('u_shape', black, white, green, pos_x + board_width / 3, pos_y, main_box_width, main_box_height, - partial(print, ('hello',))) - draw_sub_grid('bottom_left', black, white, green, pos_x + board_width * (2 / 3), pos_y, main_box_width, main_box_height, - partial(print, ('hello',))) + draw_sub_grid(border='bottom_right', border_colour=black, box_colour=white, highlight_colour=green, + x=pos_x, y=pos_y, w=main_box_width, h=main_box_height) + draw_sub_grid(border='u_shape', border_colour=black, box_colour=white, highlight_colour=green, + x=pos_x + board_width / 3, y=pos_y, w=main_box_width, h=main_box_height) + draw_sub_grid(border='bottom_left', border_colour=black, box_colour=white, highlight_colour=green, + x=pos_x + board_width * (2 / 3), y=pos_y, w=main_box_width, h=main_box_height) # middle row - draw_sub_grid(']_shape', black, white, green, pos_x, pos_y + board_height / 3, main_box_width, main_box_height, - partial(print, ('hello',))) - draw_sub_grid('o_shape', black, white, green, pos_x + board_width / 3, pos_y + board_height / 3, main_box_width, main_box_height, - partial(print, ('hello',))) - draw_sub_grid('[_shape', black, white, green, pos_x + board_width * (2 / 3), pos_y + board_height / 3, main_box_width, - main_box_height, partial(print, ('hello',))) + draw_sub_grid(border=']_shape', border_colour=black, box_colour=white, highlight_colour=green, + x=pos_x, y=pos_y + board_height / 3, w=main_box_width, h=main_box_height) + draw_sub_grid(border='o_shape', border_colour=black, box_colour=white, highlight_colour=green, + x=pos_x + board_width / 3, y=pos_y + board_height / 3, w=main_box_width, h=main_box_height) + draw_sub_grid(border='[_shape', border_colour=black, box_colour=white, highlight_colour=green, + x=pos_x + board_width * (2 / 3), y=pos_y + board_height / 3, w=main_box_width, h=main_box_height) # bottom row - draw_sub_grid('top_right', black, white, green, pos_x, pos_y + board_height * (2 / 3), main_box_width, main_box_height, - partial(print, ('hello',))) - draw_sub_grid('n_shape', black, white, green, pos_x + board_width / 3, pos_y + board_height * (2 / 3), main_box_width, - main_box_height, partial(print, ('hello',))) - draw_sub_grid('top_left', black, white, green, pos_x + board_width * (2 / 3), pos_y + board_height * (2 / 3), main_box_width, - main_box_height, partial(print, ('hello',))) + draw_sub_grid(border='top_right', border_colour=black, box_colour=white, highlight_colour=green, + x=pos_x, y=pos_y + board_height * (2 / 3), w=main_box_width, h=main_box_height) + draw_sub_grid(border='n_shape', border_colour=black, box_colour=white, highlight_colour=green, + x=pos_x + board_width / 3, y=pos_y + board_height * (2 / 3), w=main_box_width, h=main_box_height) + draw_sub_grid(border='top_left', border_colour=black, box_colour=white, highlight_colour=green, + x=pos_x + board_width * (2 / 3), y=pos_y + board_height * (2 / 3), w=main_box_width, + h=main_box_height) def subcell_clicked(x, y, w, h): @@ -215,7 +221,7 @@ def draw_clicked_cells(): main_box_height = board_height / 3 cell_width = main_box_width / 3 cell_height = main_box_height / 3 -offset_x, offset_y = (display_width - board_width)/2, (display_height - board_height)/2 +offset_x, offset_y = (display_width - board_width) / 2, (display_height - board_height) / 2 while True: for event in pygame.event.get(): @@ -229,3 +235,4 @@ def draw_clicked_cells(): pygame.display.update() clock.tick(60) +# TODO replace 3 with board size