D
D
Denis Kulikov2021-11-18 19:43:56
Python
Denis Kulikov, 2021-11-18 19:43:56

The code throws an error, what's wrong?

Here is the code

import pygame
import random
from os import path
import pygame_gui 
from win32api import GetSystemMetrics
from menu import menu, check_menu

FPS = 60

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WIDTH = GetSystemMetrics(0)
HEIGHT = GetSystemMetrics(1)	


check_menu = check_menu()	
manager = pygame_gui.UIManager((WIDTH, HEIGHT))
all_sprites = pygame.sprite.Group()
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((0, 0), pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.FULLSCREEN)
clock = pygame.time.Clock()
game_over = True

running = True
while running:
  time_delta = clock.tick(60)/1000.0
  #Меню
  if game_over:
    menu()
  if not check_menu:
    running = False
  clock.tick(FPS)	
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      running = False

    manager.process_events(event)
  manager.update(time_delta)

  #Рендеринг
  screen.fill(BLACK)
  manager.draw_ui(screen)
  pygame.display.flip()

pygame.quit()

and so
import pygame_gui
import pygame
from os import path
from win32api import GetSystemMetrics
from PIL import Image


BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
path_dir = path.join(path.dirname(__file__))
img_dir = path.join(path.dirname(__file__), 'img')
snd_dir = path.join(path.dirname(__file__), 'snd')
WIDTH = GetSystemMetrics(0)
HEIGHT = GetSystemMetrics(1)
size = (WIDTH, HEIGHT)
game_over = True
screen = pygame.display.set_mode((0, 0), pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.FULLSCREEN)
all_sprites = pygame.sprite.Group()


def menu():
  global game_over, running
  im = Image.open('C:/Users/Людмила/Desktop/Программирование/RPG/img/background_menu.png')
  out = im.resize(size)
  out.save('background_menu2.png')
  
  background = pygame.image.load(path.join(path_dir, 'background_menu2.png')).convert()
  
  background_rect = background.get_rect()
  screen.blit(background, (0, 0))
  all_sprites.draw(screen)
  pygame.display.flip()
  while game_over:
    for event in pygame.event.get():
      if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
        game_over = False
        
def check_menu():
  if not game_over:
    return False

At startup, everything is as it should be - an image is displayed, I press esc, the game closes, and the interpreter says
Warning (from warnings module):
  File "C:\Users\Людмила\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygame_gui\core\ui_font_dictionary.py", line 116
    warnings.warn(str(error))
UserWarning: Unable to load resource with path: C:\Users\Людмила\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygame_gui/data/FiraCode-Regular.ttf

I rummaged through a bunch of sites that offer to install this font, but I went into this directory and saw that it was installed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-11-18
@Artin24

This is a simple warning and is unlikely to cause the script to close.
Let's take it in order.
Run the main file, the code reaches these lines.

game_over = True

running = True
while running:
  time_delta = clock.tick(60)/1000.0
  #Меню
  if game_over:
    menu()

Since game_over == True, the function is immediately executed menu. The function menuin an infinite loop waits for the escape key, after which its execution ends and the stream returns to the main file.
running = True
while running:
  time_delta = clock.tick(60)/1000.0
  #Меню
  if game_over:
    menu()
  if not check_menu:
    running = False

check_menuwill be equal to None. The condition not Nonegives us Trueand we assign . Then some code of no interest to us, the cycle reaches the end and does not start again, since it is already equal to . So we exit the loop and execute . The end, the code worked exactly as written. As a rule, I don’t do this, but here I can only advise you to learn something about Python and PyGame. Because, even with some knowledge, reading such code is not the easiest thing to do. I'm afraid to imagine how it looks to an unknowing person. You yourself are confused in your constructions, function names, variables, the application logic itself running = FalsewhilerunnigFalsepygame.quit()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question