2016-10-16 1 views
0

파이썬에서 '뱀'같은 게임을 만들려고합니다. 내 문제는 지금 내 for-loop에서 'coinlist'의 정의 된 값을 사용할 수 없다는 것입니다. 이것은 내가 그것을 실행하면받는 오류입니다 : TypeError : 'int'객체는 subscriptable이 아닙니다. 당신의 도움을 주셔서 감사합니다.목록에서 정수 가져 오기 (파이 게임)

import pygame 
import random 

pygame.init() 

rot = (255,0,0) 
grün = (0,255,0) 
blau = (0,0,255) 
gelb = (255,255,0) 
schwarz = (0,0,0) 
weiß = (255,255,255) 

uhr = pygame.time.Clock() 

display = pygame.display.set_mode((800, 600)) 
pygame.display.set_mode((800, 600)) 
pygame.display.set_caption('Snake') 
display.fill(weiß) 


def arialmsg(msg, color, x, y, s): 
    header = pygame.font.SysFont("Arial", s) 
    text = header.render(msg, True, color) 
    display.blit(text, [x, y]) 

def mainloop(): 
    gameExit = False 
    start = False 
    movex = 400 
    movey = 300 
    changex = 0 
    changey = -2 
    rx = random.randrange(10, 790) 
    ry = random.randrange(10, 590) 
    snakelist = [] 
    snakelenght = 20 

    #coinlist defined here: 
    coinlist = [] 

    while start == False: 
     display.fill(schwarz) 
     arialmsg('Snake', grün, 350, 200, 25) 

     for event in pygame.event.get(): 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_RETURN: 
        start = True 

     pygame.display.flip() 

    #gameloop: 

    while gameExit == False: 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
      elif event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_DOWN: 
        changey = 2 
        changex = 0 
       elif event.key == pygame.K_UP: 
        changey = -2 
        changex = 0 
       elif event.key == pygame.K_RIGHT: 
        changex = 2 
        changey = 0 
       elif event.key == pygame.K_LEFT: 
        changex = -2 
        changey = 0 

     movex += changex 
     movey += changey 
     snakehead = [] 
     snakehead.append(movex) 
     snakehead.append(movey) 
     snakelist.append(snakehead) 

     display.fill(schwarz) 

     if len(coinlist) < 1: 
      rx = random.randrange(10, 790) 
      ry = random.randrange(10, 590) 
      coinlist.append(rx) 
      coinlist.append(ry) 

     for XY in snakelist: 
     pygame.draw.circle(display, grün, (XY[0], XY[1]), 10, 10) 



for-loop for the coinlist: 

     for coin in coinlist: 
     pygame.draw.rect(display, grün, (coin[0], coin[1], 10, 10)) 

     pygame.display.flip() 

     if snakelenght < len(snakelist): 
      del snakelist[:1] 

     if movex >= rx - 19 and movex <= rx + 19 and movey >= ry - 19 and movey <= ry + 19: 
      del coinlist[:1] 
      snakelenght += 10 

     uhr.tick(15) 
mainloop() 

pygame.quit() 
quit() 
+1

참고로

coinlist.append(rx) coinlist.append(ry) 

를 교체 같은 일을 생각해 보자. 'snake_color','fruit_color'를 선호하십시오. 나중에 마음이 바뀌면. – coredump

답변

3

당신은 나중에 당신은 그들이 배열이있는 것처럼 coinlist 내 요소에 액세스하려는에 (RX와 RY는 10 590분의 790에 임의의 int입니다) coinlist에의 int를 추가하고 있습니다.

는`등 rot`,`grün`는 반드시 변수에 대한 좋은 이름이 아니라는 것을

coinlist.append([rx,ry])