我正试图在python中制作一个像蛇一样的游戏。 我现在的问题是,我无法使用我的'硬币列表中定义的值'在我的for循环中。这是我执行时收到的错误:TypeError:' int'对象不可订阅。谢谢你的帮助。
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()
答案 0 :(得分:3)
你正在追加筹码列表(rx和ry是从10到790/590的随机整数),之后你试图访问coinlist中的元素,就好像它们是数组一样。
考虑做一些事情,比如替换
coinlist.append(rx)
coinlist.append(ry)
与
coinlist.append([rx,ry])