Skip to content

Commit

Permalink
Bug Fix : Updating colors makes the game state disappear (#19)
Browse files Browse the repository at this point in the history
* Bug Fix : Updating colors makes the game state disappear

## The Issue
The colors changing option for lines, points and background caused the entire game state to disappear after the changing the color

## How to replicate the bug
1. Launch the activity
2. Marks a few lines and own a few square
3. Change any one of the colors
4. Notice that the entire board has disappeared 

## Demonstration
<img src="https://thumbs2.imgbox.com/fd/ee/mzsPQ7S6_t.gif" alt="image host"/>

The colors changing option for lines, points and background caused the entire game state to disappear after the changing the color
This was due to the board being cleared and the newer lines having the new colors but the older lines not being redrawn

I have added a function called  `draw_board` on the `Game` class which re-renders all the lines and also the owners of each boxes (if they are owned)

calling the function every time any color is changed ensures that the state is visible even after changing the colors

* floor division instead of plain regular in draw_board

* Fix missing preview request

* bug fix: new game did not reset board only made iboard disappear

* refactored set_board_size and reset_game to make them more modular by implementing clear_game function
  • Loading branch information
marsian83 authored Mar 10, 2023
1 parent fa389f7 commit 63c7fd1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
5 changes: 4 additions & 1 deletion activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,13 @@ def set_current_player(self, player):
self.current_label.set_text(' %s ' % _('Player 2'))

def _new_game(self, widget):
self.game.draw_grid()
self.game.reset_game()

def read_file(self, file_path):
pass

def write_file(self, file_path):
pass

def get_preview(self):
return self._pygamecanvas.get_preview()
36 changes: 33 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ def draw_game_end(self):
self.screen.blit(text, textrect)

def set_board_size(self, size):
self.horizontal = []
self.vertical = []
self.boxes = []
self.clear_game()
self.grid_size = size
self.calc_grid_cant()
self.draw_grid()
Expand All @@ -122,15 +120,47 @@ def calc_grid_cant(self):
def set_point_color(self, color):
self.point_color = color
self.draw_grid()
self.draw_board()

def set_back_color(self, color):
self.back_color = color
self.draw_grid()
self.draw_board()

def set_line_color(self, color):
self.line_color = color
self.draw_grid()
self.draw_board()

def draw_board(self):
for box_row in self.boxes:
for box in box_row:
box_tr = (box.pos_x + self.box_size[0] // 2, box.pos_y - self.box_size[1] // 2)
box_tl = (box.pos_x - self.box_size[0] // 2, box.pos_y - self.box_size[1] // 2)
box_br = (box.pos_x + self.box_size[0] // 2, box.pos_y + self.box_size[1] // 2)
box_bl = (box.pos_x - self.box_size[0] // 2, box.pos_y + self.box_size[1] // 2)

if box.right:
self.draw_line(box_tr,box_br)
if box.left:
self.draw_line(box_tl,box_bl)
if box.up:
self.draw_line(box_tl,box_tr)
if box.down:
self.draw_line(box_bl,box_br)

if box.check:
box.showOwner()

def reset_game(self):
self.clear_game()
self.draw_grid()

def clear_game(self):
self.horizontal = []
self.vertical = []
self.boxes = []

def set_owner_color(self, color):
global COLOR_OWNER
COLOR_OWNER = color
Expand Down

0 comments on commit 63c7fd1

Please sign in to comment.