NoneType不是可迭代的错误pygame 3.5

时间:2016-02-22 00:21:05

标签: python python-3.x

我的老师检查了我的代码,我们不知道是什么给了我这个错误,我更新的最后一件事是' fireShell'定义。在我实现这个定义之前游戏正在运行,当你按空格键时游戏崩溃了。它是坦克游戏,随机蛇的东西来自我们制作的旧代码。

import pygame
import time
import random

pygame.init()

white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
purple = (200,0,200)
green = (0,155,0)
yellow = (255,255,0)
pink = (255,153,204)
light_green = (0,255,0)
orange = (255,165,0)
clock = pygame.time.Clock()

display_width = 800
display_height = 600



gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Tanks')

#icon = pygame.image.load('snakeHead.png')
#pygame.display.set_icon(icon)

#img = pygame.image.load('snakeHead.png')
#appleimg = pygame.image.load('apple.png')

FPS = 10

tankWidth = 40
tankHeight = 20
turretWidth = 5
wheelWidth = 5

smallfont = pygame.font.SysFont("comicsansms", 25)
medfont = pygame.font.SysFont("comicsansms", 40)
largefont = pygame.font.SysFont("comicsansms", 70)

def tank(x,y,turPos):
    x = int(x)
    y = int(y)

    possibleTurrets = [(x-27, y-2),
                       (x-26, y-5),
                       (x-25, y-8),
                       (x-23, y-12),
                       (x-20, y-14),
                       (x-18, y-15),
                       (x-15, y-17),
                       (x-13, y-19),
                       (x-11, y-21), 
                       ]


    pygame.draw.circle(gameDisplay, black, (x,y),10)
    pygame.draw.rect(gameDisplay, black, (x-tankHeight, y, tankWidth, tankHeight))

    pygame.draw.line(gameDisplay, black, (x,y,), possibleTurrets[turPos], turretWidth)

    pygame.draw.circle(gameDisplay, black, (x-15, y+20), wheelWidth)
    pygame.draw.circle(gameDisplay, black, (x-10, y+20), wheelWidth)
    pygame.draw.circle(gameDisplay, black, (x-5, y+20), wheelWidth)
    pygame.draw.circle(gameDisplay, black, (x, y+20), wheelWidth)
    pygame.draw.circle(gameDisplay, black, (x+5, y+20), wheelWidth)
    pygame.draw.circle(gameDisplay, black, (x+10, y+20), wheelWidth)
    pygame.draw.circle(gameDisplay, black, (x+15, y+20), wheelWidth)



def introbutton():
    cur = pygame.mouse.get_pos()

    if 150+100> cur[0] > 150 and 500+50 > cur[1] > 500:
        pygame.draw.rect(gameDisplay,light_green,(150,500,100,50))
    else:
        pygame.draw.rect(gameDisplay,green,(150,500,100,50))

    if 350+100> cur[0] > 350 and 500+50 > cur[1] > 500:
        pygame.draw.rect(gameDisplay,pink,(350,500,100,50))
    else:
        pygame.draw.rect(gameDisplay,yellow,(350,500,100,50))

    if 550+100> cur [0] > 550 and 500+50 > cur[1] > 500:
        pygame.draw.rect(gameDisplay,orange,(550,500,100,50))
    else:
        pygame.draw.rect(gameDisplay,red,(550,500,100,50))

def text_to_button(msg, color, buttonx, buttony, buttonwidth, buttonheight, size = "small"):
    textSurf, textRect = text_objects(msg,color,size)
    textRect.center = ((buttonx+(buttonwidth/2)), buttony+(buttonheight/2))
    gameDisplay.blit(textSurf, textRect)

def controls():

    controls = True

    while controls:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(white)

        message_to_screen("Controls",
                          green,
                          -100,
                          "large")
        message_to_screen("Fire: Spacebar",
                          black,
                          -30)
        message_to_screen("Move Turret: W & S",
                          black,
                          10)
        message_to_screen("Move Tank: A & D",
                          black,
                          50)
        message_to_screen("Pause: P",
                          black,
                          90,)

        introbutton()


        button("play",
               150,500,100,50,
               green,
               light_green,
               action = "play")
        button("menu",
               350,500,100,50, 
               yellow,
               pink,
               action = "menu")
        button("quit",
               550,500,100,50, 
               red,
               orange,
               action = "quit")


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

def button(text, x, y, width, height, inactive_color, active_color, action = None):
    cur = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x + width > cur[0] > x and y + height > cur[1] > y:
        pygame.draw.rect(gameDisplay, active_color, (x,y,width,height))
        if click[0] == 1 and action != None:
            if action == "quit":
                pygame.quit()
                quit()
            if action == "play":
                gameLoop()
            if action == "controls":
                controls()
            if action == "menu":
                game_intro()
    else:
        pygame.draw.rect(gameDisplay,inactive_color, (x,y,width,height))

    text_to_button(text,black,x,y,width,height)


def pause():
    paused = True
    message_to_screen("Paused",
                      black,
                      -100,
                      "large")

    message_to_screen("Press C to continue or Q to quit.",
                      black,
                      25)

    pygame.display.update()

    while paused:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_c:
                    paused = False

                elif event.key == pygame.K_q:
                    pygame.quit()
                    quit()


        clock.tick(5)

def score(score):
    text = smallfont.render("score: "+str(score), True, black)
    gameDisplay.blit(text, [0,0])

def game_intro():

    intro = True

    while intro:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_c:
                    intro = False
                if event.key == pygame.K_q:
                    pygame.quit()
                    quit()

        gameDisplay.fill(white)

        message_to_screen("Welcome to Tanks",
                          green,
                          -100,
                          "large")
        message_to_screen("The objective of the game is to shoot and destroy",
                          black,
                          -30)
        message_to_screen("the enemy tank before they destroy you.",
                          black,
                          10)
        message_to_screen("The more enemies you destroy the harder it gets.",
                          black,
                          50)

        introbutton()


        button("play",
               150,500,100,50,
               green,
               light_green,
               action = "play")
        button("controls",
               350,500,100,50, 
               yellow,
               pink,
               action = "controls")
        button("quit",
               550,500,100,50, 
               red,
               orange,
               action = "quit")


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


def text_objects(text, color, size):
    if size == "small":

        textSurface = smallfont.render(text, True, color)
    elif size == "medium":
        textSurface = medfont.render(text, True, color)
    elif size == "large":
        textSurface = largefont.render(text, True, color)

    return textSurface, textSurface.get_rect()

def message_to_screen(msg, color, y_displace=0, size = "small"):
    textSurf, textRect = text_objects(msg,color, size)
    textRect.center = (display_width/2), (display_height/2)+y_displace
    gameDisplay.blit(textSurf, textRect)

def barrier(xlocation,randomHeight, barrier_width):
    #xlocation = (display_width/2) + random.randint(-0.2*display_width, 0.2*display_width)
    #randomHeight = random.randrange(display_height*0.1,display_height*0.6)

    pygame.draw.rect(gameDisplay, black, [xlocation, display_height-randomHeight, 50, randomHeight])


def fireShell(xy,tankx,tanky,turPos):
    fire = True

    startingShell = list(xy)
    print("FIRE",xy)

    while fire:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        print(startingShell[0],startingShell[1])
        pygame.draw.circle(gameDisplay, red, (startingShell[0],startingShell[1]),5)

        startingShell[0] -= (12 - turPos)*2
        startingShell[1] += int((((startingShell[0]-xy[0])*0.015)**2) - (turPos+turPos/(12-turPos)))

        if startingShell[1] > display_height:
            fire = False

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

def gameLoop():

    gameExit = False
    gameOver = False

    mainTankX = display_width * 0.9
    mainTankY = display_height * 0.7
    tankMove = 0

    currentTurPos = 0
    changeTur = 0

    barrier_width = 50
    xlocation = (display_width/2) + random.randint(-0.2*display_width, 0.2*display_width)
    randomHeight = random.randrange(display_height*0.1,display_height*0.6)


    while not gameExit:
        gameDisplay.fill(white)
        gun = tank(mainTankX,mainTankY,currentTurPos)

        if gameOver == True:
            message_to_screen("Game Over",
                              purple,
                              -50,
                              "large")

            message_to_screen("Press C to play again or Q to quit",
                              black,
                              50,
                              "medium")

            pygame.display.update()

        while gameOver == True:

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    gameExit = True
                    gameOver = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        gameExit = True
                        gameOver = False
                    if event.key == pygame.K_c:
                        gameLoop()


        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    tankMove = -5

                elif event.key == pygame.K_RIGHT:
                    tankMove = 5

                elif event.key == pygame.K_UP:
                    changeTur = 1

                elif event.key == pygame.K_DOWN:
                    changeTur = -1

                elif event.key == pygame.K_p:
                    pause()

                elif event.key == pygame.K_SPACE:
                    fireShell(gun,mainTankX,mainTankY,currentTurPos)

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    tankMove = 0
                elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    changeTur = 0


        #gameDisplay.fill(white)
        mainTankX += tankMove
        currentTurPos += changeTur
        if currentTurPos > 8:
            currentTurPos = 8
        elif currentTurPos < 0:
            currentTurPos = 0

        if mainTankX - (tankWidth/2) < xlocation+barrier_width:
            mainTankX += 5



        #tank(mainTankX,mainTankY,currentTurPos)

        ##for x in range(1):
        barrier(xlocation, randomHeight, barrier_width)


        #score(snakeLength-1)

        pygame.display.update()


        clock.tick(FPS)

    pygame.quit()

    quit()

game_intro()
gameLoop()

1 个答案:

答案 0 :(得分:1)

我检查的第一个地方(有更多的印刷品,测试等)是

def fireShell(xy,tankx,tanky,turPos):
    ...
    while fire:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

这是新功能中的唯一迭代。如果pygame.event.get()返回None,我就会发现此错误

In [235]: for i in None:print(i)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-235-5168590b751e> in <module>()
----> 1 for i in None:print(i)

TypeError: 'NoneType' object is not iterable