pygame函数只能运行一次,不会循环

时间:2018-08-13 03:40:17

标签: python pygame

我有一个正在上学的游戏的暂停菜单。如果用户单击“ p”,它将启动暂停菜单。暂停菜单具有一项功能,如果用户单击一个按钮,该用户将被重新启动进入游戏。问题在于,将它们发布到游戏中后,“ p”暂停功能不再起作用。我不确定是否正确循环。

乒乓球游戏

'specification_id': $(this).closest('form').find('#specification_id').val(),

现在这是我的暂停菜单代码。

   import pygame

black = (0,0,0)
white = (255,255,255)

pygame.init()

size = 800,600
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Basketball Shootout")

done = False
clock = pygame.time.Clock()


def player1(x1, y1, xsize, ysize):
    pygame.draw.rect(screen, black, [x1, y1, xsize, ysize])

def player2(x2, y2, xsize, ysize):
    pygame.draw.rect(screen, black, [x2,y2,xsize,ysize])

def ball(ballx, bally):
    pygame.draw.circle(screen, black, [ballx,bally],20)

def Score1(score1):
    font = pygame.font.Font("Minecraft.ttf" ,50)
    text = font.render(str(score1), True, white)
    screen.blit(text, [160, 550])

def Score2(score2):
    font = pygame.font.Font("Minecraft.ttf" ,50)
    text = font.render(str(score2), True, white)
    screen.blit(text, [610, 550])

x1 = 20
y1 = 175
xsize = 35
ysize = 150
speed1 = 0

x2 = 740
y2 = 175
speed2 = 0

ballx = 550
bally = 250
speedx = 8
speedy = 5

score1 = 0
score2 = 0

bg = pygame.image.load("pongbg2.png")
rect1 = pygame.Rect(50,510,100,50)

def pausescreen():
    import pausescreen

display_game = True
game_page = 1


while not done: 
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

            pygame.draw.rect(screen, (255, 255, 255), rect1)

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                speed1 = -10
            if event.key == pygame.K_s:
                speed1 = 10
            if event.key == pygame.K_UP:
                speed2 = -10
            if event.key == pygame.K_DOWN:
                speed2 = 10

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w:
                speed1 = 0
            if event.key == pygame.K_s:
                speed1 = 0
            if event.key == pygame.K_UP:
                speed2 = 0
            if event.key ==  pygame.K_DOWN:
                speed2 = 0
            if event.key == pygame.K_p:
                pausescreen()




    screen.blit(bg, (0, 0))
    player1(x1, y1, xsize, ysize)
    player2(x2, y2, xsize, ysize)
    ball(ballx,bally)
    Score1(score1)
    Score2(score2)

    y1 += speed1
    y2 += speed2
    ballx += speedx
    bally += speedy

    if y1 < 0:
        y1 = 0

    if y1 > 350:
        y1 = 350

    if y2 < 0:
        y2 = 0

    if y2 > 350:
        y2 = 350

    if ballx+20 > x2 and bally-20 > y2 and bally+20 < y2+ysize and ballx < x2+3:
        speedx = -speedx

    if ballx-20 < x1+35 and bally-20 > y1 and bally+20 < y1+ysize and ballx > x1+38:
        speedx = -speedx

    if bally > 477 or bally < 23:
        speedy = -speedy

    if ballx < 13:
        score2 += 1
        ballx = 350
        bally = 250

    if ballx > 750:
        score1 += 1
        ballx = 350
        bally = 250




    pygame.display.flip()

    clock.tick(60)

pygame.quit()

1 个答案:

答案 0 :(得分:0)

问题是您正在从另一个程序导入暂停屏幕,这会产生问题,因为您只能导入一次库。您可以尝试将暂停屏幕代码放入函数中而不是调用它,但是如果您坚持要这样做,可以将您的pause_screen代码放入函数中并执行from pausescreen import name_of_function,因此只要需要调用该代码,只需编写{{ 1}}