只能将元组(不是“int”)连接到元组

时间:2017-12-09 09:53:26

标签: python python-3.x pygame

它不会像它应该的那样移动,它说:

  

x + = x_change   TypeError:只能将元组(不是“int”)连接到元组

代码在这里:

import pygame

pygame.init()

display_width = 800
display_height = 600

green = (30,255,50)

gameDisplay = pygame.display.set_mode((display_width,display_height))

pygame.display.set_caption("testV1.0")

clock = pygame.time.Clock()

CharacterImg = pygame.image.load("Character.png")
def Character(x,y):
    gameDisplay.blit(CharacterImg,(x,y))

x = (display_width * 0,45)
y = (display_height * 0,8)

x_change = 0

hit = False

while not hit:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            hit = True

            if event.type == pygame.KEYDOWN:
                if pygame.key == K_LEFT:
                    x_change += -50
                if pygame.key == K_RIGHT:
                    x_change += 50

            if event.type == pygame.KEYUP:
                if pygame.key == K_LEFT:
                    x_change += 50
                if pygame.key == K_RIGHT:
                    x_change += -50

            x += x_change



        print(event)

    gameDisplay.fill(green)
    Character(x,y)
    pygame.display.update()
    clock.tick(30)

pygame.quit()
quit()

2 个答案:

答案 0 :(得分:2)

逗号创建元组,因此x和y都是元组。如果你想要浮点数,你可以使用一个点(你也不需要括号):

@EnableCaching
@Configuration
public class CacheConfiguration {

    public static final String CACHE_NAME = "cache";

    @Bean
    public CacheManager cacheManager() {
          ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager(CACHE_NAME);
         return cacheManager;
    }

    @CacheEvict(allEntries = true, value = CACHE_NAME)
    @Scheduled(fixedDelay = 10* 60 * 1000 ,  initialDelay = 500)
    public void evictCache() {}
}

此外,在事件循环中,您必须将x = display_width * 0.45 y = display_height * 0.8 替换为pygame.key

event.key可能应该在while循环中而不是在事件循环中。

你必须对这个区块说一次:

x += x_change

答案 1 :(得分:0)

对于python,您在代码中使用错误的小数点分隔符 ,,应为.

<强>为

x = (display_width * 0,45)
y = (display_height * 0,8)

不可

x = (display_width * 0.45)
y = (display_height * 0.8)