-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.py
More file actions
158 lines (130 loc) · 4.48 KB
/
menu.py
File metadata and controls
158 lines (130 loc) · 4.48 KB
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
# python
#
# Multiplayer Networking Game
#
import pygame
from pygame import *
import src.screen_settings as screen_settings
import client
from src.sprite import Sprite
from src.texture import load_texture
import src.menu_element as menu_element
from src.visual import Colors
import src.log as log
from src.app_interaction import DiscordRPC
from src.settings import Settings
import sys
pygame.init()
screen_settings.DisplayParams.center = (screen_settings.DisplayParams.size[0] / 2, screen_settings.DisplayParams.size[1] / 2)
screen = pygame.display.set_mode(screen_settings.DisplayParams.size)
pygame.display.set_caption(screen_settings.DisplayParams.titles.menu)
pygame.display.set_icon(screen_settings.DisplayParams.icon)
clock = pygame.time.Clock()
volume = 0.5
class Scene:
class Menu:
music = False
class Game:
areas = list([])
class Menu:
elements = list([])
def get_sprites(self):
sprites = list([])
for i in range(len(self.elements)):
sprites.append(self.elements[i].sprite)
return sprites
should_continue = True
should_update = True
singleplayer_server_thread = None
def join_singleplayer(self):
port = self.port
addr = "127.0.0.1"
if self.no_server == True:
port = None
addr = None
self.should_continue = client.begin_singleplayer(screen, port)
def join_multiplayer(self):
port = self.port
addr = self.address
if self.no_server == True:
port = None
addr = None
self.should_continue = client.begin_multiplayer(screen, addr, port)
no_server = False
port = 2048
address = "127.0.0.1"
def draw_menu(screen, sprites = False):
if sprites == False:
screen.fill(screen_settings.DisplayParams.fill_color)
else:
screen.fill(screen_settings.DisplayParams.fill_color)
screen_settings.draw_frame(screen, sprites)
display.flip()
Menu.should_update = False
def check_hover(elements):
mousepos = pygame.mouse.get_pos()
for i in range(len(elements)):
if elements[i].sprite.rect.collidepoint(mousepos):
try:
elements[i].hover()
Menu.should_update = True
except:
pass # doesn't matter if I do pass or continue
else:
try:
elements[i].unhover()
Menu.should_update = True
except:
pass
def check_click(elements):
mousepos = pygame.mouse.get_pos()
for i in range(len(elements)):
if elements[i].sprite.rect.collidepoint(mousepos):
try:
elements[i].click()
#Menu.should_update = True
except:
continue
# command line options
options = sys.argv[1:]
options_len = len(options)
options_it = iter(options)
for op in options_it:
match op:
case "":
pass
case "--no-server":
Menu.no_server = True
case "--port":
port = options_it.__next__()
if port and port != "":
port = int(port)
Menu.port = port
case "--address":
addr = options_it.__next__()
if addr and addr != "":
Menu.address = addr
def main():
#bg = Sprite.spriteobj_to_sprite(Sprite, load_spriteobj("menu_bg.png"))
#Menu.elements.append(bg)
log.log_begin()
play = menu_element.Button(Menu.join_singleplayer, Menu, "Singleplayer", Colors.white, 4, Vector2(screen_settings.DisplayParams.width / 2, screen_settings.DisplayParams.height / 2 - 360))
Menu.elements.append(play)
play2 = menu_element.Button(Menu.join_multiplayer, Menu, "Multiplayer", Colors.white, 4, Vector2(screen_settings.DisplayParams.width / 2, screen_settings.DisplayParams.height / 2 - 128))
Menu.elements.append(play2)
DiscordRPC.set(DiscordRPC, "In Menu", "Sitting in Main Menu")
while Menu.should_continue is True:
dtime = clock.tick(Settings.fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
log.log("Quitting Menu")
Menu.should_continue = False
elif event.type == pygame.MOUSEBUTTONDOWN:
check_click(Menu.elements)
check_hover(Menu.elements)
if Menu.should_update is True: draw_menu(screen, Menu.get_sprites(Menu))
pygame.quit()
DiscordRPC.stop(DiscordRPC)
log.log("Menu Stopped")
log.log_end()
main()