0

我正在使用 Python 和 Pygame 创建一个类似 Pong 的小程序。我将很快对其进行编辑以添加积分系统和其他功能,但我想先解决一个问题。

在这个 Pong 程序中,你得到了三个生命,如果你在最后一个生命中输了,它应该显示一个游戏结束图像。它在前三个生命中工作得非常好,但它不会产生游戏过度图像,而是产生这个:

Traceback (most recent call last):
  File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 258, in <module>
    gameplay1()
  File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 84, in gameplay1
    change_level1()
  File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 241, in change_level1
    gameplay2()
  File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 161, in gameplay2
    change_level2()
  File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 245, in change_level2
    gameplay3()
  File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 238, in gameplay3
    game_over()
  File "/Volumes/JARED'S USB 3/Python/Pie Game.py", line 255, in game_over
    screen.blit(game_over1(300,250))
TypeError: 'pygame.Surface' object is not callable

我的代码由许多 def 函数组成,但其他的工作得很好。这是我的代码:

import pygame, time, sys, random
from pygame.locals import *
pygame.init()

screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption ("Pong Squash")

def gameplay1():
    global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
    game_over_display = "game_over.png"
    lives = "lives.png"
    points = "points.png"
    lives_remaining = "lives_remaining.png"

    game_over1 = pygame.image.load(game_over_display).convert()
    lives1 = pygame.image.load(lives).convert()
    points1 = pygame.image.load(points).convert()
    lives_remaining1 = pygame.image.load(lives_remaining).convert()

    font1 = pygame.font.Font(None, 40)

    white = 255,255,255
    black = 0, 0, 0
    green = 0, 250, 0
    yellow = 255, 255, 0

    lives_count = font1.render(('3'), True, white)

    position_x = 175
    position_y = 375
    velocity_x1 = 0
    velocity_y1 = 0
    position1 = 275
    position2 = 150
    velocity1 = 2
    velocity2 = 2

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if waiting:
                        waiting = False
                        reset_ball()
                if event.key == pygame.K_LEFT:
                    velocity_x1 = (velocity_x1 - 3)
                elif event.key == pygame. K_RIGHT:
                    velocity_x1 = (velocity_x1 + 3)
            elif event.type == KEYUP:
                if event.key == K_LEFT or event.key == K_RIGHT:
                    velocity_x1 = 0

        screen.fill((0, 0, 0))
        color = 255
        width = 0
        position = position_x, position_y, 250, 50
        position_x += velocity_x1
        position_y += velocity_y1
        position1 += velocity1
        position2 += velocity2

        player = pygame.draw.rect(screen, (color, color, color), position, width)
        ball = pygame.draw.rect(screen, (color, color, color), (position1, position2, 25, 25), width)
        screen.blit(lives1, (450, 0))
        screen.blit(points1,(0, 0))
        screen.blit(lives_count,(560,5))
        pygame.display.update()

        if player.colliderect(ball):
            velocity1 = - velocity1
            velocity2 = - velocity2

        if position_x > 350 or position_x < 0:
            velocity_x1 = 0
        elif position1 > 575 or position1 < 0:
            velocity1 = - velocity1
        elif position2 < 0:
            velocity2 = - velocity2
            velocity2 = velocity2 + 0.5
        elif position2 > 475:
            change_level1()
