'Image coordinates in pygame
I am helping my cousin to build a Snakes and Ladders game using pygame. I am also new to pygame so learning as working on game. I took reference from a project to help (https://itsourcecode.com/free-projects/python-projects/snakes-and-ladders-game-in-python-with-source-code/), we will be having your own game board for playing and the above project uses a method with some coordinates matrix for goti movement on the board
# Goti movement function
def movement(a):
l1 = [[406, 606], [456, 606], [506, 606], [556, 606], [606, 606], [656, 606], [706, 606], [756, 606], [806, 606],
[856, 606], [906, 606], [906, 560], [856, 560], [806, 560], [756, 560], [706, 560], [656, 560], [606, 560],
[556, 560], [506, 560], [456, 560], [456, 506], [506, 506], [556, 506], [606, 506], [656, 506], [706, 506],
[756, 506], [806, 506], [856, 506], [906, 506], [906, 460], [856, 460], [806, 460], [756, 460], [706, 460],
[656, 460], [606, 460], [556, 460], [506, 460], [456, 460], [456, 406], [506, 406], [556, 406], [606, 406],
[656, 406], [706, 406], [756, 406], [806, 406], [856, 406], [906, 406], [906, 360], [856, 360], [806, 360],
[756, 360], [706, 360], [656, 360], [606, 360], [556, 360], [506, 360], [456, 360], [456, 306], [506, 306],
[556, 306], [606, 306], [656, 306], [706, 306], [756, 306], [806, 306], [856, 306], [906, 306], [906, 260],
[856, 260], [806, 260], [756, 260], [706, 260], [656, 260], [606, 260], [556, 260], [506, 260], [456, 260],
[456, 206], [506, 206], [556, 206], [606, 206], [656, 206], [706, 206], [756, 206], [806, 206], [856, 206],
[906, 206], [906, 160], [856, 160], [806, 160], [756, 160], [706, 160], [656, 160], [606, 160], [556, 160],
[506, 160], [456, 160]]
l2 = l1[a]
x = l2[0] - 25
y = l2[1] - 25
return x, y
I need to define a similar method with coordinates for my game board. Really appreciate any help in this direction.
Solution 1:[1]
The following can be a skeleton of a possible method of generating custom coordinates for blocks -
- First define a size for each block in your game board.(
BLOCK_SIZE) - Now, decide on an initial block, a block from any one of the corners of the board and note it's coordinates as initial coordinates.(
INIT_COORDS = (xi, yi)) - Now, within a method with appropriate logic based on the shape of the board you can increment the
INIT_COORDS, by BLOCK_SIZE, each iteration to obtain coordinates for another block.
Something similar to the code below -:
COORDS = []
BLOCK_SIZE = ...# The block size for the game board.
INIT_COORDS = [xi, yi] # defining as list to allow changes while iterating.
CURR_COORDS = [xi, yi] # defining as list to allow changes while iterating.
NUM_OF_BLOCKS_IN_POS_X = ...# The number of blocks in the direction of the positive x.
NUM_OF_BLOCKS_IN_POS_Y = ...# The number of blocks in the direction of the positive y.
NUM_OF_BLOCKS_IN_NEG_X = ...# The number of blocks in the direction of the negative x.
NUM_OF_BLOCKS_IN_NEG_Y = ...# The number of blocks in the direction of the negative y.
for k in range(NUM_OF_BLOCKS_IN_POS_X) : # LOOP FOR THE NUMBER OF BLOCKS TO GENERATE IN THIS DIRECTION.
# ALSO NOTE WE COPY THE CURR_COORDS BEFORE APPENDING THEM, AS WE DO NOT WANT THE COORDS TO CHANGE, IF CURR_COORDS IS CHANGED.
COORDS.append(CURR_COORDS.copy()) # APPEND CURRENT COORDINATES TO THE COORDINATE LIST/
CURR_COORDS[0] += BLOCK_SIZE # INCREMENT CURRENT COORDINATES TO THE NEXT BLOCK IN THAT DIRECTION.
continue # CONTINUE LOOPING.
# DO SAME FOR OTHER DIRECTIONS.. ORDER WILL DEPEND ON BOARD ALIGNMENT.
for k in range(NUM_OF_BLOCKS_IN_POS_Y) :
COORDS.append(CURR_COORDS.copy())
CURR_COORDS[1] += BLOCK_SIZE
continue
for k in range(NUM_OF_BLOCKS_IN_NEG_X) :
COORDS.append(CURR_COORDS.copy())
CURR_COORDS[0] -= BLOCK_SIZE
continue
for k in range(NUM_OF_BLOCKS_IN_NEG_Y) :
COORDS.append(CURR_COORDS.copy())
CURR_COORDS[1] -= BLOCK_SIZE
continue
NOTE: Above code is just pseudo code, and can be modified as pleased for the intended use case. The loop order might be changed based on the alignment of the blocks on the board.
Based on your convenience you can add this into your method/function body as well incase you need a method.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
