I need a function to pass coordinates as tuples, e.g. (x, y).
This is for my first project.
I want to create a Chessboard with pygame by simply blit white and black squares on the display.
Now, I have managed to blit all the white squares at the right destinations.
What I am trying to do here, is to automate the creation of white squares (later the same will happen with black ones), and more precisely, I am trying to pass the following tuples:
# first column
window.blit(square, (0, 0))
window.blit(square, (0, 200))
window.blit(square, (0, 400))
window.blit(square, (0, 600))
window.blit(square, (0, 800))
# second column
window.blit(square, (100, 100))
window.blit(square, (100, 300))
window.blit(square, (100, 500))
window.blit(square, (100, 700))
# third column
window.blit(square, (200, 0))
window.blit(square, (200, 200))
window.blit(square, (200, 400))
window.blit(square, (200, 600))
window.blit(square, (200, 800))
# fourth column
window.blit(square, (300, 100))
window.blit(square, (300, 300))
window.blit(square, (300, 500))
window.blit(square, (300, 700))
# fifth column
window.blit(square, (400, 0))
window.blit(square, (400, 200))
window.blit(square, (400, 400))
window.blit(square, (400, 600))
window.blit(square, (400, 800))
# seventh column
window.blit(square, (500, 100))
window.blit(square, (500, 300))
window.blit(square, (500, 500))
window.blit(square, (500, 700))
# seventh column
window.blit(square, (600, 0))
window.blit(square, (600, 200))
window.blit(square, (600, 400))
window.blit(square, (600, 600))
window.blit(square, (600, 800))
# eighth column
window.blit(square, (700, 100))
window.blit(square, (700, 300))
window.blit(square, (700, 500))
window.blit(square, (700, 700))
In my coordinator function, I have managed to write the following code, but from here on I don't really know what to do:
def coordinator():
x = 0
y = 0
for column in range(0, 5):
print((x, y))
y += 200
x = 200
y = 0
for column in range(0, 5):
print((x, y))
y += 200
coordinator()
Which gives as output:
(0, 0)
(0, 200)
(0, 400)
(0, 600)
(0, 800)
(200, 0)
(200, 200)
(200, 400)
(200, 600)
(200, 800)
Thanks in advance for any help.
Ok guys good news, I have managed to write some code which prints all the coordinates I need. here is my 'Main' class, and namely display.py:
import pygame
import sys
from coordinator import coordinator
pygame.init()
window_size = (800, 800)
game_window = pygame.display.set_mode(size=window_size)
pygame.display.set_caption('My Game')
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
class WhiteSquare:
height = int(window_size[0] / 8)
width = int(window_size[1] / 8)
a = height
b = width
white_square = pygame.Surface((a, b))
white_square.fill((255, 255, 255), )
ws = WhiteSquare()
class BlueSquare:
height = int(window_size[0] / 8)
width = int(window_size[1] / 8)
a = height
b = width
blue_square = pygame.Surface((a, b))
blue_square.fill((0, 0, 255), )
bs = BlueSquare()
class ChessBoard:
game_window.blit(ws.white_square, (0, 0))
game_window.blit(bs.blue_square, (0, 100))
cb = ChessBoard()
coordinator()
pygame.display.update()
For those solutions proposed which have got a black_square or refer to a black square in any manner, I have obviously changed my 'blue' into 'black'. I am only using a blue colour because I want to make sure that also black squares are working right on a black background (if that makes sense).
Here is the coordinator.py which works good enough by its own. The problem arises when I try to call it in the display.py file.
Have a look:
def coordinator():
o = white_odd_columns()
e = white_even_columns()
print('o: ' + str(o))
print('e: ' + str(e))
def white_odd_columns():
for x in range(0, 800, 200):
print('odd column: ')
y = 0
for first_column in range(0, 5):
odd_coordinate = (x, y)
print('This is odd_coordinate ' + str(odd_coordinate))
y += 200
#return odd_coordinate
def white_even_columns():
for x in range(100, 800, 200):
print('even column: ')
y = 100
for first_column in range(0, 4):
even_coordinate = (x, y)
print('This is even_coordinate ' + str(even_coordinate))
y += 200
#return even_coordinate
white_even_columns()
white_odd_columns()
coordinator()
Now, when I run coordinator I get the following output:
even column:
This is even_coordinate (100, 100)
This is even_coordinate (100, 300)
This is even_coordinate (100, 500)
This is even_coordinate (100, 700)
even column:
This is even_coordinate (300, 100)
This is even_coordinate (300, 300)
This is even_coordinate (300, 500)
This is even_coordinate (300, 700)
even column:
This is even_coordinate (500, 100)
This is even_coordinate (500, 300)
This is even_coordinate (500, 500)
This is even_coordinate (500, 700)
even column:
This is even_coordinate (700, 100)
This is even_coordinate (700, 300)
This is even_coordinate (700, 500)
This is even_coordinate (700, 700)
odd column:
This is odd_coordinate (0, 0)
This is odd_coordinate (0, 200)
This is odd_coordinate (0, 400)
This is odd_coordinate (0, 600)
This is odd_coordinate (0, 800)
odd column:
This is odd_coordinate (200, 0)
This is odd_coordinate (200, 200)
This is odd_coordinate (200, 400)
This is odd_coordinate (200, 600)
This is odd_coordinate (200, 800)
odd column:
This is odd_coordinate (400, 0)
This is odd_coordinate (400, 200)
This is odd_coordinate (400, 400)
This is odd_coordinate (400, 600)
This is odd_coordinate (400, 800)
odd column:
This is odd_coordinate (600, 0)
This is odd_coordinate (600, 200)
This is odd_coordinate (600, 400)
This is odd_coordinate (600, 600)
This is odd_coordinate (600, 800)
odd column:
This is odd_coordinate (0, 0)
This is odd_coordinate (0, 200)
This is odd_coordinate (0, 400)
This is odd_coordinate (0, 600)
This is odd_coordinate (0, 800)
odd column:
This is odd_coordinate (200, 0)
This is odd_coordinate (200, 200)
This is odd_coordinate (200, 400)
This is odd_coordinate (200, 600)
This is odd_coordinate (200, 800)
odd column:
This is odd_coordinate (400, 0)
This is odd_coordinate (400, 200)
This is odd_coordinate (400, 400)
This is odd_coordinate (400, 600)
This is odd_coordinate (400, 800)
odd column:
This is odd_coordinate (600, 0)
This is odd_coordinate (600, 200)
This is odd_coordinate (600, 400)
This is odd_coordinate (600, 600)
This is odd_coordinate (600, 800)
even column:
This is even_coordinate (100, 100)
This is even_coordinate (100, 300)
This is even_coordinate (100, 500)
This is even_coordinate (100, 700)
even column:
This is even_coordinate (300, 100)
This is even_coordinate (300, 300)
This is even_coordinate (300, 500)
This is even_coordinate (300, 700)
even column:
This is even_coordinate (500, 100)
This is even_coordinate (500, 300)
This is even_coordinate (500, 500)
This is even_coordinate (500, 700)
even column:
This is even_coordinate (700, 100)
This is even_coordinate (700, 300)
This is even_coordinate (700, 500)
This is even_coordinate (700, 700)
o: None
e: None
The problem I have got is that in coordinator.py, when I run the coordinator() function, I would like to see a list of tuples with all the coordinates, so that I can then easily access them through the list index.
If that works, then hopefully I can pass the coordinates to the ChessBoard class.
I will keep trying your solutions, although they are a bit too difficult to understand for me since I have just started with python and pygame.
This said, thanks for your time guys, I will keep an eye on your answers.