Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python类型错误:';int';尽管赋值正确,对象仍不可iterable_Python_Methods_Int_Typeerror - Fatal编程技术网

Python类型错误:';int';尽管赋值正确,对象仍不可iterable

Python类型错误:';int';尽管赋值正确,对象仍不可iterable,python,methods,int,typeerror,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

当我运行这个程序时,当它到达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()
''这是一个简单的flappy bird复制品,是我用来帮助我的电脑
理解python
随机输入
导入pygame
从pygame导入*
输入数学
导入系统
#窗口预设
尺寸=宽度,高度=500500
Ag=-9.80665
clock=pygame.time.clock()
白色=(255255)
蓝色=(0,0255)
红色=(255,0,0)
灰色(190193212)
#将pygame表面初始化为屏幕
pygame.init()
pygame.font.init()
#为窗口创建图标
screen=pygame.display.set_模式(大小)
pygame.display.set_标题(“Flappy Bird复制品”)
icon=pygame.image.load(“images/icon.png”).convert()
pygame.display.set_图标(图标)
类图形():
#保存加载/显示图形的方法
def加载_图像(自):
#加载背景和精灵图像
self.background\u image=pygame.image.load(“images/flappy\u background.png”).convert()
self.bird\u image=pygame.image.load(“images/flappy\u sprite.jpg”).convert()
self.pipe\u image=pygame.image.load(“images/flappy\u pipe.png”).convert()
self.pipe\u image.set\u颜色键(白色)
self.inversed\u pipe\u image=pygame.transform.flip(self.pipe\u image,False,True)
self.bird_image.set_colorkey(白色)
def显示管道(自身、管道xPos、管道高度):
#计算管道的新位置
管道直径=318
管道_vX=-1
倒置管道直径=0
#检查是否存在预先存在的速度和位置
#并指定默认值
如果管道\u xPos==无或管道\u xPos=高度-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
回溯(最近一次呼叫最后一次):
文件“C:\Users\Klaus\Documents\coding\python stuff\pygames\Flappy python\Flappy\u bird\u source.py”,第162行,在
main()
文件“C:\Users\Klaus\Documents\coding\python-stuff\pygames\Flappy-python\Flappy\u bird\u source.py”,第158行,主目录
bird_vY,bird_yPos,pipe_xPos,pipe_height=图形显示循环(bird_vY,bird_yPos,pipe_xPos,pipe_height)
文件“C:\Users\Klaus\Documents\coding\python stuff\pygames\Flappy python\Flappy\u bird\u source.py”,第85行,在display\u循环中
pipe\u xPos,pipe\u height=self.display\u pipe(pipe\u xPos,pipe\u height)
TypeError:“int”对象不可编辑
display\u pipe()
返回一个整数

您试图将
pipe\u xPos,pipe\u height
分配给返回值,就好像它是一个包含两个元素的元组一样

如果要从
display\u pipe()
返回
pipe\u xPos
pipe\u height
的值,请将返回行更改为:

return pipe_xPos, pipe_height

请给我们异常的完整回溯。在结尾添加了它。那么这是否意味着该方法只能返回一个值?@Andrewallis不,它可以返回您想要的任何值,但您需要告诉它这样做。谢谢。这似乎解决了问题。