简体   繁体   中英

Pygame: Can't move player when main menu function added

I'm still working on a game in Pygame. I've got the gameplay to work, I just want to make a main menu function so when a button is pressed, the game starts.

When I added in the main menu, and edited the game loop, I was unable to move the player sprite, and the game wouldn't close when I press the "X out" button (it goes irresponsive(this didn't happen before)).

This didn't happen originally.

And there's no traceback.

Here's my code:

import pygame
import time
import itertools
import os

pygame.init()
SCREENWIDTH = 1000
SCREENHEIGHT = 650
screen = pygame.display.set_mode([SCREENWIDTH, SCREENHEIGHT])
screen.fill((255, 123, 67))
screen.blit(pygame.image.load("Backgrounds/mainmenu.png"), (0, 0))

background = screen.copy()
clock = pygame.time.Clock()
mi_level = False

class Player(pygame.sprite.Sprite):
    sprite = pygame.image.load("Sprites/lee.png")

    def __init__(self, *groups):
        super().__init__(*groups)
        self.image = Player.sprite
        self.rect = self.image.get_rect(topleft=(445, 550))
        self.pos = pygame.Vector2(self.rect.center)
        self.lives = 50
        self.score = 0
        self.hitbox = self.rect.inflate(-20, -30)
    def update(self):
        self.pos = self.rect.center
        key = pygame.key.get_pressed()
        dist = 3
        if key[pygame.K_DOWN]:
            self.rect.y += dist
            self.hitbox.y += dist
        elif key[pygame.K_UP]:
            self.rect.y -= dist
            self.hitbox.y -= dist
        if key[pygame.K_RIGHT]:
            self.rect.x += dist
            self.hitbox.x += dist
        elif key[pygame.K_LEFT]:
            self.rect.x -= dist
            self.hitbox.x -= dist

        if self.rect.right > SCREENWIDTH:
            self.rect.right = SCREENWIDTH
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.bottom > SCREENHEIGHT:
            self.rect.bottom = SCREENHEIGHT
        if self.rect.top < 50:
            self.rect.top = 50

        self.hitbox.center = self.pos
        if self.lives <= 0:
            screen.blit(pygame.image.load("gameover.png"), (0,0))
            pygame.display.flip()
            time.sleep(5)
            os._exit(0)
            pygame.quit()
            stageon = False
sprites = pygame.sprite.Group()
player = Player(sprites)

def main():
    running = True
    mi_level = False
    screen.blit(pygame.image.load("Backgrounds/mainmenu.png"), (0, 0))
    pygame.display.update()
    while True:
        for events in pygame.event.get():
            if events.type == pygame.QUIT or running == False:
                time.sleep(1)
                running = False
                pygame.quit()
                return
        pygame.draw.rect(screen, (255, 204, 204), (690, 110, 700, 135), 0)
        if pygame.mouse.get_pressed()[0] and pygame.Rect(690, 110, 880, 165).collidepoint(pygame.mouse.get_pos()):
            screen.fill((255, 123, 67))
            screen.blit(pygame.image.load("Backgrounds/mi_level.png"), (0, 50))
            pygame.display.flip
            background = screen.copy()
            mi_level = True
        pygame.display.update()            
        while mi_level:
            sprites.update()

            screen.blit(background, (0, 0))
            sprites.draw(screen)
            pygame.display.update()

            clock.tick(100)
            if mi_level == False:
                return
            if events.type == pygame.QUIT or running == False:
                time.sleep(1)
                running = False
                pygame.quit()
                return


if __name__ == '__main__':
    main()

I wonder what amateur mistake I've made this time...

Any help would be appreciated. Thanks :)

There is already a game loop. There is not any need of "nested" game loops:

while mi_level:

A simple if statement, which evaluates if the game is started is sufficient:

if mi_level:
    # []...]

Note, in your code in the inner loops, the events haven't been evaluated, because pygame.event.get() is missing. This causes that the pygame.QUIT event wasn't handled any more and the window didn't close and react as intended.

See the changes to the main loop:

def main():
    running = True
    mi_level = False
    screen.blit(pygame.image.load("Backgrounds/mainmenu.png"), (0, 0))
    pygame.display.update()
    while True:
        for events in pygame.event.get():
            if events.type == pygame.QUIT or running == False:
                time.sleep(1)
                running = False
                pygame.quit()
                return
        pygame.draw.rect(screen, (255, 204, 204), (690, 110, 700, 135), 0)
        if pygame.mouse.get_pressed()[0] and pygame.Rect(690, 110, 880, 165).collidepoint(pygame.mouse.get_pos()):
            screen.fill((255, 123, 67))
            screen.blit(pygame.image.load("Backgrounds/mi_level.png"), (0, 50))
            pygame.display.flip()
            background = screen.copy()
            mi_level = True

        if mi_level:
            sprites.update()
            screen.blit(background, (0, 0))
            sprites.draw(screen)
            pygame.display.update()

        clock.tick(100)
        pygame.display.update()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM