Python 为什么我会出错?Pygame blits 导入系统、pygame、数学、随机 pygame.init() 尺寸=宽度,高度=1000720 x=0 y=0 房屋1x=500 住宅1y=360 豪斯2x=1470 住宅2Y=360 rock1x=-100 rock1y=300 scrollx=0 scrolly=0 screen=pygame.display.set_模式(大小,pygame.DOUBLEBUF) 计数器=0 playercollideRect=pygame.rect.rect((0,0)、(75,25)) house1collideRect=pygame.rect.rect((0,0)、(870605)) house2collideRect=pygame.rect.rect((0,0)、(870605)) rock1collideRect=pygame.rect.rect((0,0)、(140100)) 冲突=错误 lastkey='向下' animationdirection=‘向下’ playerimage=pygame.image.load(“img/player/player0.png”).convertu alpha() playerimage=playerimage.get_rect() house1image=pygame.image.load(“img/structures/house1.png”).convertu alpha() house1image=house1image.get_rect() house2image=pygame.image.load(“img/structures/house2.png”).convertu alpha() house2imagerect=house2image.get_rect() rock1image=pygame.image.load(“img/objects/rock1.png”).convert_alpha() rock1imagerect=rock1image.get_rect() clock=pygame.time.clock() 屏幕颜色=0,155,20 gamefont=pygame.font.SysFont(“arial”,30) playerImationDown=['img/player/player1.png'、'img/player/player0.png'、'img/player/player2.png'、'img/player0.png'] playerImationUp=['img/player/playerback1.png'、'img/player/playerback0.png'、'img/player/playerback2.png'、'img/playerback0.png'] playerImationLeft=['img/player/playerleft1.png'、'img/player/playerleft0.png'、'img/player/playerleft2.png'、'img/playerleft0.png'] playerImationRight=['img/player/playerright1.png'、'img/player/playerright0.png'、'img/playerright2.png'、'img/playerright0.png'] pygame.mixer.music.load('audio/music/ambient.mp3') pygame.mixer.music.set_endevent(pygame.constants.USEREVENT) pygame.mixer.music.play() pygame.display.set_标题('Game') pygame.event.set_允许([pygame.QUIT,pygame.KEYDOWN,pygame.keydup]) 碰撞器=[house1collideRect,house2collideRect,rock1collideRect] blitrects=[house1imagerect,house2imagerect,rock1imagerect] blitimages=[house1image,house2image,rock1image] B位置=[rock1y,house1y,house2y] 而1: 时钟滴答作响(500) 对于pygame.event.get()中的事件: 如果event.type==pygame.QUIT:sys.exit() 如果y>720-75-100: y=720-75-100 滚动-=1 如果y1000-37-100: x=1000-37-100 scrollx-=1 如果x