def gameplay2():  
    global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
    game_over_display = "game_over.png"
    lives = "lives.png"
    points = "points.png"
    lives_remaining = "lives_remaining.png"

    game_over1 = pygame.image.load(game_over_display).convert()
    lives1 = pygame.image.load(lives).convert()
    points1 = pygame.image.load(points).convert()
    lives_remaining1 = pygame.image.load(lives_remaining).convert()

    font1 = pygame.font.Font(None, 40)

    white = 255,255,255
    black = 0, 0, 0
    green = 0, 250, 0
    yellow = 255, 255, 0

    lives_count = font1.render(('2'), True, white)

    position_x = 175
    position_y = 375
    velocity_x1 = 0
    velocity_y1 = 0
    position1 = 275
    position2 = 150
    velocity1 = 2
    velocity2 = 2

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if waiting:
                        waiting = False
                        reset_ball()
                if event.key == pygame.K_LEFT:
                    velocity_x1 = (velocity_x1 - 3)
                elif event.key == pygame. K_RIGHT:
                    velocity_x1 = (velocity_x1 + 3)
            elif event.type == KEYUP:
                if event.key == K_LEFT or event.key == K_RIGHT:
                    velocity_x1 = 0

        screen.fill((0, 0, 0))
        color = 255
        width = 0
        position = position_x, position_y, 250, 50
        position_x += velocity_x1
        position_y += velocity_y1
        position1 += velocity1
        position2 += velocity2

        player = pygame.draw.rect(screen, (color, color, color), position, width)
        ball = pygame.draw.rect(screen, (color, color, color), (position1, position2, 25, 25), width)
        screen.blit(lives1, (450, 0))
        screen.blit(points1,(0, 0))
        screen.blit(lives_count,(560,5))
        pygame.display.update()

        if player.colliderect(ball):
            velocity1 = - velocity1
            velocity2 = - velocity2

        if position_x > 350 or position_x < 0:
            velocity_x1 = 0
        elif position1 > 575 or position1 < 0:
            velocity1 = - velocity1
        elif position2 < 0:
            velocity2 = - velocity2
            velocity2 = velocity2 + 0.5
        elif position2 > 475:
            change_level2()
def gameplay3():
    global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
    game_over_display = "game_over.png"
    lives = "lives.png"
    points = "points.png"
    lives_remaining = "lives_remaining.png"

    game_over1 = pygame.image.load(game_over_display).convert()
    lives1 = pygame.image.load(lives).convert()
    points1 = pygame.image.load(points).convert()
    lives_remaining1 = pygame.image.load(lives_remaining).convert()

    font1 = pygame.font.Font(None, 40)

    white = 255,255,255
    black = 0, 0, 0
    green = 0, 250, 0
    yellow = 255, 255, 0

    lives_count = font1.render(('1'), True, white)

    position_x = 175
    position_y = 375
    velocity_x1 = 0
    velocity_y1 = 0
    position1 = 275
    position2 = 150
    velocity1 = 2
    velocity2 = 2

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if waiting:
                        waiting = False
                        reset_ball()
                if event.key == pygame.K_LEFT:
                    velocity_x1 = (velocity_x1 - 3)
                elif event.key == pygame. K_RIGHT:
                    velocity_x1 = (velocity_x1 + 3)
            elif event.type == KEYUP:
                if event.key == K_LEFT or event.key == K_RIGHT:
                    velocity_x1 = 0

        screen.fill((0, 0, 0))
        color = 255
        width = 0
        position = position_x, position_y, 250, 50
        position_x += velocity_x1
        position_y += velocity_y1
        position1 += velocity1
        position2 += velocity2

        player = pygame.draw.rect(screen, (color, color, color), position, width)
        ball = pygame.draw.rect(screen, (color, color, color), (position1, position2, 25, 25), width)
        screen.blit(lives1, (450, 0))
        screen.blit(points1,(0, 0))
        screen.blit(lives_count,(560,5))
        pygame.display.update()

        if player.colliderect(ball):
            velocity1 = - velocity1
            velocity2 = - velocity2

        if position_x > 350 or position_x < 0:
            velocity_x1 = 0
        elif position1 > 575 or position1 < 0:
            velocity1 = - velocity1
        elif position2 < 0:
            velocity2 = - velocity2
            velocity2 = velocity2 + 0.5
        elif position2 > 475:
            game_over()
def change_level1():
    global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
    gameplay2()
    pygame.display.update()
def change_level2():
    global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
    gameplay3()
    pygame.display.update()
def reset_ball():
  global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
  velocity1 = 2
  velocity2 = 2
  pygame.display.update()
def game_over():
    global game_over_display, lives, points, lives_remaining, game_over1, lives1, points1, lives_remaining1, font1, white, black, green, yellow, lives_number, lives_count, position_x, position_y, velocity_x1, velocity_y1, position1, position2, velocity1, velocity2, color, width, position, player, ball
    screen.fill((0,0,0))
    screen.blit(game_over1(300,250))
    pygame.display.update()

gameplay1()

对不起,这么久。请帮忙!我是 Pygame 的新手!

4

1 回答 1

4
screen.blit(game_over1(300,250))

文档:pygame.Surface

screen.blit(game_over1, (300,250))
于 2013-07-27T18:40:31.667 回答