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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -39
app.py CHANGED
@@ -1,8 +1,5 @@
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."""
@@ -20,45 +17,32 @@ def update_player_position(player_pos, dice, board):
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,7 +65,7 @@ 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"):
 
1
  import random
2
  import streamlit as st
 
 
 
3
 
4
  def roll_dice():
5
  """Simulates rolling a six-sided dice."""
 
17
  st.write("Oops! You encountered a snake")
18
  return player_pos
19
 
20
+ def draw_board(player_position):
21
+ """Draws the game board using Streamlit."""
22
+ board_size = 10
23
+ board_squares = []
24
+ for i in range(100):
25
+ row = i // board_size
26
+ col = i % board_size
27
+ square_number = i + 1
28
 
29
+ #Adjust the number according to the row odd or even
30
+ if row % 2 == 1:
31
+ square_number = (row * board_size) + (board_size - col)
32
+ else:
33
+ square_number = (row * board_size) + col + 1
34
 
35
+ square_style = "background-color: lightgray; padding: 10px; border: 1px solid black; text-align: center;"
36
+ if square_number == player_position:
37
+ square_style = "background-color: red; padding: 10px; border: 1px solid black; text-align: center; color:white; font-weight: bold;"
38
 
39
+ board_squares.append(f"<div style='{square_style}'>{square_number}</div>")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
+ # create a layout of 10 rows each with 10 columns
42
+ for row in range(board_size):
43
+ cols = st.columns(board_size)
44
+ for col in range(board_size):
45
+ cols[col].markdown(board_squares[(row * board_size) + col], unsafe_allow_html=True)
46
 
47
 
48
  def snake_and_ladder():
 
65
 
66
  player_pos = st.session_state.player_pos
67
 
68
+ draw_board(player_pos)
69
 
70
  if player_pos < 100:
71
  if st.button("Roll Dice"):