当前位置: 首页 > 知识库问答 >
问题:

PyGame元组表面错误

唐阳飇
2023-03-14

这是我在愤怒的小鸟仿制品中绘制管道的一个函数:

def drawPipes():
#Pipes 1
    if pipes1 == 1:
        topPipe1Y =300
        botPipe1Y =400
        screen.blit( topL, ( col1X, 0))
        screen.blit( botS, ( col1X, 400)) <-----This part keeps getting the error.

    elif pipes1 == 2:
        topPipe1Y = 200
        botPipe1Y = 300
        screen.blit( topM, ( col1X, 0))
        screen.blit( botM, ( col1X, 300)) <-----This part keeps getting the error.

    elif pipes1 == 3:
        topPipe1Y = 100
        botPipe1Y = 200
        screen.blit( topS, ( col1X, 0))
        screen.blit( botL, ( col1X, 200)) <-----This part keeps getting the error.

#Pipes 2
    if pipes2 == 1:
        topPipe2Y =300
        botPipe2Y =400
        screen.blit( topL, ( col2X, 0))
        screen.blit( botS, ( col2X, 400)) <-----This part keeps getting the error.

    elif pipes2 == 2:
        topPipe2Y =200
        botPipe2Y =300
        screen.blit( topM, ( col2X, 0))
        screen.blit( botM, ( col2X, 300)) <-----This part keeps getting the error.

    elif pipes2 == 3:
        topPipe2Y =100
        botPipe2Y =200
        screen.blit( topS, ( col2X, 0))
        screen.blit( botL, ( col2X, 200)) <-----This part keeps getting the error.

#Pipes 3
    if pipes3 == 1:
        topPipe2Y =300
        botPipe2Y =400
        screen.blit( topL, ( col3X, 0))
        screen.blit( botS, ( col3X, 400)) <-----This part keeps getting the error.

    elif pipes3 == 2:
        topPipe2Y =200
        botPipe2Y =300
        screen.blit( topM, ( col3X, 0))
        screen.blit( botM, ( col3X, 300)) <-----This part keeps getting the error.

    elif pipes3 == 3:
        topPipe2Y =100
        botPipe2Y =200
        screen.blit( topS, ( col3X, 0))
        screen.blit( botL, ( col3X, 200)) <-----This part keeps getting the error.

我的问题是,当我试图运行我的程序时,我得到一个错误

TypeError:参数1必须是pygame.Surface,而不是元组。

我不知道该做什么,有时我能运行它,有时我不能。我的表面被称为屏幕,我在其他多个程序中使用了相同的语法,我使用了pyplay,但我从来没有遇到过这个问题。如果有人能帮我,我将不胜感激。如果你需要看到我的整个程序,它在这里:

"""-------------------------------------------------------------------------------
Name:        module1
Purpose:

Author:      Nano
Created:     18/05/2014
Copyright:   (c) Nano 2014
Licence:     Open Source
----------------------------------------------------------------------------------
Notes/ Checklist:
   Turn off option to turn on and off printing.
----------------------------------------------------------------------------------"""
import pygame, sys, math, random, time, datetime
from pygame.locals import *
from datetime import timedelta

pygame.init()
fpsClock = pygame.time.Clock()
fps = 60
screenWid = 750
screenLen = 500
resolution = ( screenWid, screenLen)
pygame.display.set_caption('Flappy Bird')
pygame.display.init()
screen = pygame.display.set_mode((resolution))

printing = True

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
yellow = (255,255,0)
cyan = (0,255,255)
purple = (255,0,255)
lightTurquoise = (153,217,234)
bgColor = (lightTurquoise)

#--------------------------------------Pipes--------------------------------------
topL = pygame.image.load('TopPipe2.png')
topM = pygame.image.load('TopPipe1.png')
topS = pygame.image.load('TopPipe3.png')
botL = pygame.image.load('BottomPipe2.png')
botM = pygame.image.load('BottomPipe1.png')
botS = pygame.image.load('BottomPipe3.png')

sTopX = 100
mTopX = 200
lTopX = 300

sBotX = screenLen-100
mBotX = screenLen-200
lBotX = screenLen-300

col1Start = screenWid
col2Start = col1Start+277
col3Start = col2Start + 277

col1X = col1Start
col2X = col2Start
col3X = col3Start

pipes1=0
pipes2=0
pipes3=0


pipes1 = random.randint(1,3)
pipes2 = random.randint(1,3)
pipes3 = random.randint(1,3)

topPipe1Y = 0
botPipe1Y = 0

topPipe2Y = 0
botPipe2Y = 0

topPipe3Y = 0
botPipe3Y = 0


