Python TypeError:尽管有适当的赋值,'int'对象仍然是不可迭代的

时间:2014-11-27 20:28:53

标签: python methods int typeerror

当我运行这个程序时,它在到达display_pipe方法时给出了一个类型错误,因为它不认为其中一个变量是int,当每个参数作为int输入,而所有其他变量都是方法是整数。

'''This is a simple replica of flappy bird for the pc that I made to help me
understand python.'''


import random
import pygame
from pygame import *
import math
import sys

#Presets for window
size=width,height=500,500
Ag=-9.80665
clock = pygame.time.Clock()
white=(255,255,255)
blue=(0,0,255)
red=(255,0,0)
gray_bgColor=(190,193,212)

#Initialise pygame Surface as screen
pygame.init()
pygame.font.init()
#Creates icon for window
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Flappy Bird Replica")
icon = pygame.image.load("images/icon.png").convert()
pygame.display.set_icon(icon)

class graphics():
    #Holds the methods for loading/displaying graphics
    def load_images(self):
        #Loads the background and sprite images
        self.background_image=pygame.image.load("images/flappy_background.png").convert()
        self.bird_image=pygame.image.load("images/flappy_sprite.jpg").convert()
        self.pipe_image=pygame.image.load("images/flappy_pipe.png").convert()
        self.pipe_image.set_colorkey(white)
        self.inverted_pipe_image=pygame.transform.flip(self.pipe_image,False,True)
        self.bird_image.set_colorkey(white)

    def display_pipe(self,pipe_xPos,pipe_height):
        #Calculates new position of pipe
        pipe_yPos=318
        pipe_vX=-1
        inverted_pipe_yPos=0
        #Checks if there is a pre-existing velocity and xPosition
        #and assigns defaults
        if pipe_xPos==None or pipe_xPos<=-78:
            pipe_xPos=500
            pipe_height=random.randrange(0,3)
        pipe_xPos+=pipe_vX
        #Randomizes the height of the pipes
        if pipe_height==0:
            self.inverted_pipe_image=pygame.transform.scale(self.inverted_pipe_image,(self.inverted_pipe_image.get_width(),200))
        elif pipe_height==1:
            self.inverted_pipe_image=pygame.transform.scale(self.inverted_pipe_image,(self.inverted_pipe_image.get_width(),150))
        elif pipe_height==2:
            self.inverted_pipe_image=pygame.transform.scale(self.inverted_pipe_image,(self.inverted_pipe_image.get_width(),100))

        screen.blit(self.pipe_image,[pipe_xPos,pipe_yPos])
        screen.blit(self.inverted_pipe_image,[pipe_xPos,0])

        return pipe_height

    def display_loop(self,bird_vY,bird_yPos,pipe_xPos,pipe_height):
        #Calculates new position of bird
        bird_xPos=200
        jumpspeed=-1.7
        fallspeed=0.02
        #Checks if there is a pre-existing velocity and yPosition
        #and assigns defaults
        if bird_vY==None or bird_yPos==None:
            bird_vy=0
            bird_yPos=200
        for event in pygame.event.get():
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_UP:
                    bird_vY=jumpspeed
        bird_vY+=fallspeed
        bird_yPos+=bird_vY

        #Blits all the images to the screen
        screen.blit(self.background_image,[0,0])
        screen.blit(self.bird_image,[bird_xPos,bird_yPos])

        pipe_xPos,pipe_height=self.display_pipe(pipe_xPos,pipe_height)

        pygame.display.flip()

        return bird_vY,bird_yPos,pipe_xPos,pipe_height

class titleScreen():
    #Holds the methods for the title screen/menu
    def title(self):
        #Different fonts
        titleFont=pygame.font.SysFont("verdana",30,bold=True,italic=True)
        startFont=pygame.font.SysFont("verdana",25,bold=False,italic=False)
        quitFont=pygame.font.SysFont("verdana",25,bold=False,italic=False)
        #Sets up the title
        titleText="Flappy Game"
        titlePos=(0,0)
        renderTitle=titleFont.render(titleText,1,blue,gray_bgColor)
        titlex,titley=titleFont.size(titleText)
        screen.blit(renderTitle,titlePos)
        #Sets up the start button
        startText="Start Game"
        startPos=(0,titley)
        renderStart=startFont.render(startText,1,blue,gray_bgColor)
        startx,starty=startFont.size(startText)
        self.start_rect = pygame.Rect(startPos[0],titley,startx,starty)
        screen.blit(renderStart,startPos)
        #Sets up the quit button
        quitText="Quit"
        quitPos=(0,starty+titley)
        renderQuit=quitFont.render(quitText,1,red,gray_bgColor)
        quitx,quity=quitFont.size(quitText)
        self.quit_rect = pygame.Rect(quitPos[0],titley+starty,quitx,quity)
        screen.blit(renderQuit,quitPos)

    def get_click(self):
        #Gets mouse click and processes outcomes
        for event in pygame.event.get():
            if event.type==pygame.MOUSEBUTTONDOWN:
                x,y=pygame.mouse.get_pos()
                #Tests for start:
                if self.start_rect.collidepoint(x,y):
                    print("start")
                    return True
                elif self.quit_rect.collidepoint(x,y):
                    print("quit")
                    sys.exit()
                else:
                    return False

#Assign objects to respective classes        
titleC=titleScreen()
graphicsC=graphics()

def showTitle():
    #bundles all title_screen functions
    screen.blit(graphicsC.background_image,[0,0])
    titleC.title()
    pygame.display.flip()

def main():
    graphicsC.load_images()
    while True:
        title=True
        while title==True:
            showTitle()
            start=titleC.get_click()
            if start==True:
                title=False
                bird_yPos=200
                bird_vY=0
                pipe_xPos=500
                pipe_height=1
        while title==False:
            bird_vY,bird_yPos,pipe_xPos,pipe_height=graphicsC.display_loop(bird_vY,bird_yPos,pipe_xPos,pipe_height)
            pygame.time.delay(3)
            if bird_yPos>=height-120:
                title=True            
main()

错误:

Traceback (most recent call last):
  File "C:\Users\Klaus\Documents\coding\python stuff\pygames\Flappy Python\flappy_bird_source.py", line 162, in <module>
    main()
  File "C:\Users\Klaus\Documents\coding\python stuff\pygames\Flappy Python\flappy_bird_source.py", line 158, in main
    bird_vY,bird_yPos,pipe_xPos,pipe_height=graphicsC.display_loop(bird_vY,bird_yPos,pipe_xPos,pipe_height)
  File "C:\Users\Klaus\Documents\coding\python stuff\pygames\Flappy Python\flappy_bird_source.py", line 85, in display_loop
    pipe_xPos,pipe_height=self.display_pipe(pipe_xPos,pipe_height)
TypeError: 'int' object is not iterable

1 个答案:

答案 0 :(得分:2)

display_pipe()返回一个整数。

您正在尝试将pipe_xPos,pipe_height指定给返回值,就好像它是一个包含两个元素的元组。

如果您想从pipe_xPos返回pipe_heightdisplay_pipe()的值,请将返回行更改为:

return pipe_xPos, pipe_height