【问题标题】:how can i restart a game in pygame?如何在pygame中重新启动游戏?
【发布时间】:2021-07-12 00:32:15
【问题描述】:

我是 pygame 的新手,我尝试从头开始制作一个简单的乒乓球游戏,但我无法让游戏重新启动。我试图在一个while循环中做一个while循环。 当您松开它时,它会跳转到丢失循环并显示它应该显示的内容,并且一切正常,没有错误消息,但重新启动似乎不起作用。

import pygame
from pygame import rect
from pygame.constants import KEYDOWN, KEYUP
import random

def drawText(t, x, y):
    text = font.render(t, True, YELLOW, GREY)
    text_rectangle = text.get_rect()
    text_rectangle.topleft = (x,y)
    screen.blit(text, text_rectangle)


# constant Variables
WIDTH = 800
HEIGHT = 600
FPS = 60
GREY = (89, 88, 78)
YELLOW = (232, 219, 93)

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

icon = pygame.image.load("pong.png")

clock = pygame.time.Clock()

# Init
pygame.init()
# Create the Window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong")
pygame.display.set_icon(icon)
font = pygame.font.Font(pygame.font.get_default_font(), 24)

playerOneY = 200
playerTwoY = 200

ballY = HEIGHT / 2
ballX = WIDTH / 2

up1 = False
up2 = False
down1 = False
down2 = False
ballY = 300
ballX = 400
ballDx = 4
ballDy = 4

score = 0

lose = False
running = True
# game loop
while running:
    clock.tick(FPS)

    
    # QUIT
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == KEYDOWN:
            if event.key == pygame.K_w:
                up1 = True
            if event.key == pygame.K_s:
                down1 = True
            if event.key == pygame.K_UP:
                up2 = True
            if event.key == pygame.K_DOWN:
                down2 = True
            
        elif event.type == KEYUP:
            if event.key == pygame.K_w:
                up1 = False
            if event.key == pygame.K_s:
                down1 = False
            if event.key == pygame.K_UP:
                up2 = False
            if event.key == pygame.K_DOWN:
                down2 = False
        
    player1 = pygame.Rect(10, playerOneY, 20, 200)
    player2 = pygame.Rect(770, playerTwoY, 20, 200)
    ball = pygame.Rect(ballX, ballY, 20, 20)    
        

    

    # input
    # Ball Collition
    ballX += ballDx
    ballY += ballDy
    if (ball.colliderect(player1)):
        ballDx = abs(ballDx)
        score += 1
        print(score / 3)

    elif (ball.colliderect(player2)):
        ballDx = abs(ballDx) * -1
        score += 1
        print(score / 3)
    elif(ballY <= 0):
        ballDy = abs(ballDy)
    elif(ballY >= HEIGHT):
        ballDy = abs(ballDy) * -1
    elif(ballX >= WIDTH):
        running = False
        lose = True
    elif(ballX <= 0):
        running = False
        lose = True
    
        

    
        
    # Player Movement
    if up1 == True:
        playerOneY -= 5
    if up2 == True:
        playerTwoY -= 5
    
    if down1 == True:
        playerOneY += 5
    if down2 == True:
        playerTwoY += 5

    # Boundaries
    if playerOneY < 0:
        playerOneY = 0
    if playerOneY > 400:
        playerOneY = 400

    if playerTwoY < 0:
        playerTwoY = 0
    if playerTwoY > 400:
        playerTwoY = 400

            
            
        

    # Draw
    
    # Player 1
    screen.fill(BLACK)
    # Score
    drawText(str(score), 400, 50)
    pygame.draw.rect(screen, WHITE, player1)   
    # Player 2
    pygame.draw.rect(screen, WHITE, player2)
    # Ball
    pygame.draw.rect(screen, WHITE, ball)

    # update
    pygame.display.flip()

    while lose:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                lose = False
            if event.type == KEYDOWN:
                if event.key == pygame.K_q:
                    lose = False
                if event.key == pygame.K_r:
                    lose = False
            if event.type == KEYUP:
                if event.key == pygame.K_r:
                    running = True
                    

        
        
        screen.fill(BLACK)
        drawText("You Lost!", 350, 200)
        drawText("You had", 355, 250)
        drawText(str(score), 400, 300)
        drawText("Points", 370, 350)
        drawText("Press Q to QUIT or R to Restart", 250, 400)

        pygame.display.flip()
        


# quit
pygame.quit()

