Skip to content
Advertisement

How to fix Clock? For Chess. Python

I’m creating a Chess clock for my chess project. In chess there are time formats 5|5 is where each player (white and black) is granted a general 5 minutes, so when either player makes a move it adds 5 seconds to the clock. So 15|10 is 15 minutes in general and 10 seconds per move.

My code:

JavaScript

The issues I face:

So let’s say the player makes a move and the time is at 4:59. We want to add 5 secs. So it adds 5 seconds so 4:64 so it’ll minus a minute so 3:04. I don’t know how to fix that.

Another is that whenever you want to add 5 seconds or whatever it’ll never goes past 59 and add those seconds on to the total and add a minute.

I’ve worked hours on this believe it or not figuring out the simple things. I’ve looked up how to make clocks and things in python (or pygame) and I always get adding clocks. I never found a clock that adds time, but also counts down at the same time.

Whenever I even try to load or click one of the buttons. It takes a few seconds to register it. So if you can help me figure this all out or find out a better way to make it I appreciate it a lot.

Advertisement

Answer

Name changes

First of all, I would rename some variables to better convey their meaning (given that some weren’t immediately obvious), so I changed these:

JavaScript

Button creation

Next, I don’t think you need to re-create your two buttons every frame. Just once should be enough to enable them to be drawn, so I moved them as shown:

JavaScript

Minutes and seconds timer

Next, I like what you were going for with the reset and adding time code, but think in a scenario like this it actually just makes things more complicated. So I removed these if statements:

JavaScript

and moved their logic straight into the pygame event handling loop code:

JavaScript

but this also means that we don’t have any logic to handle what happens if the seconds goes “out of bounds”, so I wrote the following to happen right after our pygame event loop:

JavaScript

Splitting into functions

The refactored code is almost complete, but before that I want to split our code into functions to help maintainability and modularilty. I also really like the use of classes for the buttons.

First I want a “draw everything on the screen” function:

JavaScript

Timer

The reason you feel that the buttons take forever to respond is because pygame can only respond to button presses once a second. This is because of your time.sleep(1), meaning that your event loop is only run once a second. We will need a non-blocking way of decreasing the seconds timer by 1 once a second:

JavaScript

Final code

JavaScript

Thoughts

I haven’t run any of the code, but the thought, intent and improvement is still just as valid. There are still improvements that could be made to the code (having to return minutes and seconds, and passing reset_button and add_button as parameters, are both “not great”) so feel free to use my example as inspiration.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement