2017-05-24 61 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