ZeeAI1 commited on
Commit
08c02dd
·
verified ·
1 Parent(s): bcc8dea

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -0
app.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image, ImageDraw
3
+
4
+ # Streamlit setup
5
+ st.set_page_config(page_title="Breakout Game", layout="centered")
6
+ st.title("🎮 Breakout Game - Hugging Face Version")
7
+ st.write("This is a simplified version of the Breakout game using Streamlit.")
8
+
9
+ # Initialize game parameters
10
+ WIDTH, HEIGHT = 800, 600
11
+ PADDLE_WIDTH, PADDLE_HEIGHT = 100, 15
12
+ BALL_RADIUS = 10
13
+ BRICK_ROWS, BRICK_COLS = 5, 8
14
+ BRICK_WIDTH = WIDTH // BRICK_COLS
15
+ BRICK_HEIGHT = 30
16
+
17
+ # Session state for game variables
18
+ if "paddle_x" not in st.session_state:
19
+ st.session_state.paddle_x = WIDTH // 2 - PADDLE_WIDTH // 2
20
+ if "ball_pos" not in st.session_state:
21
+ st.session_state.ball_pos = [WIDTH // 2, HEIGHT // 2]
22
+ if "ball_speed" not in st.session_state:
23
+ st.session_state.ball_speed = [4, -4]
24
+ if "bricks" not in st.session_state:
25
+ st.session_state.bricks = [
26
+ [col * BRICK_WIDTH, row * BRICK_HEIGHT]
27
+ for row in range(BRICK_ROWS)
28
+ for col in range(BRICK_COLS)
29
+ ]
30
+ if "score" not in st.session_state:
31
+ st.session_state.score = 0
32
+ if "lives" not in st.session_state:
33
+ st.session_state.lives = 3
34
+
35
+
36
+ # Draw the game screen
37
+ def draw_game():
38
+ # Create a blank canvas
39
+ img = Image.new("RGB", (WIDTH, HEIGHT), "black")
40
+ draw = ImageDraw.Draw(img)
41
+
42
+ # Draw paddle
43
+ paddle_y = HEIGHT - 40
44
+ draw.rectangle(
45
+ [
46
+ (st.session_state.paddle_x, paddle_y),
47
+ (st.session_state.paddle_x + PADDLE_WIDTH, paddle_y + PADDLE_HEIGHT),
48
+ ],
49
+ fill="white",
50
+ )
51
+
52
+ # Draw ball
53
+ ball_x, ball_y = st.session_state.ball_pos
54
+ draw.ellipse(
55
+ [
56
+ (ball_x - BALL_RADIUS, ball_y - BALL_RADIUS),
57
+ (ball_x + BALL_RADIUS, ball_y + BALL_RADIUS),
58
+ ],
59
+ fill="red",
60
+ )
61
+
62
+ # Draw bricks
63
+ for brick in st.session_state.bricks:
64
+ brick_x, brick_y = brick
65
+ draw.rectangle(
66
+ [(brick_x, brick_y), (brick_x + BRICK_WIDTH - 2, brick_y + BRICK_HEIGHT - 2)],
67
+ fill="blue",
68
+ )
69
+
70
+ return img
71
+
72
+
73
+ # Update game state
74
+ def update_game(action):
75
+ # Move paddle
76
+ if action == "left" and st.session_state.paddle_x > 0:
77
+ st.session_state.paddle_x -= 20
78
+ if action == "right" and st.session_state.paddle_x < WIDTH - PADDLE_WIDTH:
79
+ st.session_state.paddle_x += 20
80
+
81
+ # Move ball
82
+ ball_x, ball_y = st.session_state.ball_pos
83
+ speed_x, speed_y = st.session_state.ball_speed
84
+ ball_x += speed_x
85
+ ball_y += speed_y
86
+
87
+ # Ball collision with walls
88
+ if ball_x - BALL_RADIUS <= 0 or ball_x + BALL_RADIUS >= WIDTH:
89
+ speed_x = -speed_x
90
+ if ball_y - BALL_RADIUS <= 0:
91
+ speed_y = -speed_y
92
+
93
+ # Ball collision with paddle
94
+ paddle_y = HEIGHT - 40
95
+ if (
96
+ paddle_y <= ball_y + BALL_RADIUS <= paddle_y + PADDLE_HEIGHT
97
+ and st.session_state.paddle_x
98
+ <= ball_x
99
+ <= st.session_state.paddle_x + PADDLE_WIDTH
100
+ ):
101
+ speed_y = -speed_y
102
+
103
+ # Ball collision with bricks
104
+ new_bricks = []
105
+ for brick in st.session_state.bricks:
106
+ brick_x, brick_y = brick
107
+ if not (
108
+ brick_x <= ball_x <= brick_x + BRICK_WIDTH
109
+ and brick_y <= ball_y <= brick_y + BRICK_HEIGHT
110
+ ):
111
+ new_bricks.append(brick)
112
+ else:
113
+ speed_y = -speed_y
114
+ st.session_state.score += 10
115
+
116
+ st.session_state.bricks = new_bricks
117
+
118
+ # Ball out of bounds
119
+ if ball_y + BALL_RADIUS >= HEIGHT:
120
+ st.session_state.lives -= 1
121
+ ball_x, ball_y = WIDTH // 2, HEIGHT // 2
122
+ speed_x, speed_y = 4, -4
123
+
124
+ # Update ball position and speed
125
+ st.session_state.ball_pos = [ball_x, ball_y]
126
+ st.session_state.ball_speed = [speed_x, speed_y]
127
+
128
+
129
+ # Display the game
130
+ st.image(draw_game(), use_column_width=True)
131
+
132
+ # Display score and lives
133
+ st.write(f"**Score**: {st.session_state.score}")
134
+ st.write(f"**Lives**: {st.session_state.lives}")
135
+
136
+ # Game over
137
+ if st.session_state.lives <= 0:
138
+ st.write("**Game Over! Refresh the page to restart.**")
139
+ else:
140
+ # Controls
141
+ col1, col2, col3 = st.columns(3)
142
+ with col1:
143
+ if st.button("⬅️ Move Left"):
144
+ update_game("left")
145
+ with col2:
146
+ if st.button("🔄 Stay"):
147
+ update_game(None)
148
+ with col3:
149
+ if st.button("➡️ Move Right"):
150
+ update_game("right")