2017-05-24 53 views
0

我剛開始使用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

這裏x = (display_width*0,45),應該是x = display_width * 0.45。因爲通過執行(display_width*0,45)您正在創建一個元組,如(0,45)

+0

非常感謝您! – arno

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 
+0

謝謝!對不起,這是一個愚蠢的問題 – arno