"""def drawPipes():
#Pipes 1
   if pipes1 == 1:
       topPipe1Y =300
       botPipe1Y =400
       screen.blit( topL, ( col1X, 0))
       screen.blit( botS, ( col1X, 400))

   elif pipes1 == 2:
       topPipe1Y = 200
       botPipe1Y = 300
       screen.blit( topM, ( col1X, 0))
       screen.blit( botM, ( col1X, 300))

   elif pipes1 == 3:
       topPipe1Y = 100
       botPipe1Y = 200
       screen.blit( topS, ( col1X, 0))
       screen.blit( botL, ( col1X, 200))

#Pipes 2
   if pipes2 == 1:
       topPipe2Y =300
       botPipe2Y =400
       screen.blit( topL, ( col2X, 0))
       screen.blit( botS, ( col2X, 400))

   elif pipes2 == 2:
       topPipe2Y =200
       botPipe2Y =300
       screen.blit( topM, ( col2X, 0))
       screen.blit( botM, ( col2X, 300))

   elif pipes2 == 3:
       topPipe2Y =100
       botPipe2Y =200
       screen.blit( topS, ( col2X, 0))
       screen.blit( botL, ( col2X, 200))

#Pipes 3
   if pipes3 == 1:
       topPipe2Y =300
       botPipe2Y =400
       screen.blit( topL, ( col3X, 0))
       screen.blit( botS, ( col3X, 400))

   elif pipes3 == 2:
       topPipe2Y =200
       botPipe2Y =300
       screen.blit( topM, ( col3X, 0))
       screen.blit( botM, ( col3X, 300))

   elif pipes3 == 3:
       topPipe2Y =100
       botPipe2Y =200
       screen.blit( topS, ( col3X, 0))
       screen.blit( botL, ( col3X, 200))"""

def colisions(x,y,columnx,topPipeY,botPipeY):
    if x>=columnx and x + 51 <= columnx:
        if y >=topPipeY or y<= botPipeY:
            print("top pipe 1")
            movement = 'none'
            birdy = int(screenLen/2)
            if score > highScore:
                highScore = score
            score = 0
            speed = 0
            col1X = col1Start
            col2X = col2Start
            col3X = col3Start
            print("Oops")

score = 0
bird = pygame.image.load('FlappyBird.png')
bgImage = pygame.image.load('BackDrop.png')
birdx = 250
birdy = screenLen / 2
birdWid = 51
birdLen = 36
font = pygame.font.SysFont( "Times New Roman, Arial", 20)
scoreBoard = font.render("Score:", True, black)
scoreBoardPos = (5,5)
highScoreText = font.render("High Score:", True, black)
scorePos = (65,5)
movement = 'none'
goingUp = 0
highScore = 0
orgSpeed = 2
speed = 0
two = 200
three = 300
four = 400


"""================================Main Game Loop================================="""
while True:
    topR = birdx+birdWid,birdy
    botL = birdx, birdy+birdLen
    botR = birdx+birdWid ,birdy+birdLen
    if col1X <= -82:
        col1X = screenWid
        pipes1 = random.randint(1,3)
    if col2X <= -82:
        col2X = screenWid
        pipes2 = random.randint(1,3)
    if col3X <= -82:
        col3X = screenWid
        pipes3 = random.randint(1,3)
    col1X-=speed
    col2X-=speed
    col3X-=speed
    scoreAmount = font.render(str(score),True, black)
    highScoreNum = font.render(str(highScore),True, black)
    screen.fill(bgColor)
    screen.blit(bgImage,(0,0))
#-----------------------------------Draws Pipes------------------------------------
##Pipes 1
    if pipes1 == 1:
        topPipe1Y =300
        botPipe1Y =400
        screen.blit( topL, ( col1X, 0))
        screen.blit( botS, ( col1X, four))

    elif pipes1 == 2:
        topPipe1Y = 200
        botPipe1Y = 300
        screen.blit( topM, ( col1X, 0))
        screen.blit( botM, ( col1X, three))

    elif pipes1 == 3:
        topPipe1Y = 100
        botPipe1Y = 200
        screen.blit( topS, ( col1X, 0))
        screen.blit( botL, ( col1X, two))

##Pipes 2
    if pipes2 == 1:
        topPipe2Y =300
        botPipe2Y =400
        screen.blit( topL, ( col2X, 0))
        screen.blit( botS, ( col2X, four))

    elif pipes2 == 2:
        topPipe2Y =200
        botPipe2Y =300
        screen.blit( topM, ( col2X, 0))
        screen.blit( botM, ( col2X, three))

    elif pipes2 == 3:
        topPipe2Y =100
        botPipe2Y =200
        screen.blit( topS, ( col2X, 0))
        screen.blit( botL, ( col2X, three))

