Help please! This post doesn't have to do with chess.com, but it is relevant to coding in python.

Sort:
krazeechess

So, I just started coding about 6 months ago. I know some HTML, CSS, and Javascript, but I felt like they were just not as fast and easy as python. I decided to start learning the pygame library. Today, I tried to make a chess board with the least amount of lines possible. Here is my code:

import pygame

pygame.init()
window = pygame.display.set_mode([640, 640])
window.fill((0, 0, 0))

x_axis = 0
y_axis = 0
rect = pygame.Rect(x_axis, y_axis, 80, 80)
flag = True

for y in range(0, 8):
for x in range(0, 8):
pygame.draw.rect(window, (255, 255, 255), rect)
pygame.draw.rect(window, (101, 67, 33), rect, 5)
rect = pygame.Rect(x_axis, y_axis, 80, 80)
x_axis += 160
y_axis += 80
if flag:
x_axis = 80
flag = False
else:
x_axis = 0
flag = True
pygame.display.update()
pygame.time.wait(40000)

As you can see, it was 23 lines without the empty lines. I feel like this is a bit lengthy, especially for just a black and white board with brown outlines. Could someone please show me a better way to this (if there is)?

krazeechess

By the way, you can simply copy and paste the code into a python file and run it with an IDE, and it should run properly.

Kadenstarr

it says there is no pygame module

AdamRaichu

If it isn't related to chess.com, it probably shouldn't be posted in this club. However, you could probably make your own club about it if you want.

WhiteDrake

Well, python isn't perl, so shorter doesn't mean better. However,

x_axis = 0
y_axis = 0

could be shortened to

x_axis, y_axis = 0, 0

And then,

    if flag:
x_axis = 80
flag = False
else:
x_axis = 0
flag = True

could be shortened to

    x_axis = 80 if flag else 0
flag = not flag

That's probably also more "pythonic", without unnecessary nesting.

krazeechess
Kadenstarr wrote:

it says there is no pygame module

Well yeah, you need to install the pygame library.

krazeechess
tokenplayer wrote:

Well...have you tried Java?

No, I feel like I should focus on one programming language rather than on 5.

krazeechess
WhiteDrake wrote:

Well, python isn't perl, so shorter doesn't mean better. However,

x_axis = 0
y_axis = 0

could be shortened to

x_axis, y_axis = 0, 0

And then,

if flag:
x_axis = 80
flag = False
else:
x_axis = 0
flag = True

could be shortened to

x_axis = 80 if flag else 0
flag = not flag

That's probably also more "pythonic", without unnecessary nesting.

Wait so if you do "flag = not flag" it makes the boolean variable the opposite value?

krazeechess
AdamRaichu wrote:

If it isn't related to chess.com, it probably shouldn't be posted in this club. However, you could probably make your own club about it if you want.

Alright, I won't do it again. I thought I could post anything related to coding here.

WhiteDrake

I guess you could also shorten this

for y in range(0, 8):
for x in range(0, 8):
pygame.draw.rect(window, (255, 255, 255), rect)
pygame.draw.rect(window, (101, 67, 33), rect, 5)
rect = pygame.Rect(x_axis, y_axis, 80, 80)
x_axis += 160
y_axis += 80

to this

for y_axis in range(0, 8 * 80, 80):
for x_axis in range(0, 8 * 160, 160):
pygame.draw.rect(window, (255, 255, 255), rect)
pygame.draw.rect(window, (101, 67, 33), rect, 5)
rect = pygame.Rect(x_axis, y_axis, 80, 80)

but I haven;t run the code, so it's without any guarantees wink.png I'm sure you can correct the numbers if I made a mistake. That 160 looks weird, I would expect 80. But I didn't look at the code too long.

WhiteDrake
krazeechess wrote:

Wait so if you do "flag = not flag" it makes the boolean variable the opposite value?

Well, "not" negates the boolean expression that comes afterwards. "flag" on the right hand side is that boolean expression whose value gets negated in this case. It might look more clearly if we had something like boolVraiableA = not boolVariableB. Does it make sense?

krazeechess
WhiteDrake wrote:
krazeechess wrote:

Wait so if you do "flag = not flag" it makes the boolean variable the opposite value?

Well, "not" negates the boolean expression that comes afterwards. "flag" on the right hand side is that boolean expression whose value gets negated in this case. It might look more clearly if we had something like boolVraiableA = not boolVariableB. Does it make sense?

Oh yeah I see I was just thinking about it in the wrong way.

krazeechess
WhiteDrake wrote:

I guess you could also shorten this

for y in range(0, 8):
for x in range(0, 8):
pygame.draw.rect(window, (255, 255, 255), rect)
pygame.draw.rect(window, (101, 67, 33), rect, 5)
rect = pygame.Rect(x_axis, y_axis, 80, 80)
x_axis += 160
y_axis += 80

to this

for y_axis in range(0, 8 * 80, 80):
for x_axis in range(0, 8 * 160, 160):
pygame.draw.rect(window, (255, 255, 255), rect)
pygame.draw.rect(window, (101, 67, 33), rect, 5)
rect = pygame.Rect(x_axis, y_axis, 80, 80)

but I haven;t run the code, so it's without any guarantees I'm sure you can correct the numbers if I made a mistake. That 160 looks weird, I would expect 80. But I didn't look at the code too long.

Ah yes so basically I made the background of the window black so that i only need to draw the white squares. That means that the x axis has to increase 160 pixels, since the x axis is supposed to be for the top left corner. That would be two squares, so 160 pixels.

krazeechess
WhiteDrake wrote:

I guess you could also shorten this

for y in range(0, 8):
for x in range(0, 8):
pygame.draw.rect(window, (255, 255, 255), rect)
pygame.draw.rect(window, (101, 67, 33), rect, 5)
rect = pygame.Rect(x_axis, y_axis, 80, 80)
x_axis += 160
y_axis += 80

to this

for y_axis in range(0, 8 * 80, 80):
for x_axis in range(0, 8 * 160, 160):
pygame.draw.rect(window, (255, 255, 255), rect)
pygame.draw.rect(window, (101, 67, 33), rect, 5)
rect = pygame.Rect(x_axis, y_axis, 80, 80)

but I haven;t run the code, so it's without any guarantees I'm sure you can correct the numbers if I made a mistake.

The problem with that is that on a chessboard, the squares on the first file (the A file) alternate between colors. If I were to put that code, then it wouldn't alternate because the x axis of the first square in the row would be the same every time. That would make it 4 columns of black and 4 columns of white. What I mean is that the first white square of the first row has an x axis starting at 0 whereas the first white square of the second row has an x axis starting at 80, and so on. This would make them all start at 0.

wonderbread3333

Missing that purple crayon man.

wonderbread3333

Lol

Elegoo

It doesn't need to be short in particular. Just save it and use it as a module. 

good idea for not learning 5 lang, but sometimes it is handy to know a full stack. (front end, back end, and some sort of data management like SQL)*

 

*please don't be mean about the spelling of SQL (or sequel). I don't have a particular preference, and I know that is a bit controversial.  

Elegoo

If you have pip, you can just run pip install pygame for installing the pygame module

Elegoo

I would be happy to see the finished result when complete.

stephen_33
krazeechess wrote:

No, I feel like I should focus on one programming language rather than on 5.

Very sensible and Python is a great choice - well supported with loads of online help when you hit snags.

BTW, don't worry too much about your topic being 'inappropriate' because it is about chess and I certainly can't see the club admins raising any objections. But this club is mainly for supporting the use of the site's API, so have you tried using that yet?

I haven't looked into the pygame modules before and had to install them - quite a lot to look over. I ran your code (without the delay at the end pygame.time.wait - what's that for?) and the checkerboard opened nicely. You can't close the window by normal means though?

I had to restart the shell to clear it. Is there a method in pygame for closing a window?