简体   繁体   中英

Traceback Error (Can Only Concatenate Tuple)

I've just started working with Python and I wanted to program a small game, where you can move a small character around with the wasd keys, but this error keeps occurring:

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

Here is my code:

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()

Here x = (display_width*0,45) , should be x = display_width * 0.45 . Because by doing (display_width*0,45) you are creating a tuple like (0,45) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM