Snakes and Ladders Game in Python with Source Code
Snakes and Ladders Game is developed in Python Programming Language and it is a desktop application. This Snakes and Ladders Game in Python is free to download the open source code and it is created for the beginners who wants to learn python. This project system used a Pygame and Random module. Pygame is a cross-platform set of Python modules designed for writing video games. This is the game between computer and the user.
This Snakes and Ladders Game contains python file scripts (snakes_ladders.py), resources files and sound files. The gameplay of the system, which is the user can choose an option either to play multiple participant or with the computer.
Beginning of the game, the player needs to roll the dice and in the wake of moving it the game moves the token consequently as indicated by the dice number. The interactivity is like the genuine one. Here, the player likewise gets one more opportunity to roll the dice at whatever point he/she gets 6 number.
There are quantities of stepping stools and snakes in the game which causes the player to update or minimization the square number. The player who arrives at the last square of the track is the champ.
Watch the video here to see the full running Snakes and Ladders game in python with source code.
Anyway if you want level up your knowledge in programming especially games in python, try this new article I’ve made for you Code For Game in Python: Python Game Projects With Source Code
Before you start on how to create Snakes and Ladders Game in Python, make sure that you have PyCharm IDE and Pygame installed in your computer.
This are the steps on How to Create Snakes and Ladders Game in Python.
Snakes and Ladders Game in Python with Source Code
- Step 1: Create a project name.
First when you finished installed the Pycharm IDE in your computer, open it and then create a “project name” after creating a project name click the “create” button.
- Step 2: Create a python file.
Second after creating a project name, “right click” your project name and then click “new” after that click the “python file“.
- Step 3: Name your python file.
Third after creating a python file, Name your python file after that click “enter“.
- Step 4: The actual code.
This is the actual coding on how to create Snakes and Ladders Game in Python, and you are free to copy this code and download the full source code given below.
Importing Random Module
In the code below. which is for the random() function, which generates random numbers between 0 and 1.
1 |
from random import randint |
Importing Pygame Module
In the code given below, which is pygame library is an open-source module for the Python programming language specifically intended to help you make games and other multimedia applications. Pygame can run across many platforms and operating systems.(Snakes and Ladders Game in Python)
1 |
import pygame |
This module is for the graphics
In the code given below, which is for the graphics color use in a text.(Snakes and Ladders Game in Python)
1 2 3 4 5 6 7 8 9 10 11 |
black_color = (10, 10, 10) white_color = (250, 250, 250) red_color = (200, 0, 0) blue_red_color = (240, 0, 0) green_color = (0, 200, 0) blue_green_color = (0, 230, 0) blue_color = (0, 0, 200) grey_color = (50, 50, 50) yellow_color = (150, 150, 0) purple_color = (43, 3, 132) blue_purple_color = (60, 0, 190) |
This module is for the dice image
In the code given below, which is for the dice image use in a game.(Snakes and Ladders Game in Python)
1 2 3 4 5 6 |
d1 = pygame.image.load("resources/dice_image1.png") d2 = pygame.image.load("resources/dice_image2.png") d3 = pygame.image.load("resources/dice_image3.png") d4 = pygame.image.load("resources/dice_image4.png") d5 = pygame.image.load("resources/dice_image5.png") d6 = pygame.image.load("resources/dice_image6.png") |
This module is for the Introduction image
In the code given below, which is the introduction image use in a game.
1 2 3 4 5 6 |
initial_background = pygame.image.load("resources/introduction_image.png") initial_background2 = pygame.image.load("resources/introduction_image2.jpg") initial_background3 = pygame.image.load("resources/introduction_image3.jpg") initial_background4 = pygame.image.load("resources/introduction_image4.jpg") initial_background5 = pygame.image.load("resources/introduction_image5.jpg") creditations1 = pygame.image.load("resources/owner.jpg") |
This module is for the sound use in a game
In the code given below, which is for the music background use in a game.(Snakes and Ladders Game in Python)
1 2 3 4 5 |
pygame.mixer.music.load("sound/music.wav") snake_sound = pygame.mixer.Sound("sound/snake.wav") win = pygame.mixer.Sound("sound/win.wav") lose = pygame.mixer.Sound("sound/lose.wav") ladder = pygame.mixer.Sound("sound/ladder.wav") |
This module is for the ladders
In the code given below, which is for the function of ladders use in a system.(Snakes and Ladders Game in Python)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
def ladders(x): if x == 1: return 38 elif x == 4: return 14 elif x == 9: return 31 elif x == 28: return 84 elif x == 21: return 42 elif x == 51: return 67 elif x == 80: return 99 elif x == 72: return 91 else: return x |
This module is for the snakes
In the code given below, which is for the function of snake use in a system.(Snakes and Ladders Game in Python)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
def snakes(x): if x == 17: return 7 elif x == 54: return 34 elif x == 62: return 19 elif x == 64: return 60 elif x == 87: return 36 elif x == 93: return 73 elif x == 95: return 75 elif x == 98: return 79 else: return x |
This module is for the dice
In the code given below, which is for the function of number of dice use in a system.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
def dice(d): if d == 1: d = d1 elif d == 2: d = d2 elif d == 3: d = d3 elif d == 4: d = d4 elif d == 5: d = d5 elif d == 6: d = d6 |
This module is for the quit
In the code given below, which is for the function of exit/quit use in game.
1 2 3 |
def Quit(): pygame.quit() quit() |
This module is for the main menu
In the code given below, which is for the function of main menu in a system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
def main_menu(): pygame.mixer.music.play(-1) m = True while m: for event in pygame.event.get(): if event.type == pygame.QUIT: Quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: Quit() # mouse position mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() game_layout_display.blit(menu_background, (0, 0)) button("Play", mouse[0], mouse[1], (width_screen / 2 - 100), height_screen / 2, 200, 100, green_color, blue_green_color, 60, 1) button("Quit", mouse[0], mouse[1], (width_screen / 2 - 100), (height_screen / 2) + 200, 200, 100, red_color, blue_red_color, 60, 0) mouse = pygame.mouse.get_pos() if button2("Mute Music", mouse[0], mouse[1], 1166, 0, 200, 50, purple_color, blue_purple_color, 25): pygame.mixer.music.pause() if button2("Play Music", mouse[0], mouse[1], 1166, 75, 200, 50, purple_color, blue_purple_color, 25): pygame.mixer.music.unpause() if button2("Credits", mouse[0], mouse[1], 1166, 150, 200, 50, purple_color, blue_purple_color, 25): creditation() pygame.display.update() |
This module is for the choice or options for players
In the code given below, which is for the function of a players that you want to choose either single player, two player, three player, or four player and it is color design .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
def choice(): f = True while f == True: for event in pygame.event.get(): if event.type == pygame.QUIT: Quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: Quit() # mouse position mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() best1 = best2 = best3 = best4 = best5 = -1 game_layout_display.blit(menu_background, (0, 0)) # Single player button best1 = button("Single Player", mouse[0], mouse[1], (width_screen / 2 - 150), 250, 300, 50, green_color, blue_green_color, 30, "s") # 2 player button best2 = button("2 Players", mouse[0], mouse[1], (width_screen / 2) - 150, 350, 300, 50, green_color, blue_green_color, 30, 2) # 3 player best3 = button("3 Players", mouse[0], mouse[1], (width_screen / 2) - 150, 450, 300, 50, green_color, blue_green_color, 30, 3) # 4 player best4 = button("4 Players", mouse[0], mouse[1], (width_screen / 2) - 150, 550, 300, 50, green_color, blue_green_color, 30, 4) # Back button best5 = button("Back", mouse[0], mouse[1], 0, 650, 200, 50, red_color, blue_red_color, 30, 5) if best5 == 5: main_menu() if best1 == "s": played(21) if best2 == 2: played(2) if best3 == 3: played(3) if best4 == 4: played(4) pygame.display.update() |
This module is for playing in a game
In the code given below, which is for the function of all players you choose in a game.(Snakes and Ladders Game in Python)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
def playing(best): best6 = -1 time = 3000 if best6 == 7: choice() game_layout_display.blit(post, (0, 0)) game_layout_display.blit(mother_board, (width_screen / 2 - 250, height_screen / 2 - 250)) xcr = xcy = xcg = xcb = 406 - 25 ycr = ycy = ycg = ycb = 606 - 25 game_layout_display.blit(red_c, (xcy, ycy)) if 5 > best > 1 or best == 21: game_layout_display.blit(yellow_c, (xcy, ycy)) if 5 > best > 2 or best == 21: game_layout_display.blit(green_c, (xcg, ycg)) if 5 > best > 2: game_layout_display.blit(blue_c, (xcb, ycb)) gamer1 = "Player 1" gamer1score = 0 if best == 21: gamer2 = "Computer" gamer2score = 0 if 5 > best > 1: gamer2 = "Player 2" gamer2score = 0 if 5 > best > 2: gamer3 = "Player 3" gamer3score = 0 if 5 > best > 3: gamer4 = "Player 4" gamer4score = 0 tips = 1 play = True while True: less = False set = False time = 3000 game_layout_display.blit(post, (0, 0)) game_layout_display.blit(mother_board, (width_screen / 2 - 250, height_screen / 2 - 250)) mouse = pygame.mouse.get_pos() for event in pygame.event.get(): if event.type == pygame.QUIT: Quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: Quit() if best == 21: if button1("Player 1", mouse[0], mouse[1], 100, 700, 200, 50, red_color, grey_color, 30): if tips == 1: gamer1score, less, set, six = turn(gamer1score, less, set) if not six: tips += 1 xcr, ycr = movement(gamer1score) if gamer1score == 100: time = pygame.time.get_ticks() while pygame.time.get_ticks() - time < 2000: message_display1_screen("Player 1 Wins", 650, 50, 50, blue_color) pygame.mixer.Sound.play(win) pygame.display.update() break button1("Computer", mouse[0], mouse[1], 400, 700, 200, 50, yellow_color, grey_color, 30) if True: if tips == 2: gamer2score, less, set, six = turn(gamer2score, less, set) xcy, ycy = movement(gamer2score) if not six: tips += 1 if best < 3 or best == 21: tips = 1 if gamer2score == 100: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2000: message_display1_screen("Computer Wins", 650, 50, 50, black_color) pygame.mixer.Sound.play(lose) pygame.display.update() break if 5 > best > 1: if button1("Player 1", mouse[0], mouse[1], 100, 700, 200, 50, red_color, grey_color, 30): if tips == 1: gamer1score, less, set, six = turn(gamer1score, less, set) xcr, ycr = movement(gamer1score) if not six: tips += 1 if gamer1score == 100: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2000: message_display1_screen("Player 1 Wins", 650, 50, 50, black_color) pygame.mixer.Sound.play(win) pygame.display.update() break if button1("Player 2", mouse[0], mouse[1], 400, 700, 200, 50, yellow_color, grey_color, 30): if tips == 2: gamer2score, less, set, six = turn(gamer2score, less, set) xcy, ycy = movement(gamer2score) if not six: tips += 1 if best < 3: tips = 1 if gamer2score == 100: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2000: message_display1_screen("Player 2 Wins", 650, 50, 50, black_color) pygame.mixer.Sound.play(win) pygame.display.update() break if 5 > best > 2: if button1("Player 3", mouse[0], mouse[1], 700, 700, 200, 50, green_color, grey_color, 30): if tips == 3: gamer3score, less, set, six = turn(gamer3score, less, set) xcg, ycg = movement(gamer3score) if not six: tips += 1 if best < 4: tips = 1 if gamer3score == 100: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2000: message_display1_screen("Player 3 Wins", 650, 50, 50, black_color) pygame.mixer.Sound.play(win) pygame.display.update() break if 5 > best > 3: if button1("Player 4", mouse[0], mouse[1], 1000, 700, 200, 50, blue_color, grey_color, 30): if tips == 4: gamer4score, less, set, six = turn(gamer4score, less, set) xcb, ycb = movement(gamer4score) if not six: tips += 1 if best < 5: tips = 1 if gamer4score == 100: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2000: message_display1_screen("Player 4 Wins", 650, 50, 50, black_color) pygame.mixer.Sound.play(win) pygame.display.update() break best6 = button("Back", mouse[0], mouse[1], 0, 0, 200, 50, red_color, blue_red_color, 30, 7) game_layout_display.blit(red_c, (xcr, ycr)) if 5 > best > 1 or best == 21: game_layout_display.blit(yellow_c, (xcy + 2, ycy)) if 5 > best > 2: game_layout_display.blit(green_c, (xcg + 4, ycg)) if 5 > best > 3: game_layout_display.blit(blue_c, (xcb + 6, ycb)) if less: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2000: message_display1_screen("There's a Ladder!", 650, 50, 35, black_color) pygame.display.update() if set: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2000: message_display1_screen("There's a Snake!", 650, 50, 35, black_color) pygame.display.update() time_clocks.tick(7) pygame.display.update() |
Complete Source Code of Snakes and Ladders Game in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
import pygame from random import randint time_clocks = pygame.time.Clock() # Initialize pygame.init() width_screen = 1366 height_screen = 768 ic = pygame.image.load("resources/icon.png") game_layout_display = pygame.display.set_mode((width_screen, height_screen), pygame.FULLSCREEN) pygame.display.set_caption("Snakes and Ladders Game ") pygame.display.set_icon(ic) pygame.display.update() # Graphics: black_color = (10, 10, 10) white_color = (250, 250, 250) red_color = (200, 0, 0) blue_red_color = (240, 0, 0) green_color = (0, 200, 0) blue_green_color = (0, 230, 0) blue_color = (0, 0, 200) grey_color = (50, 50, 50) yellow_color = (150, 150, 0) purple_color = (43, 3, 132) blue_purple_color = (60, 0, 190) mother_board = pygame.image.load("resources/Snakes_ladders_big_image.png") d1 = pygame.image.load("resources/dice_image1.png") d2 = pygame.image.load("resources/dice_image2.png") d3 = pygame.image.load("resources/dice_image3.png") d4 = pygame.image.load("resources/dice_image4.png") d5 = pygame.image.load("resources/dice_image5.png") d6 = pygame.image.load("resources/dice_image6.png") red_c = pygame.image.load("resources/red_c.png") yellow_c = pygame.image.load("resources/yellow_c.png") green_c = pygame.image.load("resources/green_c.png") blue_c = pygame.image.load("resources/blue_c.png") menu_background = pygame.image.load("resources/menu.jpg") post = pygame.image.load("resources/game_background.jpg") initial_background = pygame.image.load("resources/introduction_image.png") initial_background2 = pygame.image.load("resources/introduction_image2.jpg") initial_background3 = pygame.image.load("resources/introduction_image3.jpg") initial_background4 = pygame.image.load("resources/introduction_image4.jpg") initial_background5 = pygame.image.load("resources/introduction_image5.jpg") creditations1 = pygame.image.load("resources/owner.jpg") pygame.mixer.music.load("sound/music.wav") snake_sound = pygame.mixer.Sound("sound/snake.wav") win = pygame.mixer.Sound("sound/win.wav") lose = pygame.mixer.Sound("sound/lose.wav") ladder = pygame.mixer.Sound("sound/ladder.wav") # mouse position mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() # Message displaying for buttons def message_display_screen(text, x, y, fs): largeText = pygame.font.Font('freesansbold.ttf', fs) TextSurf, TextRect = text_objects_screen(text, largeText) TextRect.center = (x, y) game_layout_display.blit(TextSurf, TextRect) def text_objects_screen(text, font): textSurface = font.render(text, True, white_color) return textSurface, textSurface.get_rect() # Message displaying for field def message_display1_screen(text, x, y, fs, c): largeText = pygame.font.Font('freesansbold.ttf', fs) TextSurf, TextRect = text_objects1(text, largeText) TextRect.center = (x, y) game_layout_display.blit(TextSurf, TextRect) def text_objects1_screen(text, font, c): textSurface = font.render(text, True, c) return textSurface, textSurface.get_rect() # 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 def text_objects1(text, font): textSurface = font.render(text, True, blue_color) return textSurface, textSurface.get_rect() # Ladder check def ladders(x): if x == 1: return 38 elif x == 4: return 14 elif x == 9: return 31 elif x == 28: return 84 elif x == 21: return 42 elif x == 51: return 67 elif x == 80: return 99 elif x == 72: return 91 else: return x # Snake Check def snakes(x): if x == 17: return 7 elif x == 54: return 34 elif x == 62: return 19 elif x == 64: return 60 elif x == 87: return 36 elif x == 93: return 73 elif x == 95: return 75 elif x == 98: return 79 else: return x def dice(d): if d == 1: d = d1 elif d == 2: d = d2 elif d == 3: d = d3 elif d == 4: d = d4 elif d == 5: d = d5 elif d == 6: d = d6 time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 1000: game_layout_display.blit(d, (300, 500)) pygame.display.update() # for mute and unmute def button2(t, xm, ym, x, y, wid, hei, int, after, fast): # mouse position mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if x + wid > xm > x and y + hei > ym > y: pygame.draw.rect(game_layout_display, after, [x - 2.5, y - 2.5, wid + 5, hei + 5]) if pygame.mouse.get_pressed() == (1, 0, 0): return True else: pygame.draw.rect(game_layout_display, int, [x, y, wid, hei]) message_display_screen(t, (x + wid + x) / 2, (y + hei + y) / 2, fast) # Buttons for playing: def button1(t, xm, ym, x, y, wid, hei, int, after, fast): # mouse position mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if x + wid > xm > x and y + hei > ym > y: pygame.draw.rect(game_layout_display, after, [x - 2.5, y - 2.5, wid + 5, hei + 5]) if pygame.mouse.get_pressed() == (1, 0, 0): return True else: pygame.draw.rect(game_layout_display, int, [x, y, wid, hei]) message_display_screen(t, (x + wid + x) / 2, (y + hei + y) / 2, fast) # Turn def turn(sc, lefted, section): d = randint(1, 6) # player dice roll if d == 6: six = True else: six = False p = dice(d) sc += d if sc <= 100: laddle = ladders(sc) # checking for ladders for player if laddle != sc: lefted = True pygame.mixer.Sound.play(ladder) time_clock = pygame.time.get_ticks() sc = laddle sink = snakes(sc) if sink != sc: # checking for snakes for player section = True pygame.mixer.Sound.play(snake_sound) sc = sink else: # checks if player score is not grater than 100 sc -= d time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 1500: message_display1_screen("Can't move!", 650, 50, 35, black_color) pygame.display.update() return sc, lefted, section, six # Quitting: def Quit(): pygame.quit() quit() # Buttons: def button(t, xm, ym, x, y, wid, hei, int, after, fast, best): if x + wid > xm > x and y + hei > ym > y: pygame.draw.rect(game_layout_display, after, [x - 2.5, y - 2.5, wid + 5, hei + 5]) if pygame.mouse.get_pressed() == (1, 0, 0): if best == 1: choice() elif best == 5: return 5 elif best == 0: Quit() elif best == "s" or best == 2 or best == 3 or best == 4: return best elif best == 7: choice() else: return True else: pygame.draw.rect(game_layout_display, int, [x, y, wid, hei]) message_display_screen(t, (x + wid + x) / 2, (y + hei + y) / 2, fast) def introduction(): time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2500: game_layout_display.blit(initial_background, (0, 0)) pygame.display.update() while True: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 500: game_layout_display.blit(initial_background2, (0, 0)) pygame.display.update() time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 500: game_layout_display.blit(initial_background3, (0, 0)) pygame.display.update() time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 500: game_layout_display.blit(initial_background4, (0, 0)) pygame.display.update() time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 500: game_layout_display.blit(initial_background5, (0, 0)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: return pygame.display.update() def creditation(): while True: game_layout_display.blit(creditations1, (0, 0)) for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: Quit() # mouse position mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if button("Back", mouse[0], mouse[1], width_screen / 2 - 100, 700, 200, 50, red_color, blue_red_color, 25, 20): main_menu() pygame.display.update() # Main Menu def main_menu(): pygame.mixer.music.play(-1) m = True while m: for event in pygame.event.get(): if event.type == pygame.QUIT: Quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: Quit() # mouse position mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() game_layout_display.blit(menu_background, (0, 0)) button("Play", mouse[0], mouse[1], (width_screen / 2 - 100), height_screen / 2, 200, 100, green_color, blue_green_color, 60, 1) button("Quit", mouse[0], mouse[1], (width_screen / 2 - 100), (height_screen / 2) + 200, 200, 100, red_color, blue_red_color, 60, 0) mouse = pygame.mouse.get_pos() if button2("Mute Music", mouse[0], mouse[1], 1166, 0, 200, 50, purple_color, blue_purple_color, 25): pygame.mixer.music.pause() if button2("Play Music", mouse[0], mouse[1], 1166, 75, 200, 50, purple_color, blue_purple_color, 25): pygame.mixer.music.unpause() if button2("Credits", mouse[0], mouse[1], 1166, 150, 200, 50, purple_color, blue_purple_color, 25): creditation() pygame.display.update() # Options Menu: def choice(): f = True while f == True: for event in pygame.event.get(): if event.type == pygame.QUIT: Quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: Quit() # mouse position mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() best1 = best2 = best3 = best4 = best5 = -1 game_layout_display.blit(menu_background, (0, 0)) # Single player button best1 = button("Single Player", mouse[0], mouse[1], (width_screen / 2 - 150), 250, 300, 50, green_color, blue_green_color, 30, "s") # 2 player button best2 = button("2 Players", mouse[0], mouse[1], (width_screen / 2) - 150, 350, 300, 50, green_color, blue_green_color, 30, 2) # 3 player best3 = button("3 Players", mouse[0], mouse[1], (width_screen / 2) - 150, 450, 300, 50, green_color, blue_green_color, 30, 3) # 4 player best4 = button("4 Players", mouse[0], mouse[1], (width_screen / 2) - 150, 550, 300, 50, green_color, blue_green_color, 30, 4) # Back button best5 = button("Back", mouse[0], mouse[1], 0, 650, 200, 50, red_color, blue_red_color, 30, 5) if best5 == 5: main_menu() if best1 == "s": playing(21) if best2 == 2: playing(2) if best3 == 3: playing(3) if best4 == 4: playing(4) pygame.display.update() def playing(best): best6 = -1 time = 3000 if best6 == 7: choice() game_layout_display.blit(post, (0, 0)) game_layout_display.blit(mother_board, (width_screen / 2 - 250, height_screen / 2 - 250)) xcr = xcy = xcg = xcb = 406 - 25 ycr = ycy = ycg = ycb = 606 - 25 game_layout_display.blit(red_c, (xcy, ycy)) if 5 > best > 1 or best == 21: game_layout_display.blit(yellow_c, (xcy, ycy)) if 5 > best > 2 or best == 21: game_layout_display.blit(green_c, (xcg, ycg)) if 5 > best > 2: game_layout_display.blit(blue_c, (xcb, ycb)) gamer1 = "Player 1" gamer1score = 0 if best == 21: gamer2 = "Computer" gamer2score = 0 if 5 > best > 1: gamer2 = "Player 2" gamer2score = 0 if 5 > best > 2: gamer3 = "Player 3" gamer3score = 0 if 5 > best > 3: gamer4 = "Player 4" gamer4score = 0 tips = 1 play = True while True: less = False set = False time = 3000 game_layout_display.blit(post, (0, 0)) game_layout_display.blit(mother_board, (width_screen / 2 - 250, height_screen / 2 - 250)) mouse = pygame.mouse.get_pos() for event in pygame.event.get(): if event.type == pygame.QUIT: Quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: Quit() if best == 21: if button1("Player 1", mouse[0], mouse[1], 100, 700, 200, 50, red_color, grey_color, 30): if tips == 1: gamer1score, less, set, six = turn(gamer1score, less, set) if not six: tips += 1 xcr, ycr = movement(gamer1score) if gamer1score == 100: time = pygame.time.get_ticks() while pygame.time.get_ticks() - time < 2000: message_display1_screen("Player 1 Wins", 650, 50, 50, blue_color) pygame.mixer.Sound.play(win) pygame.display.update() break button1("Computer", mouse[0], mouse[1], 400, 700, 200, 50, yellow_color, grey_color, 30) if True: if tips == 2: gamer2score, less, set, six = turn(gamer2score, less, set) xcy, ycy = movement(gamer2score) if not six: tips += 1 if best < 3 or best == 21: tips = 1 if gamer2score == 100: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2000: message_display1_screen("Computer Wins", 650, 50, 50, black_color) pygame.mixer.Sound.play(lose) pygame.display.update() break if 5 > best > 1: if button1("Player 1", mouse[0], mouse[1], 100, 700, 200, 50, red_color, grey_color, 30): if tips == 1: gamer1score, less, set, six = turn(gamer1score, less, set) xcr, ycr = movement(gamer1score) if not six: tips += 1 if gamer1score == 100: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2000: message_display1_screen("Player 1 Wins", 650, 50, 50, black_color) pygame.mixer.Sound.play(win) pygame.display.update() break if button1("Player 2", mouse[0], mouse[1], 400, 700, 200, 50, yellow_color, grey_color, 30): if tips == 2: gamer2score, less, set, six = turn(gamer2score, less, set) xcy, ycy = movement(gamer2score) if not six: tips += 1 if best < 3: tips = 1 if gamer2score == 100: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2000: message_display1_screen("Player 2 Wins", 650, 50, 50, black_color) pygame.mixer.Sound.play(win) pygame.display.update() break if 5 > best > 2: if button1("Player 3", mouse[0], mouse[1], 700, 700, 200, 50, green_color, grey_color, 30): if tips == 3: gamer3score, less, set, six = turn(gamer3score, less, set) xcg, ycg = movement(gamer3score) if not six: tips += 1 if best < 4: tips = 1 if gamer3score == 100: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2000: message_display1_screen("Player 3 Wins", 650, 50, 50, black_color) pygame.mixer.Sound.play(win) pygame.display.update() break if 5 > best > 3: if button1("Player 4", mouse[0], mouse[1], 1000, 700, 200, 50, blue_color, grey_color, 30): if tips == 4: gamer4score, less, set, six = turn(gamer4score, less, set) xcb, ycb = movement(gamer4score) if not six: tips += 1 if best < 5: tips = 1 if gamer4score == 100: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2000: message_display1_screen("Player 4 Wins", 650, 50, 50, black_color) pygame.mixer.Sound.play(win) pygame.display.update() break best6 = button("Back", mouse[0], mouse[1], 0, 0, 200, 50, red_color, blue_red_color, 30, 7) game_layout_display.blit(red_c, (xcr, ycr)) if 5 > best > 1 or best == 21: game_layout_display.blit(yellow_c, (xcy + 2, ycy)) if 5 > best > 2: game_layout_display.blit(green_c, (xcg + 4, ycg)) if 5 > best > 3: game_layout_display.blit(blue_c, (xcb + 6, ycb)) if less: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2000: message_display1_screen("There's a Ladder!", 650, 50, 35, black_color) pygame.display.update() if set: time_clock = pygame.time.get_ticks() while pygame.time.get_ticks() - time_clock < 2000: message_display1_screen("There's a Snake!", 650, 50, 35, black_color) pygame.display.update() time_clocks.tick(7) pygame.display.update() introduction() main_menu() |
Run Quick Virus Scan for secure Download
Run Quick Scan for safe DownloadDownloadable Source Code
I have here the list of Best Python Project with Source code free to download for free, I hope this can help you a lot.
Summary
That’s how you create Snakes and Ladders Game in Python in your projects. You can always expand and try different ways in implementing the Snakes and Ladders Game in your Python projects. Snakes and Ladders Game in Python is free to download the open source code and it is use for educational purposes only..
Related Articles
- Aircraft War Game in Python with Source Code
- Mario Game In Python With Source Code
- Hangman Game In Python With Source Code
- Stickman Game in Python with Source Code
- Snake Game In Python Code
- How to Make Bouncing Ball Game in Python with Source Code
- How to Create Rock-Paper-Scissor Game in Python
- Tetris In Python Code
- Tank Game Python with Source Code
Inquiries
If you have any questions or suggestions about Snakes and Ladders Game in Python with Source Code , please feel free to leave a comment below.