Python中的Pygame:Screen.Blit问题

时间:2013-07-27 18:04:56

标签: image python-2.7 pygame

我正在使用Python和Pygame创建一个类似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的新手!

1 个答案:

答案 0 :(得分:4)

screen.blit(game_over1(300,250))

docs:pygame.Surface

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