Anonymous
Starten Sie zufällig die Ebenen beim Drücken einer Taste [Duplikat]
Post
by Anonymous » 13 Apr 2025, 12:38
Ich möchte ein Spiel mit mehreren Levels erstellen, das in Zukunft mit Fragen und Aufgaben gefüllt sein wird. Im Moment im Code von Level 2 ist es "Anfang" und "Anfang2". Wie man sie zufällig in zufälliger Reihenfolge läuft.
Code: Select all
def beginnings():
if True:
f = [beginning(), beginning2()]
print(random.choice(f))
def beginning():
Button1 = ImageButton(380, 260, 250, 80, "", "image/Button/Начасть работа. Hovered.png",
"image/Button/Начасть работа. Normal (1).png")
Button2 = ImageButton(380, 365, 250, 80, "", "image/Button/Опрокат. Hovered.png",
"image/Button/Опрокат. Normal.png")
Button3 = ImageButton(380, 470, 250, 80, "", "image/Button/Вход. Hovered.png",
"image/Button/Вход. Normal.png")
running = True
while running:
screen.fill((0, 0, 0))
screen.blit(bg2, (0, -90))
Button1.check_hover(pygame.mouse.get_pos())
Button1.draw(screen)
Button2.check_hover(pygame.mouse.get_pos())
Button2.draw(screen)
Button3.check_hover(pygame.mouse.get_pos())
Button3.draw(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
Button1.handle_event(event)
Button2.handle_event(event)
Button3.handle_event(event)
if event.type == pygame.USEREVENT and event.button == Button1:
start()
if event.type == pygame.USEREVENT and event.button == Button2:
pass
if event.type == pygame.USEREVENT and event.button == Button3:
pygame.quit()
sys.exit()
pygame.display.flip()
def beginning2():
running = True
while running:
screen.fill((0, 0, 0))
screen.blit(bg2, (0, -90))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
pygame.display.flip()
< /code>
Bei der Implementierung jedoch: < /p>
if event.type == pygame.USEREVENT and event.button == ButtonB:
beginnings()
< /code>
Das gleiche Niveau wird immer geöffnet. /> Bitte sagen Sie mir, wie ich die Ebenen zufällig starten kann < /p>
import pygame
import sys
import pygame.time
import pygame.locals
from Button import ImageButton
pygame.init()
screen = pygame.display.set_mode((1280, 780))
bg = pygame.image.load("image/result_Фон11.png")
bg2 = pygame.image.load("image/result_Фон2.png")
bg3 = pygame.image.load("image/Презентация.png")
bg4 = pygame.image.load("image/result_Фон2 (размытый).png")
pygame.display.set_caption("Соучастие в преступлении")
icon = pygame.image.load("image/Icon/crowd-80.png")
pygame.display.set_icon(icon)
clock = pygame.time.Clock()
def main_menu():
def start():
image = pygame.image.load("image/Airbrush-ARDUWgv8Nwg-Photoroom.png")
imageD = pygame.image.load("image/Dio.png")
myfond = pygame.font.Font('Font/Lilita One Regular.ttf', 20)
text_surfaceS = myfond.render('Нужна твоя помощь!', True, 'Black')
myfond = pygame.font.Font('Font/Lilita One Regular.ttf', 20)
text_surfaceSo = myfond.render('Поступило сообщение', True, 'Black')
ButtonB = ImageButton(1180, 360, 100, 100, "", "image/Button/стрелка-вправо.png",
"image/Button/стрелка-вправо цвет.png")
running = True
while running:
screen.fill((0, 0, 0))
screen.blit(bg4, (0, -90))
screen.blit(image, (40, 270))
screen.blit(imageD, (450, 50))
screen.blit(text_surfaceS, (590, 80))
screen.blit(text_surfaceSo, (590, 100))
ButtonB.check_hover(pygame.mouse.get_pos())
ButtonB.draw(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
sys.exit()
ButtonB.handle_event(event)
if event.type == pygame.USEREVENT and event.button == ButtonB:
beginning()
pygame.display.flip()
def pr():
myfond = pygame.font.Font('Font/PassionsConflictRUS-Regular.otf', 50)
text_surfacePr = myfond.render('Проект направлен на формирование ...', True, 'Black')
running = True
while running:
screen.fill((0, 0, 0))
screen.blit(bg3, (0, -90))
screen.blit(text_surfacePr, (60, 60))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
pygame.display.update()
def beginning():
running = True
while running:
screen.fill((0, 0, 0))
screen.blit(bg2, (0, -90))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
pygame.display.flip()
def beginning2():
running = True
while running:
screen.fill((0, 0, 0))
screen.blit(bg2, (0, -90))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
pygame.display.flip()
if __name__ == "__main__":
main_menu()
1744540739
Anonymous
[url=viewtopic.php?t=14917]Ich möchte[/url] ein Spiel mit mehreren Levels erstellen, das in Zukunft mit Fragen und Aufgaben gefüllt sein wird. Im Moment im Code von Level 2 ist es "Anfang" und "Anfang2". Wie man sie zufällig in zufälliger Reihenfolge läuft.[code]def beginnings(): if True: f = [beginning(), beginning2()] print(random.choice(f)) def beginning(): Button1 = ImageButton(380, 260, 250, 80, "", "image/Button/Начасть работа. Hovered.png", "image/Button/Начасть работа. Normal (1).png") Button2 = ImageButton(380, 365, 250, 80, "", "image/Button/Опрокат. Hovered.png", "image/Button/Опрокат. Normal.png") Button3 = ImageButton(380, 470, 250, 80, "", "image/Button/Вход. Hovered.png", "image/Button/Вход. Normal.png") running = True while running: screen.fill((0, 0, 0)) screen.blit(bg2, (0, -90)) Button1.check_hover(pygame.mouse.get_pos()) Button1.draw(screen) Button2.check_hover(pygame.mouse.get_pos()) Button2.draw(screen) Button3.check_hover(pygame.mouse.get_pos()) Button3.draw(screen) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() sys.exit() Button1.handle_event(event) Button2.handle_event(event) Button3.handle_event(event) if event.type == pygame.USEREVENT and event.button == Button1: start() if event.type == pygame.USEREVENT and event.button == Button2: pass if event.type == pygame.USEREVENT and event.button == Button3: pygame.quit() sys.exit() pygame.display.flip() def beginning2(): running = True while running: screen.fill((0, 0, 0)) screen.blit(bg2, (0, -90)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() sys.exit() pygame.display.flip() < /code> Bei der Implementierung jedoch: < /p> if event.type == pygame.USEREVENT and event.button == ButtonB: beginnings() < /code> Das gleiche Niveau wird immer geöffnet. /> Bitte sagen Sie mir, wie ich die Ebenen zufällig starten kann < /p> import pygame import sys import pygame.time import pygame.locals from Button import ImageButton pygame.init() screen = pygame.display.set_mode((1280, 780)) bg = pygame.image.load("image/result_Фон11.png") bg2 = pygame.image.load("image/result_Фон2.png") bg3 = pygame.image.load("image/Презентация.png") bg4 = pygame.image.load("image/result_Фон2 (размытый).png") pygame.display.set_caption("Соучастие в преступлении") icon = pygame.image.load("image/Icon/crowd-80.png") pygame.display.set_icon(icon) clock = pygame.time.Clock() def main_menu(): def start(): image = pygame.image.load("image/Airbrush-ARDUWgv8Nwg-Photoroom.png") imageD = pygame.image.load("image/Dio.png") myfond = pygame.font.Font('Font/Lilita One Regular.ttf', 20) text_surfaceS = myfond.render('Нужна твоя помощь!', True, 'Black') myfond = pygame.font.Font('Font/Lilita One Regular.ttf', 20) text_surfaceSo = myfond.render('Поступило сообщение', True, 'Black') ButtonB = ImageButton(1180, 360, 100, 100, "", "image/Button/стрелка-вправо.png", "image/Button/стрелка-вправо цвет.png") running = True while running: screen.fill((0, 0, 0)) screen.blit(bg4, (0, -90)) screen.blit(image, (40, 270)) screen.blit(imageD, (450, 50)) screen.blit(text_surfaceS, (590, 80)) screen.blit(text_surfaceSo, (590, 100)) ButtonB.check_hover(pygame.mouse.get_pos()) ButtonB.draw(screen) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False pygame.quit() sys.exit() ButtonB.handle_event(event) if event.type == pygame.USEREVENT and event.button == ButtonB: beginning() pygame.display.flip() def pr(): myfond = pygame.font.Font('Font/PassionsConflictRUS-Regular.otf', 50) text_surfacePr = myfond.render('Проект направлен на формирование ...', True, 'Black') running = True while running: screen.fill((0, 0, 0)) screen.blit(bg3, (0, -90)) screen.blit(text_surfacePr, (60, 60)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() sys.exit() pygame.display.update() def beginning(): running = True while running: screen.fill((0, 0, 0)) screen.blit(bg2, (0, -90)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() sys.exit() pygame.display.flip() def beginning2(): running = True while running: screen.fill((0, 0, 0)) screen.blit(bg2, (0, -90)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() sys.exit() pygame.display.flip() if __name__ == "__main__": main_menu() [/code]