“TypeError:必须是pygame.Surface,而不是元组”怎么办? (pygame启动器)

时间:2016-12-04 16:18:01

标签: python pygame

我开始学习pygame和python,最近我收到了这条错误消息:

  

“TypeError:必须是pygame.Surface,而不是元组”

这是给我错误的代码部分:

# circles basic setup 
circle = screen, white
radius = 5.5
surface = 10, 10
pygame.draw.circle(surface, white, (183, 218), 0, 5)

完整代码:

import pygame, time

from pygame import mixer, surface

pygame.init()

# define colors
red = (255, 255, 255)
white = (255, 255, 255)
black = (0, 0, 0)

# define size's
widthScreen, heightScreen = 368, 406

# define sounds
movingSound = "Pacman Waka Waka.mp3"

# setup
pygame.init()
screen = pygame.display.set_mode((widthScreen, heightScreen))
pygame.display.set_caption("PacMan Goes")
screen.fill(white)


pygame.draw.rect(screen, red, (0, 0, widthScreen, heightScreen), 5)
pac = pygame.image.load("pacman.png")
pac = pygame.transform.scale(pac, (22, 22))
pac_rect = pac.get_rect()
pac_rect.move_ip(173, 218)
mixer.init()
mixer.music.load('pacManSong.mp3')
mixer.music.play()

# speed in pixels per frame
x_speed = 0
y_speed = 0
# define Background
bg = pygame.image.load("pacmanBackground.jpg").convert()
bg = pygame.transform.scale(bg, (widthScreen, heightScreen))
openScreen = pygame.image.load("Pac-Man_Logo.PNG")

# circles basic setup
position = (183, 218)
radius = 5  # expected integer
border_size = 5

pygame.draw.circle(screen, white, position, radius, border_size)

time.sleep(4)

game_on = True
#   -------------- Main Program Loop --------------   #
while game_on:
# --- Event Processing --- #
screen.fill(white)
event = pygame.event.poll()
if event.type == pygame.QUIT:
    game_on = False
    # user pressed down on a key
elif event.type == pygame.KEYDOWN:
    # figure out if that was an arrow key, if so
    # adjust speed.
    if event.key == pygame.K_LEFT:
        pac = pygame.image.load("pacmanLeft.png")
        pac = pygame.transform.scale(pac, (20, 20))
        mixer.music.load('Pacman Waka Waka.mp3')
        mixer.music.play()
        pygame.mixer.music.rewind()
        x_speed = -3
    elif event.key == pygame.K_RIGHT:
        mixer.music.load(movingSound)
        mixer.music.play()
        pygame.mixer.music.rewind()
        pac = pygame.image.load("pacman.png")
        pac = pygame.transform.scale(pac, (20, 20))
        x_speed = 3
    elif event.key == pygame.K_UP:
        mixer.music.load(movingSound)
        mixer.music.play()
        pygame.mixer.music.rewind()
        pac = pygame.image.load("Up.png")
        pac = pygame.transform.scale(pac, (20, 20))
        y_speed = -3
    elif event.key == pygame.K_DOWN:
        pac = pygame.image.load("Down.png")
        pac = pygame.transform.scale(pac, (20, 22))
        mixer.music.load(movingSound)
        mixer.music.play()
        pygame.mixer.music.rewind()
        y_speed = 3
    # user let up on a key
elif event.type == pygame.KEYUP:
    #if it is an arrow key reset vector back to 0
    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
        x_speed = 0
    elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
        y_speed = 0
if pac_rect.right >= widthScreen or pac_rect.right <= 0 and pac_rect.left >= widthScreen or pac_rect.left <= 0:
    x_speed *= -1

if pac_rect.bottom >= heightScreen or pac_rect.bottom <= 0 and pac_rect.top >= heightScreen or pac_rect.top <= 0:
    y_speed *= -1
pac_rect = pac_rect.move(x_speed, y_speed)
screen.blit(bg, (0, 0))
screen.blit(pac, pac_rect)
pygame.display.flip()
pygame.display.update()
pygame.time.wait(10)

1 个答案:

答案 0 :(得分:0)

也许你忘了初始化pygame。这是我的工作实例,随时问我进一步的问题!

import pygame,sys
from pygame.locals import *

pygame.init()
surface = pygame.display.set_mode((100, 100))
pygame.display.set_caption('Hello World')

radius = 5 #must be integer
WHITE = (255,255,255)
circle = pygame.draw.circle(surface,WHITE,(50,50),radius)


while True: 
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()