微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Blit的目标位置无效

如何解决Blit的目标位置无效

当我在python中输入此代码时,它会产生一个错误,指出在代码中找到无效的blit目标位置:display.blit(grass_image,(x*16),(y*16)) #display grass image with it resolutions

有什么建议吗?

import pygame,sys #Imports pygame and sys into code

clock = pygame.time.Clock() #Sets clock

from pygame.locals import * #Imports the pygame modules
pygame.init() #Creates Pygame

pygame.display.set_caption("Platformer") #Sets the window name

window_size = (400,400) #Size of Window

screen = pygame.display.set_mode(window_size,32) # Creates Window

player_image = pygame.image.load('PLAYER.png').convert() #Loads in player image
player_image.set_colorkey((255,255,255)) #Makes Transparent Image

display = pygame.Surface((300,200)) #Used for rendering objects in the game_map

grass_image = pygame.image.load('Grass.jpg') #Loads in Grass Image
dirt_image = pygame.image.load('Dirt.png') #Loads in Dirt Image

moving_left = False #Sets moving_left
moving_right = False #Sets moving_right

player_location = [0,0] #Sets the player location

vertical_movement = 0 #Sets the vertical value of the player

game_map =  [['0','0','0'],['0','2',#Game Map
             ['0',['2','2'],['1','1','1'],'1']]

player_rect = pygame.Rect(player_location[0],player_location[1],player_image.get_width(),player_image.get_height()) #Player Collision Box

def collision_test(rect,tiles):
    hit_list = []
    for tile in tiles:
        if rect.colliderect(tile):     #Testing if a player collides into a tile
            hit_list.append(tile)
    return hit_list

def move(rect,movement,tiles): #Player Collisions
    collision_types = ('top' == False,'bottom' == False,'left' == False,'right' == False) #Sets types of collisions the player has
    rect.x += movement[0] #Sets player's x value
    hit_list = collision_test(rect,tiles) #Lists what the player can collide into
    for tile in hit_list:
        if movement[0] > 0: #If player's x value is less than 0 (Moving to the left)
            rect.right = tile.left #Player moves to the left
            collision_types['right'] = True #Sets 'right' collision to True
        elif movement[0] < 0: #If player's x value is more than 0 (Moving to the right)
            rect.left = tile.right #Player moves to the right
            collision_types['left'] = True #Sets 'left' collision to True
    rect.y += movement[1] #Sets players y value
    hit_list = collision_test(rect,tiles) #Lists what the player can collide to
    for tile in hit_list:
        if movement[1] > 0: #If player's y value is less than 0 (Going down)
            rect.bottom = tile.top #Player moves down
            collision_types['bottom'] = True #Sets 'bottom' collision to True
        elif movement[1] < 0: #If player's y value is more than 0 (Going up)
            rect.top = tile.bottom #Player Moves up
            collision_types['top'] = True #Sets 'top' collision to true
    return rect,collision_types #Returns the values of rect AND collision types

while True: #Game Loop

    y = 0
    x = 0
    for layer in game_map:
        for tile in layer:
            if tile == '1': #If game_map has 1
                display.blit(dirt_image,(y*16)) #display dirt image with its resolutions
            if tile == '2': #If game map has 2
                display.blit(grass_image,(y*16)) #display grass image with it resolutions
            x += 1 #Adds 1 to x
        y += 1 #Adds 1 to y
    
    screen.fill((146,223,225))#Background Colour
    
    screen.blit(player_image,player_location)#Player Image Location On Screen

    player_movement = [0,0] #Sets Value of the player's movement
    if moving_right == True: #If user presses the rightkey
        player_movement[0] += 2 #Player moves right
    if moving_left == True: #If user presses the leftkey
        player_movement[0] -= 2 #Player moves left
    player_movement[1] += vertical_movement
    vertical_movement += 0.2
    if vertical_movement > 3:       #Vertical Movement
        vertical_movement = 3

    player_rect.x = player_location[0] #Horozontal Collision
    player_rect.y = player_location[1] #Vertical Collision
    
    for event in pygame.event.get(): #EVENT LOOP
        
        if event.type == QUIT: #If user pressed user quit
            pygame.quit() #Stops pygame
            sys.exit() #Stops the script
            
        if event.type == KEYDOWN: #If user presses down on a key
            if event.key == K_RIGHT: #If user presses the rightkey
                moving_right = True #Player moves right
            if event.key == K_LEFT: #If user presses the leftkey
                moving_left = True #Player moves left
            if event.key == K_UP: #If player presses down on the upkey
                vertical_movement = -5 #Player jumps up
                
        if event.type == KEYUP: #If user does not press down on a key
            if event.key == K_RIGHT: #If user does not press the rightkey
                moving_right = False #Player does not move right
            if event.key == K_LEFT: #If user does not press the leftkey
                moving_left = False #Player does not move left

                
    screen.blit(pygame.transform.scale,(display,window_size),(0,0)) #Scales the game_map to the screen
    pygame.display.update() #Updates display
    clock.tick(60) #60 FPS

解决方法

pygame.Surface.blit的_dest _argument必须是一个具有2个组成部分的元组,代表位置:

display.blit(grass_image,(x*16),(y*16))

display.blit(grass_image,(x*16,y*16) )

或者, dest 参数可以是具有四个组成部分的y元组,代表一个矩形。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?