-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
229 lines (193 loc) · 7.62 KB
/
game.py
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
import pygame
import sys
import subprocess
# Initialize Pygame
pygame.init()
# Set up screen dimensions
screen_width = 1024
screen_height = 768
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Celestial Oddyssey - Main Menu')
# Load background image
menu_bg = pygame.image.load('assets/menu_bg.png')
logo = pygame.image.load('assets/logo.png')
bg_x = 0
bg_speed = 0.25
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
LIGHT_GRAY = (170, 170, 170)
# Define fonts
font = pygame.font.Font(None, 36)
# Button class
class Button:
def __init__(self, text, pos, callback):
self.text = text
self.pos = pos
self.callback = callback
self.rect = pygame.Rect(pos[0], pos[1], 300, 50)
self.highlighted = False
def draw(self, screen):
color = LIGHT_GRAY if self.highlighted else GRAY
pygame.draw.rect(screen, color, self.rect)
text_surface = font.render(self.text, True, BLACK)
text_rect = text_surface.get_rect(center=self.rect.center)
screen.blit(text_surface, text_rect)
def check_hover(self, mouse_pos):
self.highlighted = self.rect.collidepoint(mouse_pos)
def click(self):
if self.highlighted:
self.callback()
# Input box class
class InputBox:
def __init__(self, x, y, w, h, max_length=21, text='', input_type='text'):
self.rect = pygame.Rect(x, y, w, h)
self.color = WHITE
self.text = text
self.txt_surface = font.render(text, True, WHITE)
self.active = False
self.max_length = max_length
self.input_type = input_type
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.active = not self.active
else:
self.active = False
self.color = LIGHT_GRAY if self.active else WHITE
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_RETURN:
print(self.text)
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
elif len(self.text) < self.max_length:
if self.input_type == 'number' and event.unicode.isdigit():
self.text += event.unicode
elif self.input_type == 'text':
self.text += event.unicode
self.txt_surface = font.render(self.text, True, WHITE)
def update(self):
width = max(300, self.txt_surface.get_width())
self.rect.w = width
def draw(self, screen):
screen.blit(self.txt_surface, (self.rect.x + 5, self.rect.y + 5))
pygame.draw.rect(screen, self.color, self.rect, 2)
def draw_text(screen, text, pos):
text_surface = font.render(text, True, WHITE)
screen.blit(text_surface, pos)
def join_game():
global current_screen
current_screen = "join"
def new_server():
global current_screen
current_screen = "new_server"
def quit_game():
pygame.quit()
sys.exit()
def go_back():
global current_screen
current_screen = "menu"
def start_server(ip, port, nickname, player_limit):
subprocess.Popen(["python", "server.py", ip, str(port)])
start_client(ip, port, nickname)
def start_client(ip, port, nickname):
subprocess.Popen(["python", "client.py", ip, str(port), nickname])
# Create buttons
buttons = [
Button("Join Game", (362, 300), join_game),
Button("New Server", (362, 400), new_server),
Button("Quit", (362, 500), quit_game)
]
# Create join and start server buttons
join_button = Button("Join Server", (362, 435), lambda: start_client(ip_box_join.text, port_box_join.text, nickname_box_join.text))
start_server_button = Button("Start Server", (362, 480), lambda: start_server(ip_box_server.text, port_box_server.text, nickname_box_server.text, player_limit_box.text))
# Create back button
back_button = Button("Back", (362, 500), go_back)
back_button2 = Button("Back", (362, 550), go_back)
# Input boxes for join game
ip_box_join = InputBox(362, 250, 300, 32, 21, '127.0.0.1')
port_box_join = InputBox(362, 320, 300, 32, 5, '5555', 'number')
nickname_box_join = InputBox(362, 390, 300, 32, 12)
# Input boxes for new server
ip_box_server = InputBox(362, 220, 300, 32, 21, '127.0.0.1')
port_box_server = InputBox(362, 290, 300, 32, 5, '5555', 'number')
nickname_box_server = InputBox(362, 360, 300, 32, 12)
player_limit_box = InputBox(362, 430, 300, 32, 5, '10', 'number')
# Main loop
running = True
current_screen = "menu"
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if current_screen == "menu":
for button in buttons:
button.check_hover(pygame.mouse.get_pos())
if event.type == pygame.MOUSEBUTTONDOWN and button.highlighted:
button.click()
elif current_screen == "join":
ip_box_join.handle_event(event)
port_box_join.handle_event(event)
nickname_box_join.handle_event(event)
back_button.check_hover(pygame.mouse.get_pos())
join_button.check_hover(pygame.mouse.get_pos())
if event.type == pygame.MOUSEBUTTONDOWN:
if back_button.highlighted:
back_button.click()
if join_button.highlighted:
start_client(ip_box_join.text, port_box_join.text, nickname_box_join.text)
elif current_screen == "new_server":
ip_box_server.handle_event(event)
port_box_server.handle_event(event)
nickname_box_server.handle_event(event)
player_limit_box.handle_event(event)
back_button2.check_hover(pygame.mouse.get_pos())
start_server_button.check_hover(pygame.mouse.get_pos())
if event.type == pygame.MOUSEBUTTONDOWN:
if back_button2.highlighted:
back_button2.click()
if start_server_button.highlighted:
start_server(ip_box_server.text, port_box_server.text, nickname_box_server.text, player_limit_box.text)
# Move background
bg_x -= bg_speed
if bg_x <= -menu_bg.get_width():
bg_x = 0
# Draw background
screen.blit(menu_bg, (bg_x, 0))
screen.blit(menu_bg, (bg_x + menu_bg.get_width(), 0))
if current_screen == "menu":
screen.blit(logo, (176, 150))
for button in buttons:
button.draw(screen)
elif current_screen == "join":
draw_text(screen, "IP Address:", (362, 220))
ip_box_join.update()
ip_box_join.draw(screen)
draw_text(screen, "Port:", (362, 290))
port_box_join.update()
port_box_join.draw(screen)
draw_text(screen, "Nickname:", (362, 360))
nickname_box_join.update()
nickname_box_join.draw(screen)
join_button.draw(screen)
back_button.draw(screen)
elif current_screen == "new_server":
draw_text(screen, "IP Address:", (362, 190))
ip_box_server.update()
ip_box_server.draw(screen)
draw_text(screen, "Port:", (362, 260))
port_box_server.update()
port_box_server.draw(screen)
draw_text(screen, "Nickname:", (362, 330))
nickname_box_server.update()
nickname_box_server.draw(screen)
draw_text(screen, "Player Limit:", (362, 400))
player_limit_box.update()
player_limit_box.draw(screen)
start_server_button.draw(screen)
back_button2.draw(screen)
pygame.display.flip()
pygame.time.Clock().tick(60)
pygame.quit()