【问题讨论】:

  • 将所有初始化代码移到一个函数中,并在需要重置时调用它。
  • 不要重新启动,而是在另一个while-loop 中运行以一次又一次地重复它。而在这个循环中,你必须把playerOneY = 200等重新设置值。
  • doesnt seem to work 是什么意思?最好有问题地描述它(不在评论中),这样我们就可以看到你真正的问题是什么。

标签: python python-3.x pygame


【解决方案1】:

您必须将几乎所有内容放入另一个 while-loop 并为 loserunningrepeate 使用正确的值

import pygame
from pygame import rect
from pygame.constants import KEYDOWN, KEYUP
import random

# --- constants ---

# constant Variables
WIDTH = 800
HEIGHT = 600
FPS = 60

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

GREY = (89, 88, 78)
YELLOW = (232, 219, 93)

# --- functions ---

def drawText(t, x, y):
    text = font.render(t, True, YELLOW, GREY)
    text_rectangle = text.get_rect()
    text_rectangle.topleft = (x,y)
    screen.blit(text, text_rectangle)

# --- main ---

# Init
pygame.init()

#icon = pygame.image.load("pong.png")

# Create the Window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong")
#pygame.display.set_icon(icon)
font = pygame.font.Font(pygame.font.get_default_font(), 24)

restart = True

while restart:
    playerOneY = 200
    playerTwoY = 200
    
    ballY = HEIGHT / 2
    ballX = WIDTH / 2
    
    up1 = False
    up2 = False
    down1 = False
    down2 = False
    ballY = 300
    ballX = 400
    ballDx = 4
    ballDy = 4
    
    score = 0
    
    # - mainloop -
    clock = pygame.time.Clock()
    
    
    lose = False
    running = True
    # game loop
    while running:
        clock.tick(FPS)
        
        # QUIT
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == KEYDOWN:
                if event.key == pygame.K_w:
                    up1 = True
                if event.key == pygame.K_s:
                    down1 = True
                if event.key == pygame.K_UP:
                    up2 = True
                if event.key == pygame.K_DOWN:
                    down2 = True
                
            elif event.type == KEYUP:
                if event.key == pygame.K_w:
                    up1 = False
                if event.key == pygame.K_s:
                    down1 = False
                if event.key == pygame.K_UP:
                    up2 = False
                if event.key == pygame.K_DOWN:
                    down2 = False
            
        player1 = pygame.Rect(10, playerOneY, 20, 200)
        player2 = pygame.Rect(770, playerTwoY, 20, 200)
        ball = pygame.Rect(ballX, ballY, 20, 20)    
    
        # input
        # Ball Collition
        ballX += ballDx
        ballY += ballDy
        if ball.colliderect(player1):
            ballDx = abs(ballDx)
            score += 1
            print(score / 3)
    
        elif ball.colliderect(player2):
            ballDx = abs(ballDx) * -1
            score += 1
            print(score / 3)
        elif ballY <= 0:
            ballDy = abs(ballDy)
        elif ballY >= HEIGHT:
            ballDy = abs(ballDy) * -1
        elif ballX >= WIDTH:
            running = False
            lose = True
        elif ballX <= 0:
            running = False
            lose = True
            
        # Player Movement
        if up1 == True:
            playerOneY -= 5
        if up2 == True:
            playerTwoY -= 5
        
        if down1 == True:
            playerOneY += 5
        if down2 == True:
            playerTwoY += 5
    
        # Boundaries
        if playerOneY < 0:
            playerOneY = 0
        if playerOneY > 400:
            playerOneY = 400
    
        if playerTwoY < 0:
            playerTwoY = 0
        if playerTwoY > 400:
            playerTwoY = 400
    
        # Draw
        
        # Player 1
        screen.fill(BLACK)
        # Score
        drawText(str(score), 400, 50)
        pygame.draw.rect(screen, WHITE, player1)   
        # Player 2
        pygame.draw.rect(screen, WHITE, player2)
        # Ball
        pygame.draw.rect(screen, WHITE, ball)
    
        # update
        pygame.display.flip()
    
        while lose:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    lose = False
                    running = False
                    restart = False
                if event.type == KEYDOWN:
                    if event.key == pygame.K_q:
                        lose = False
                        running = False
                        restart = False
                    if event.key == pygame.K_r:
                        lose = False
                        running = False
            
            screen.fill(BLACK)
            drawText("You Lost!", 350, 200)
            drawText("You had", 355, 250)
            drawText(str(score), 400, 300)
            drawText("Points", 370, 350)
            drawText("Press Q to QUIT or R to Restart", 250, 400)
    
            pygame.display.flip()
    
# quit
pygame.quit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 2017-07-23
    • 2021-02-08
    • 2021-08-30
    • 1970-01-01
    相关资源
    最近更新 更多