##Pipes 3
    if pipes3 == 1:
        topPipe2Y =300
        botPipe2Y =400
        screen.blit( topL, ( col3X, 0))
        screen.blit( botS, ( col3X, four))

    elif pipes3 == 2:
        topPipe2Y =200
        botPipe2Y =300
        screen.blit( topM, ( col3X, 0))
        screen.blit( botM, ( col3X, three))

    elif pipes3 == 3:
        topPipe2Y =100
        botPipe2Y =200
        screen.blit( topS, ( col3X, 0))
        screen.blit( botL, ( col3X, two))

    screen.blit( bird, (birdx,birdy))
    screen.blit( scoreBoard, scoreBoardPos)
    screen.blit(scoreAmount, scorePos)
    screen.blit(highScoreText,(5,45))
    screen.blit(highScoreNum, (105,45))
    if birdx == col1X:
        score +=1
    if birdx == col2X:
        score +=1
    if birdx == col3X:
        score +=1
    for event in pygame.event.get():

        if event.type == MOUSEBUTTONDOWN:
            speed = orgSpeed
            goingUp+=10
            if goingUp > 30:
                goingUp = 30
            movement = 'up'

##Required for closing game
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    if movement == 'up':
        if birdy <= 0:
            birdy = 2
            goingUp = 0
            movement = 'down'
        birdy-=4
        goingUp-=1
        if goingUp == 0:
            movement = 'down'
            goingUp = 0
    if movement == 'down':
        birdy+=4
        if birdy >= screenLen or birdy+36 >= screenLen:
            movement = 'none'
            birdy = int(screenLen/2)
            if score > highScore:
                highScore = score
            score = 0
            speed = 0
            col1X = col1Start
            col2X = col2Start
            col3X = col3Start

#------------------------------Colission Conditions-------------------------------
    colisions(birdx,birdy,col1X,topPipe1Y,botPipe1Y)
    colisions(birdx,birdy,col2X,topPipe2Y,botPipe2Y)
    colisions(birdx,birdy,col3X,topPipe3Y,botPipe3Y)

    if birdx>=col1X and birdx + 51 <= col1X:
        if birdy >=topPipe1Y or birdy<= botPipe1Y:
            print("top pipe 1")
            movement = 'none'
            birdy = int(screenLen/2)
            if score > highScore:
                highScore = score
            score = 0
            speed = 0
            col1X = col1Start
            col2X = col2Start
            col3X = col3Start
            print("Oops")

    pygame.display.update()
    fpsClock.tick(fps)

共有1个答案

盖和洽
2023-03-14

我相信这与代码中的这些行有很大关系(仍然不知道botM到底出了什么问题,但想想你在哪里重新分配它):

topR = birdx+birdWid,birdy # note these now become tuples with two integers
botL = birdx, birdy+birdLen
botR = birdx+birdWid ,birdy+birdLen

我不知道Pygame对象的易变性有多大,但是一些全局变量可以在没有global关键字的情况下编写。您当前正在将它们更改为元组。

 类似资料:
  • 我在pygame中有以下问题。我使用,但当我尝试调用 它说:

  • 我正在用PyGame创建一个游戏。在那里我想创造负面效果,所有的屏幕都变成负面。 这对于像rects这样的简单形状来说非常容易:(并使用而不是) 但我不知道如何在图像上产生这种效果。我试着做负片图像复制,但若这种效果不是即时的,会逐渐填满屏幕,那个么这将并没有帮助。

  • 问题内容: 我需要将列表排序为: 我已经使用了该函数,并且能够根据值进行排序…但是我无法按字母顺序进行排序,因为我需要按照数字的降序和字母的升序对列表进行排序数字具有相同的值。 问题答案: 使用元组作为浮点数为负的排序键可以反转顺序: 如果您不能进行求反(例如对字符串或字母值或非数字值),则可以利用Python sort函数稳定的事实,并分两步进行排序:

  • 我必须将元组与元组列表进行比较,如果整数小于列表中的任何元组,则返回True。例如,如果我有将返回True,因为单独元组(“番茄”,10,5)中的整数比列表中的元组(“橙色”,11,6)小,但是如果我有将返回False。 我试试这个 但不工作时,它应该返回假,我不知道为什么? 注意:字符串对于这个问题并不重要,我必须忽略它。

  • 问题内容: 我有一个这样的(标签,计数)元组列表: 由此,我想对所有具有相同标签的值求和(相同的标签始终相邻),并以相同的标签顺序返回列表: 我知道我可以用以下方法解决它: 但是,有没有更Pythonic /优雅/有效的方法来做到这一点? 问题答案: 可以做你想做的:

  • 当鼠标不悬停在游戏中时,我试图使游戏中的用户界面透明。但出于某种原因,当我设置图像的alpha值使其变为透明时,什么也没有发生。下面是一些可运行的代码,它复制了问题: 任何帮助都非常感谢!编辑:我收到了一个评论,有人说他们使用了自己的图像,效果很好...当我执行程序时,我会收到这个警告: 是因为我的文件,所以不能正常运行的原因吗?