2016-12-04 67 views
-1

我開始學習pygame的和Python,最近我收到此錯誤信息:「TypeError:必須是pygame.Surface,而不是元組」該怎麼辦? (pygame的起動器)

"TypeError: must be pygame.Surface, not tuple"

這是代碼的一部分,給予我的錯誤:

# 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) 
+0

BTW:你畫在白色背景下的白色圓圈,所以你不能看到它:) – furas

+0

pygame的不會直接將顯示器上,但緩衝,你必須使用'pygame的。 display.flip()'或'pygame。 display.update()'在監視器上發送緩衝區。你不使用pygame。 display.update()'在'time.sleep(4)'之前'因此它不會在監視器上發送圓。在'time.sleep(4)'之後''你使用'pygame。 display.update()'但首先你使用'screen.fill(white)',這樣它就從緩衝區中刪除了圓。 – furas

+0

錯誤消息很自我描述。 –

回答

0

試試這個:

circle = screen, white 
radius = 5.5 
surface = pygame.Surface((10, 10)) 
pygame.draw.circle(surface, white, (183, 218), 0, 5) 

http://www.pygame.org/docs/ref/surface.html

+0

我試過在其中給出相同的信息 –

+0

更新了答案。 –

+0

試着檢查我的代碼,也許你會找到答案,爲什麼它不工作 –

1

pygame.draw.circle()預計pygame.Surface()對象作爲第一個參數,但您使用的元組10, 10。您可以使用screen在窗口中繪製。

screen = pygame.display.set_mode((800, 600)) 

WHITE = (255, 255, 255) 

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

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

編輯:全面工作示例

import pygame 

# --- constants --- (UPPER_CASE names) 

WHITE = (255, 255, 255) 
BLACK = ( 0, 0, 0) 

WIDTH = 800 
HEIGHT = 600 

# --- classes --- (CamelCase names) 

#empty 

# --- functions --- (lower_case names) 

#empty 

# --- main --- (lower_case names) 

# - init - 

pygame.init() 
screen = pygame.display.set_mode((WIDTH, HEIGHT)) 
screen_rect = screen.get_rect() 

# - objects - 

# use center of screen as circle center 
circle_position = screen_rect.center 

circle_radius_1 = 5 # expected integer 
circle_radius_2 = 100 # expected integer 

circle_border_size = 5 

# - mainloop -- 

running = True 

while running: 

    # - events - 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      running = False 
     elif event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_ESCAPE: 
       running = False 

    # - updates (without draws) - 

    circle_radius_1 += 1 
    if circle_radius_1 > WIDTH/2: 
     circle_radius_1 = 5 

    circle_radius_2 += 1 
    if circle_radius_2 > WIDTH/2: 
     circle_radius_2 = 5  

    # - draws (without updates) - 

    # clear buffer (surface `screen`) 
    screen.fill(BLACK) 
    # draw circle on buffer (surface `screen`) 
    pygame.draw.circle(screen, WHITE, circle_position , circle_radius_1, circle_border_size) 
    pygame.draw.circle(screen, WHITE, circle_position , circle_radius_2, circle_border_size) 
    # send buffer (surface `screen`) on monitor 
    pygame.display.update() 

# - end - 

pygame.quit() 

編輯:與修改你的代碼。我沒有音樂測試它。

import pygame 

# --- constants --- (UPPER_CASE names) 

RED = (255, 0, 0) 
WHITE = (255, 255, 255) 
BLACK = ( 0, 0, 0) 

WIDHT_SCREEN = 368 
HEIGHT_SCREEN = 406 

FPS = 30 

MOVING_SOUND = "Pacman Waka Waka.mp3" 

# --- classe --- (CamelCase names) 

# class Player(pygame.sprite.Sprite): 
# pass 

# class Enemy(pygame.sprite.Sprite): 
# pass 

# class YellowEnemy(Enemy): 
# pass 

# --- main --- (lower_case names) 

# - init - 

pygame.init() 
pygame.mixer.init() 

screen = pygame.display.set_mode((WIDHT_SCREEN, HEIGHT_SCREEN)) 
screen_rect = screen.get_rect() 

pygame.display.set_caption("PacMan Goes") 

# - music - 

