Python Forum
[PyGame] snakes & ladders
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] snakes & ladders
#1
Hi, I'm quite new to pygame & I've been messing around with a few little things on it, but the other day we were set homework to make snakes & ladders on python, and I wanted to try and see if I could make it on pygame. I've done the code for it as a text based game.

I'm not too sure how to go about it. I wanted to know if (a) the use of dictionaries is the best way to do the grid, and (b) if it is, how do i get it to show the counter at the right coordinates based on the player's score? Also, if there's a simpler way to do it, please point me the right direction - I honestly just want to learn pygame a bit more at this point.

Thank you :)

import pygame, random

playGame = True

pygame.init()
screen = pygame.display.set_mode((600, 500))

places = {
    1 : [10, 410],
    2 : [110, 410],
    3 : [210, 410],
    4 : [310, 410],
    5 : [410, 410],
    6 : [510, 410],
    7 : [510, 310],
    8 : [410, 310],
    9 : [310, 310],
    10 : [210, 310],
    11 : [110, 310],
    12 : [10, 310],
    13 : [10, 210],
    14 : [110, 210],
    15 : [210, 210],
    16 : [310, 210],
    17 : [410, 210],
    18 : [510, 210],
    19 : [510, 110],
    20 : [410, 110],
    21 : [310, 110],
    22 : [210, 110],
    23 : [110, 110],
    24 : [10, 110],
    25 : [10, 10],
    26 : [110, 10],
    27 : [210, 10],
    28 : [310, 10],
    29 : [410, 10],
    30 : [510, 10]}
    

score = [2, 0]
currentPlayer = 0

background = pygame.image.load('board.png').convert()
playerOne = pygame.image.load('player1.png')
playerTwo = pygame.image.load('player2.png')

while playGame:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            playGame = False

    screen.blit(background, (0, 0))
    
    screen.blit(playerOne, ((places[score[currentPlayer]([0])]), (places[score[currentPlayer]([1])])))

    pygame.display.flip()

pygame.quit()
Reply
#2
I would probably programmatically create the table instead of hard coding it, if i wasnt using something like Tiled. That way you could randomize the board on every game play. But i guess you could just do a list.

Ive never played chutes and ladders, so i dont really know how the layout of the game is. But i could guess.

You need to create a Tile class that contains information such as the image to be displayed for each tile, location on the board, its position in relation to the other tiles, etc. Then lay each tile in an order from left to right, top to bottom, until they are gone. Then you could probably add ladders and chutes randomly, and assign to those tiles whether it has a ladder, chute attached, and whether the tile is in the beginning or the end of the chute/ladder. the list of Tile objects would be your database to refer to the tiles information. Then add your player classes, and allow them to move from tile to tile, unless they hit a chute or ladder, etc.

Quote:how do i get it to show the counter at the right coordinates based on the player's score?
If your trying to just display text (counter) to the top right screen you need to make a font object.
https://www.pygame.org/docs/ref/font.html
the player would be in only one location at a given time so you just display that.
Recommended Tutorials:
Reply
#3
i'm using a picture of a board as the background, so i don't need to assign things randomly. i just don't understand how you can use the dictionary to move players to the right coordinates on the board
Reply
#4
images always start at top left.
 Using dictionary are find.
You also could done it in a list.
List would started at 0 - 29 instead 1 - 30,

Suggestion.
If coords are not changable then use tuples instead of list.
places = {
1 : (10, 410),
2 : (110, 410),
#etc
}
or tuples as a list
places = (
(10, 410),
(110, 410),
# etc
)
you can do this
screen.blit(playerOne, places[score[currentPlayer]])
99 percent of computer problems exists between chair and keyboard.
Reply
#5
thank you!!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020