-1

I am trying to add a menu on my pong game but when I try to load the program the window doesn't show up and it doesn't see any error so I don't kwon where the problem is. Here is my full code if something is missing so it can be seen. The game is working properly but when added a menu it stopped working as a general.

import sys
import pygame
pygame.init()
pygame.font.get_fonts()
mainClock = pygame.time.Clock()
# menu
def draw_text(text, font, color, surface, x, y):
    textobj = font.render(text, 1, color)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)
click = False
def main_menu():
    while True:
        win.fill((0, 0, 0))
        draw_text('main menu', ('arial'), (255, 255, 255), win, 20, 20)
        mx, my = pygame.mouse.get_pos()
        button_1 = pygame.Rect(50, 100, 200, 50)
        button_2 = pygame.Rect(50, 200, 200, 50)
        if button_1.collidepoint((mx, my)):
            if click:
                game()
        if button_2.collidepoint((mx, my)):
            if click:
                options()
        pygame.draw.rect(win, (255, 0, 0), button_1)
        pygame.draw.rect(win, (255, 0, 0), button_2)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    click = True
        pygame.display.update()
def options():
    running = True
    while running:
        win.fill((0, 0, 0))

        draw_text('options', "arial", (255, 255, 255), win, 20, 20)
# color
white = (255, 255, 255)
black = (0, 0, 0)
# screen
win = pygame.display.set_mode((750, 500))
pygame.display.set_caption('PONG')
icon = pygame.image.load("lig.png")
pygame.display.set_icon(icon)
# players
class Paddle1(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([10, 75])
        self.image.fill(white)
        self.rect = self.image.get_rect()
        self.points = 0
class Paddle2(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([10, 75])
        self.image.fill(white)
        self.rect = self.image.get_rect()
        self.points = 0
paddle1 = Paddle1()
paddle1.rect.x = 25
paddle1.rect.y = 225
paddle2 = Paddle2()
paddle2.rect.x = 715
paddle2.rect.y = 225
running = True
paddle_speed = 7
# ball
class Ball(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([10, 10])
        self.image.fill(white)
        self.rect = self.image.get_rect()
        self.speed = 6
        self.dx = 2
        self.dy = 2
pong = Ball()
pong.rect.x = 375
pong.rect.y = 250
all_sprites = pygame.sprite.Group()
all_sprites.add(paddle1, paddle2, pong)
def redraw():
    win.fill(black)
    font = pygame.font.SysFont('Arial', 30)
    text = font.render('PONG', False, white)
    textRect = text.get_rect()
    textRect.center = (750 // 2, 25)
    win.blit(text, textRect)
    p1_score = font.render(str(paddle1.points), False, white)
    p1Rect = p1_score.get_rect()
    p1Rect.center = (50, 50)
    win.blit(p1_score, p1Rect)
    p2_score = font.render(str(paddle2.points), False, white)
    p2Rect = p2_score.get_rect()
    p2Rect.center = (700, 50)
    win.blit(p2_score, p2Rect)
        all_sprites.draw(win)
    pygame.display.update()
def game():
    while running:
        pygame.time.delay(30)
        win.fill((0, 0, 0))
        for event in pygame.event.get():
            key = pygame.key.get_pressed()
        if key[pygame.K_w]:
            paddle1.rect.y += -paddle_speed
        if key[pygame.K_s]:
            paddle1.rect.y += paddle_speed
        if key[pygame.K_i]:
            paddle2.rect.y += -paddle_speed
        if key[pygame.K_k]:
            paddle2.rect.y += paddle_speed
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            pong.rect.x += pong.speed * pong.dx
        pong.rect.y += pong.speed * pong.dy
        if pong.rect.y > 490:
            pong.dy = -1
        if pong.rect.y < 1:
            pong.dy = 1
        if pong.rect.x > 740:
            pong.rect.x, pong.rect.y = 375, 250
            pong.dx = -1
            paddle1.points += 1
        if pong.rect.x < 1:
            pong.rect.x, pong.rect.y = 375, 250
            pong.dx = 1
            paddle2.points += 1
            if paddle1.rect.colliderect(pong.rect):
            pong.dx = 1
        if paddle2.rect.colliderect(pong.rect):
            pong.dx = -1
            if paddle1.rect.y <= 0:
            paddle1.rect.y = 0
            if paddle1.rect.y >= 425:
            paddle1.rect.y = 425
        if paddle2.rect.y <= 0:
            paddle2.rect.y = 0
        if paddle2.rect.y >= 425:
            paddle2.rect.y = 425
        redraw()
        pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    you never call `game` – Matiiss Dec 30 '21 at 22:56
  • 1
    Or `main_menu` for that matter. – Paul M. Dec 30 '21 at 23:01
  • `options` contains an infinite loop. – Rabbid76 Dec 30 '21 at 23:01
  • nothing helped. – joybro12 Dec 30 '21 at 23:16
  • [edit] your question and update the code with what you have tried based on our suggestions, also for future (and now) reference, you should provide a [mre] not your entire code – Matiiss Dec 30 '21 at 23:18
  • so i add the meenu() at the end of the def optinosa nd meenu is how i changed the name of main_menu but i keep getting this errors now :line 82, in meenu(), line 36, in meenu draw_text('main menu', ('arial'), (255, 255, 255), win, 20, 20) and this line 23, in draw_text textobj = font.render(text, 1, color) AttributeError: 'str' object has no attribute 'render – joybro12 Dec 30 '21 at 23:27
  • you need to call some function outside of any functions, otherwise all it does when running this code is just read over the lines and that is it, about that error: apparently `font` is a string, also you should provide a [mre] – Matiiss Dec 30 '21 at 23:30
  • so I put all of my code except def options ,def meenu and def drew_ text in def gemu but now i have the same problem as in the beginning I am now getting any screen and I don't get any errors so I don't now where is the problem I can't put any code because I don't know which part to put in this comment. – joybro12 Dec 31 '21 at 11:33
  • you don't need to provide a part of your code but a [mre], also did you call that function? have you called any functions outside of other functions? at the end of the file, you need to call some function, for example after defining `game` call `gemu`: `gemu()` – Matiiss Dec 31 '21 at 11:53
  • putted gemu() at the end of def gemu: and nothing changed and I called a function outside of other function – joybro12 Dec 31 '21 at 12:07

0 Answers0