pygame.mixer.music.load('pacManSong.mp3') 
pygame.mixer.music.play() 

# - objects - 

# - objects - player - 

x_speed = 0 
y_speed = 0 

pac_left = pygame.image.load("pacmanLeft.png") 
pac_left = pygame.transform.scale(pac_left, (20, 20)) 

pac_right = pygame.image.load("pacman.png") 
pac_right = pygame.transform.scale(pac_right, (20, 20)) 

pac_up = pygame.image.load("Up.png") 
pac_up = pygame.transform.scale(pac_up, (20, 20)) 

pac_down = pygame.image.load("Down.png") 
pac_down = pygame.transform.scale(pac_down, (20, 20)) 

pac = pac_right 
pac_rect = pac.get_rect(x=173, y=218) 

# - objects - background - 

bg = pygame.image.load("pacmanBackground.jpg").convert() 
bg = pygame.transform.scale(bg, (WIDHT_SCREEN, HEIGHT_SCREEN)) 
open_screen = pygame.image.load("Pac-Man_Logo.PNG") 

# - objects - circles - 

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

# - clock - FPS - 

clock = pygame.time.Clock() 

# --- first scene --- 

# - draws - 

screen.fill(WHITE) 
pygame.draw.rect(screen, RED, screen_rect, 5) 
pygame.draw.circle(screen, BLACK, position, radius, border_size) 
pygame.display.update() 

# - mainloop - "sleep" but still react on ESC press 

current_time = pygame.time.get_ticks() 
end_time = current_time + 4000 # 4000ms = 4s 

running = True 

while running and end_time > current_time: 

    # --- events --- 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      running = False 
     elif event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_ESCAPE: 
        running = False 

    # --- constants speed on fast and slow computer 

    clock.tick(FPS) 

    # --- updates --- 

    current_time = pygame.time.get_ticks() 

# --- second scene - game --- 

# - mainloop - 

game_on = True 

while game_on: 

    # --- events --- 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      game_on = False 
     elif event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_LEFT: 
       pac = pac_left 
       pygame.mixer.music.load(moving_sound) 
       pygame.mixer.music.play() 
       pygame.mixer.music.rewind() 
       x_speed = -3 
      elif event.key == pygame.K_RIGHT: 
       pac = pac_right 
       pygame.mixer.music.load(moving_sound) 
       pygame.mixer.music.play() 
       pygame.mixer.music.rewind() 
       x_speed = 3 
      elif event.key == pygame.K_UP: 
       pac = pac_up 
       pygame.mixer.music.load(moving_sound) 
       pygame.mixer.music.play() 
       pygame.mixer.music.rewind() 
       y_speed = -3 
      elif event.key == pygame.K_DOWN: 
       pac = pac_down 
       pygame.mixer.music.load(moving_sound) 
       pygame.mixer.music.play() 
       pygame.mixer.music.rewind() 
       y_speed = 3 
     elif event.type == pygame.KEYUP: 
      if event.key in (pygame.K_LEFT, pygame.K_RIGHT): 
       x_speed = 0 
      elif event.key in (pygame.K_UP, pygame.K_DOWN): 
       y_speed = 0 

    # --- updates --- 

    if pac_rect.left <= screen_rect.left or pac_rect.right >= screen_rect.right: 
     x_speed *= -1 

    if pac_rect.top <= screen_rect.top or pac_rect.bottom >= screen_rect.bottom: 
     y_speed *= -1 

    pac_rect.move_ip(x_speed, y_speed) 

    # --- draws --- 

    screen.fill(WHITE) 

    screen.blit(bg, (0, 0)) 
    screen.blit(pac, pac_rect) 

    pygame.display.update() 

    # --- constants speed on fast and slow computer 

    clock.tick(FPS) 

# --- end --- 

pygame.quit()  
+0

現在它沒有給出錯誤信息,但它沒有顯示在屏幕上 –

+0

你使用'pygame.display.update()'嗎?你是否在屏幕上繪製了「屏幕」或其他任何必須「blit()」的表面? – furas

+0

查看完整的工作示例。 – furas

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() 
+0

更新了我的答案。謝謝你的幫助。 –

+0

嘗試檢查我的代碼也許你會找到答案,爲什麼它不工作 –

相關問題