Vishwas1 commited on
Commit
d6abb83
·
verified ·
1 Parent(s): b7e93cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -22
app.py CHANGED
@@ -1,5 +1,8 @@
1
  import random
2
  import streamlit as st
 
 
 
3
 
4
  def roll_dice():
5
  """Simulates rolling a six-sided dice."""
@@ -7,19 +10,55 @@ def roll_dice():
7
 
8
  def update_player_position(player_pos, dice, board):
9
  """Updates player position based on the dice roll and board."""
 
10
  player_pos += dice
11
  if player_pos in board:
12
  player_pos = board[player_pos]
13
- if player_pos > dice:
14
  st.write("Yay! You climbed a ladder.")
15
- else:
16
- st.write("Oops! You encountered a snake")
17
  return player_pos
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- def display_game_board():
21
- board_image = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8f/Snakes_and_ladders_board_simple.svg/1920px-Snakes_and_ladders_board_simple.svg.png"
22
- st.image(board_image, caption="Snake and Ladder Board", width=500)
23
 
24
 
25
  def snake_and_ladder():
@@ -42,29 +81,26 @@ def snake_and_ladder():
42
 
43
  player_pos = st.session_state.player_pos
44
 
45
- display_game_board()
46
 
47
  if player_pos < 100:
48
- if st.button("Roll Dice"):
49
- dice = roll_dice()
50
- st.write(f"You rolled a {dice}")
51
 
52
- player_pos = update_player_position(player_pos, dice, board)
53
 
54
- if player_pos > 100:
55
- player_pos -= dice
56
- st.write("You need to get to 100 to win, Try Again!")
57
- else:
58
- st.write(f"Your position is {player_pos}")
59
 
60
- st.session_state.player_pos = player_pos
 
61
  else:
62
  st.success("Congratulations! You won!")
63
  if st.button("Play Again"):
64
  st.session_state.player_pos = 1
65
  st.rerun()
66
-
67
-
68
-
69
- if __name__ == "__main__":
70
- snake_and_ladder()
 
1
  import random
2
  import streamlit as st
3
+ from PIL import Image, ImageDraw
4
+ import requests
5
+ from io import BytesIO
6
 
7
  def roll_dice():
8
  """Simulates rolling a six-sided dice."""
 
10
 
11
  def update_player_position(player_pos, dice, board):
12
  """Updates player position based on the dice roll and board."""
13
+ original_pos = player_pos
14
  player_pos += dice
15
  if player_pos in board:
16
  player_pos = board[player_pos]
17
+ if player_pos > original_pos:
18
  st.write("Yay! You climbed a ladder.")
19
+ elif player_pos < original_pos:
20
+ st.write("Oops! You encountered a snake")
21
  return player_pos
22
 
23
+ def calculate_player_coordinates(position):
24
+ """Calculates approximate x, y coordinates for the player position."""
25
+ row = (position - 1) // 10
26
+ col = (position - 1) % 10
27
+
28
+ # Adjust for snake and ladder board layout
29
+ if row % 2 == 1:
30
+ col = 9 - col
31
+
32
+ x = 50 + col * 50 # Adjust these values as necessary
33
+ y = 450 - row * 50 # Adjust these values as necessary
34
+
35
+ return x, y
36
+
37
+
38
+ def display_game_board(player_position):
39
+ board_image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8f/Snakes_and_ladders_board_simple.svg/1920px-Snakes_and_ladders_board_simple.svg.png"
40
+
41
+ try:
42
+ response = requests.get(board_image_url, stream=True)
43
+ response.raise_for_status()
44
+ board_image = Image.open(BytesIO(response.content)).convert("RGBA")
45
+
46
+ # Calculate player position coordinates
47
+ player_x, player_y = calculate_player_coordinates(player_position)
48
+
49
+ # Create a drawing context
50
+ draw = ImageDraw.Draw(board_image)
51
+
52
+ # Draw a circle to represent the player at their position
53
+ radius = 15
54
+ draw.ellipse((player_x - radius, player_y - radius, player_x + radius, player_y + radius), fill='red')
55
+
56
+ st.image(board_image, caption="Snake and Ladder Board", width=500)
57
+ except requests.exceptions.RequestException as e:
58
+ st.write(f"Could not load the game board image. Please check your internet connection or try again later. {e}")
59
+ except Exception as e:
60
+ st.write(f"An error occurred: {e}")
61
 
 
 
 
62
 
63
 
64
  def snake_and_ladder():
 
81
 
82
  player_pos = st.session_state.player_pos
83
 
84
+ display_game_board(player_pos)
85
 
86
  if player_pos < 100:
87
+ if st.button("Roll Dice"):
88
+ dice = roll_dice()
89
+ st.write(f"You rolled a {dice}")
90
 
91
+ player_pos = update_player_position(player_pos, dice, board)
92
 
93
+ if player_pos > 100:
94
+ player_pos -= dice
95
+ st.write("You need to get to 100 to win, Try Again!")
96
+ else:
97
+ st.write(f"Your position is {player_pos}")
98
 
99
+ st.session_state.player_pos = player_pos
100
+ st.rerun()
101
  else:
102
  st.success("Congratulations! You won!")
103
  if st.button("Play Again"):
104
  st.session_state.player_pos = 1
105
  st.rerun()
106
+ snake_and_ladder()