Python 为什么我会出错?Pygame blits 导入系统、pygame、数学、随机 pygame.init() 尺寸=宽度,高度=1000720 x=0 y=0 房屋1x=500 住宅1y=360 豪斯2x=1470 住宅2Y=360 rock1x=-100 rock1y=300 scrollx=0 scrolly=0 screen=pygame.display.set_模式(大小,pygame.DOUBLEBUF) 计数器=0 playercollideRect=pygame.rect.rect((0,0)、(75,25)) house1collideRect=pygame.rect.rect((0,0)、(870605)) house2collideRect=pygame.rect.rect((0,0)、(870605)) rock1collideRect=pygame.rect.rect((0,0)、(140100)) 冲突=错误 lastkey='向下' animationdirection=‘向下’ playerimage=pygame.image.load(“img/player/player0.png”).convertu alpha() playerimage=playerimage.get_rect() house1image=pygame.image.load(“img/structures/house1.png”).convertu alpha() house1image=house1image.get_rect() house2image=pygame.image.load(“img/structures/house2.png”).convertu alpha() house2imagerect=house2image.get_rect() rock1image=pygame.image.load(“img/objects/rock1.png”).convert_alpha() rock1imagerect=rock1image.get_rect() clock=pygame.time.clock() 屏幕颜色=0,155,20 gamefont=pygame.font.SysFont(“arial”,30) playerImationDown=['img/player/player1.png'、'img/player/player0.png'、'img/player/player2.png'、'img/player0.png'] playerImationUp=['img/player/playerback1.png'、'img/player/playerback0.png'、'img/player/playerback2.png'、'img/playerback0.png'] playerImationLeft=['img/player/playerleft1.png'、'img/player/playerleft0.png'、'img/player/playerleft2.png'、'img/playerleft0.png'] playerImationRight=['img/player/playerright1.png'、'img/player/playerright0.png'、'img/playerright2.png'、'img/playerright0.png'] pygame.mixer.music.load('audio/music/ambient.mp3') pygame.mixer.music.set_endevent(pygame.constants.USEREVENT) pygame.mixer.music.play() pygame.display.set_标题('Game') pygame.event.set_允许([pygame.QUIT,pygame.KEYDOWN,pygame.keydup]) 碰撞器=[house1collideRect,house2collideRect,rock1collideRect] blitrects=[house1imagerect,house2imagerect,rock1imagerect] blitimages=[house1image,house2image,rock1image] B位置=[rock1y,house1y,house2y] 而1: 时钟滴答作响(500) 对于pygame.event.get()中的事件: 如果event.type==pygame.QUIT:sys.exit() 如果y>720-75-100: y=720-75-100 滚动-=1 如果y1000-37-100: x=1000-37-100 scrollx-=1 如果x,python,python-2.7,pygame,blit,Python,Python 2.7,Pygame,Blit,这是我正在处理的一个小项目的代码,但出于某种原因,我遇到了以下错误: import sys, pygame, math, random pygame.init() size = width, height = 1000,720 x = 0 y = 0 house1x = 500 house1y = 360 house2x = 1470 house2y = 360 rock1x = -100 rock1y = 300 scrollx = 0 scrolly = 0 screen =

这是我正在处理的一个小项目的代码,但出于某种原因,我遇到了以下错误:

import sys, pygame, math, random
pygame.init()

size = width, height = 1000,720
x = 0
y = 0


house1x = 500
house1y = 360

house2x = 1470
house2y = 360


rock1x = -100
rock1y = 300


scrollx = 0
scrolly = 0
screen = pygame.display.set_mode(size, pygame.DOUBLEBUF)
counter = 0


playercollideRect =  pygame.rect.Rect((0, 0), (75, 25))

house1collideRect = pygame.rect.Rect((0, 0), (870, 605))
house2collideRect = pygame.rect.Rect((0, 0), (870, 605))

rock1collideRect = pygame.rect.Rect((0, 0), (140, 100))



collision = False


lastkey = 'down'
animationdirection = 'down'



playerimage = pygame.image.load("img/player/player0.png").convert_alpha()
playerimagerect = playerimage.get_rect()


house1image = pygame.image.load("img/structures/house1.png").convert_alpha()
house1imagerect = house1image.get_rect()

house2image = pygame.image.load("img/structures/house2.png").convert_alpha()
house2imagerect = house2image.get_rect()


rock1image = pygame.image.load("img/objects/rock1.png").convert_alpha()
rock1imagerect = rock1image.get_rect()



clock = pygame.time.Clock()

screencolor = 0, 155, 20
gamefont = pygame.font.SysFont("arial", 30)

playeranimationdown = ['img/player/player1.png','img/player/player0.png','img/player/player2.png','img/player/player0.png']
playeranimationup = ['img/player/playerback1.png','img/player/playerback0.png','img/player/playerback2.png','img/player/playerback0.png']
playeranimationleft = ['img/player/playerleft1.png','img/player/playerleft0.png','img/player/playerleft2.png','img/player/playerleft0.png']
playeranimationright = ['img/player/playerright1.png','img/player/playerright0.png','img/player/playerright2.png','img/player/playerright0.png']


pygame.mixer.music.load('audio/music/ambient.mp3')
pygame.mixer.music.set_endevent(pygame.constants.USEREVENT)
pygame.mixer.music.play()
pygame.display.set_caption('Game')
pygame.event.set_allowed([pygame.QUIT, pygame.KEYDOWN, pygame.KEYUP])

