回溯错误(只能连接元组)

时间:2017-05-24 16:00:20

标签: python pygame

我刚刚开始使用Python,我想编写一个小游戏,你可以用w a s d键移动一个小角色,但这个错误一直在发生:

Traceback (most recent call last):
  File "/home/username/Desktop/Python-project/Game/Game.py", line 53, in <module>
x+=x_change
TypeError: can only concatenate tuple (not "int") to tuple

这是我的代码:

import pygame
import os

pygame.init()

display_hight = 800
display_width = 1000

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

gameDisplay = pygame.display.set_mode((display_width,display_hight))
pygame.display.set_caption("ZOMPS")

clock = pygame.time.Clock()

mydir = os.path.dirname('/home/arne/Desktop/Python-project/Game/Demonsave.png')
demonImg = pygame.image.load(os.path.join(mydir,'Demonsave.png'))
demonImg = pygame.transform.scale(demonImg,(140,160)) 

def demon(x,y):
    gameDisplay.blit(demonImg,(x,y))

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

x_change=0
y_change=0

dead = False
while not dead:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            dead=True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                x_change-=5             
            elif event.key == pygame.K_d:
                x_change+=5
            elif event.key == pygame.K_w:
                y_change-=5
            elif event.key == pygame.K_s:
                y_change+=5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a or pygame.K_d:
                x_change=0
            elif event.key == pygame.K_w or pygame.K_s:
                y_change=0  

        x+=x_change
        y+=y_change


    gameDisplay.fill(red)

    demon(x,y)

    pygame.display.update() 

     clock.tick(60)

pygame.quit()

quit()

2 个答案:

答案 0 :(得分:2)

此处x = (display_width*0,45)应为x = display_width * 0.45。因为通过(display_width*0,45)来创建像(0,45)这样的元组。

答案 1 :(得分:0)

使用:

x = (display_width*0.45) # decimal point is '.' not ','
y = (display_hight*0.8) # ',' is to separate list and tuple and function ..., elements, in your case, you are using (), so it's a tuple you're creating