TypeError,定义某些事情有问题

时间:2016-08-14 12:57:05

标签: python-3.x pygame typeerror

好的,我正在用python和pygame制作一个简单的游戏,你是一个宇宙飞船,不得不躲避流星。但是在添加流星并编写代码以使其随机生成并向下移动屏幕之后。我也有使用代码标签的问题,我会按Ctrl + K并在提供的空间内发布我的代码,它给了我代码错误。对不起,这是我的链接(不是最好的方式,我知道,我无法让代码标签工作。)

我的错误:

Traceback (most recent call last):
  File "C:\Users\fredd_000\Desktop\Pygame_Script.py", line 127, in <module>
    game_loop()
  File "C:\Users\fredd_000\Desktop\Pygame_Script.py", line 98, in game_loop
    things(thing_startx, thing_starty, thing_width, thing_height, black)
  File "C:\Users\fredd_000\Desktop\Pygame_Script.py", line 28, in things
    pygame.draw.circle(gameDisplay, color,[thingx, thingy, thingw, thingh])
TypeError: function takes at least 4 arguments (3 given)

发生错误的代码:

def things(thingx, thingy, thingw, thingh, color):
    pygame.draw.circle(gameDisplay, color,[thingx, thingy, thingw, thingh])

完整代码:

import pygame
import time
import random

pygame.init()


display_width = 1200
display_height = 800

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A Game By Freddie')

black = (0,0,0)
white = (255,255,255)
blue = (25,0,255)
red = (255,0,0)

car_width = 195
car_height = 1

clock = pygame.time.Clock()
gameExit = False
shipImg = pygame.image.load('ship.png')


def things(thingx, thingy, thingw, thingh, color):
    pygame.draw.circle(gameDisplay, color,[thingx, thingy, thingw, thingh])

def ship(x,y):
    gameDisplay.blit(shipImg, (x,y))

def text_objects(text, font):
    textSurface = font.render(text, True, red)
    return textSurface, textSurface.get_rect()



def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',120)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()



def crash():
    message_display('You Died')

def game_loop():

    x =  (display_width * 0.375)
    y = (display_height * 0.65)

    x_change = 0
    y_change = 0


    thing_startx = random.randrange(0, display_width)
    thing_starty = -600
    thing_speed = 7
    thing_width = 100
    thing_height = 100

    gameExit = False

    while not gameExit:


        gameDisplay.fill(blue)
        shipImg = pygame.image.load('ship.png')
        ship(x,y)
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0


        x += x_change



        things(thing_startx, thing_starty, thing_width, thing_height, black)
        thing_starty += thing_speed

        if x > display_width - car_width or x < 0:
            crash()

        if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y_change = -5
                elif event.key == pygame.K_DOWN:
                    y_change = 5

        if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0

        y += y_change

        if y > display_height - car_height or y < 0:
            crash()





            pygame.display.update()
        clock.tick(60)


game_loop()
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:1)

您的代码存在的问题是您使用pygame.draw.circle()错误。使用该函数的语法是:pygame.draw.circle(screen, color, (x, y), diameter)。你可以说,你做的方式是pygame.draw.circle(screen, color, [x, y, w, h])。这是不正确的,圆圈没有宽度和高度。将您的代码更改为:pygame.draw.circle(gameDisplay, color, (thingx, thingy), thingw)thingw将是您圈子的直径。

您还应将功能参数更改为def things(thingx, thingy, thingw, color)

如果您仍然对Pygame不熟悉或者对如何在屏幕上绘制形状感到困惑,我强烈建议您阅读User Previledges,就像在评论中链接的Idos一样。