0
我使用Python和Pygame創建了一個類似Pong的程序。我會盡快編輯它來添加點系統和其他功能,但我想先解決一個問題。Python中的Pygame:Screen.Blit Problems
在這個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
我的代碼包含大量的高清功能,但其他人完全正常工作。這是我的代碼:
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的新手!