colliders = [house1collideRect, house2collideRect, rock1collideRect]
blitrects = [house1imagerect, house2imagerect, rock1imagerect]
blitimages = [house1image, house2image, rock1image]
blitypositions = [rock1y, house1y, house2y]

while 1:

    clock.tick(500)

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

    if y > 720-75-100:
        y = 720-75-100
        scrolly -= 1
    if y < 0+75+100:
        y = 0+75+100
        scrolly += 1
    if x > 1000-37-100:
        x=1000-37-100
        scrollx -=1
    if x < 0+37+100:
        x = 0+37+100
        scrollx +=1


    for collider in colliders:
        if playercollideRect.colliderect(collider):
            if lastkey == 'left':
                x += 1
            if lastkey == 'right':
                x -= 1
            if lastkey == 'up':
                y += 1
            if lastkey == 'down':
                y -= 1
            collision = True
        else:
            collision = False


    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP] or keys[pygame.K_DOWN] or keys[pygame.K_LEFT] or keys[pygame.K_RIGHT]:
        if keys[pygame.K_UP]:
            y -= 1

            if not collision == True:
                playerimage = pygame.image.load(playeranimationup[int(math.floor(counter/50))]).convert_alpha()
                counter = (counter + 1) % 200
            lastkey = 'up'


        elif keys[pygame.K_DOWN]:
            y += 1
            if not collision == True:
                playerimage = pygame.image.load(playeranimationdown[int(math.floor(counter/50))]).convert_alpha()
                counter = (counter + 1) % 200
            lastkey = 'down'


        elif keys[pygame.K_RIGHT]:
            x += 1
            if not collision == True:
                playerimage = pygame.image.load(playeranimationright[int(math.floor(counter/50))]).convert_alpha()

                counter = (counter + 1) % 200
            lastkey = 'right'

        elif keys[pygame.K_LEFT]:
            x -= 1

            if not collision == True:
                playerimage = pygame.image.load(playeranimationleft[int(math.floor(counter/50))]).convert_alpha()

                counter = (counter + 1) % 200
            lastkey = 'left'




    playerimagerect.centerx = x
    playerimagerect.centery = y


    house1imagerect.centerx = house1x+scrollx
    house1imagerect.centery = house1y+scrolly

    house2imagerect.centerx = house2x+scrollx
    house2imagerect.centery = house2y+scrolly



    playercollideRect.midbottom = playerimagerect.midbottom

    house1collideRect.midbottom = house1imagerect.midbottom
    house2collideRect.midbottom = house2imagerect.midbottom

    rock1collideRect.midbottom = rock1imagerect.midbottom


    realx = x-scrollx
    realy = y-scrolly



    for image, imagerect, imageypos in blitimages, blitrects, blitypositions:
        if realy < imageypos:
            screen.blit(playerimage, playerimagerect)
            screen.blit(image, imagerect)
        if realy > imageypos:
            screen.blit(image, imagerect)
            screen.blit(playerimage, playerimagerect)

        if realy == imageypos:
            screen.blit(image, imagerect)
            screen.blit(playerimage, playerimagerect)





    label = gamefont.render(str('FPS: '+str(clock.get_fps())), 1, (255,255,0))
    screen.blit(label, (50, 50))

    pygame.display.flip()
    screen.fill(screencolor)
文件“C:\Users\Isaac\Desktop\python kick\game\game.py”,第186行,在
screen.blit(image,imagerect)
TypeError:blit的目标位置无效

对我的问题有什么见解吗?我已经测试和搜索了很长时间。

应该是
screen.blit(图像,(x坐标,y坐标))
您传递的是rect而不是坐标。如果您想使用rect中的坐标,它们是
self.rect.x
self.rect.y

您只是使用了
Screen().blit()
错误。第二个a
File "C:\Users\Isaac\Desktop\python kick\game\game.py", line 186, in <module>
screen.blit(image, imagerect)
TypeError: invalid destination position for blit