import pygame
import random
import sys
import time
from pygame.locals import *
black_colour = pygame.Color(0, 0, 0)
white_colour = pygame.Color(255, 255, 153)
red_colour = pygame.Color(255, 0, 0)
grey_colour = pygame.Color(150, 150, 150)
purple_colour = pygame.Color(204, 153, 255)
def GameOver(gamesurface):
GameOver_font = pygame.font.SysFont("MicrosoftYaHei", 16)
GameOver_colour = GameOver_font.render('Game Over', True, grey_colour)
GameOver_location = GameOver_colour.get_rect()
GameOver_location.midtop = (320, 10)
gamesurface.blit(GameOver_colour, GameOver_location)
pygame.display.flip()
time.sleep(5)
pygame.quit()
sys.exit()
def main():
pygame.init()
pygame.time.Clock()
ftpsClock = pygame.time.Clock()
gamesurface = pygame.display.set_mode((640, 480))
pygame.display.set_caption('贪吃蛇~')
snakeposition = [100, 100]
snakeposition_2 = [100, 120]
snakelength = [[100, 100], [80, 100], [60, 100]]
snakelength_2 = [[100, 120], [80, 120], [60, 120]]
square_purpose = [300, 300]
square_position = 1
derection = "right"
change_derection = derection
derection_2 = "right"
change_derection_2 = derection_2
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_RIGHT:
change_derection = "right"
if event.key == K_LEFT:
change_derection = "left"
if event.key == K_UP:
change_derection = "up"
if event.key == K_DOWN:
change_derection = "down"
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
if event.key == ord('d'):
change_derection_2 = 'right'
if event.key == ord('a'):
change_derection_2 = 'left'
if event.key == ord('w'):
change_derection_2 = 'up'
if event.key == ord('s'):
change_derection_2 = 'down'
if change_derection == 'left' and not derection == 'right':
derection = change_derection
if change_derection == 'right' and not derection == 'left':
derection = change_derection
if change_derection == 'up' and not derection == 'down':
derection = change_derection
if change_derection == 'down' and not derection == 'up':
derection = change_derection
if change_derection_2 == 'left' and not derection == 'right':
derection_2 = change_derection_2
if change_derection_2 == 'right' and not derection == 'left':
derection_2 = change_derection_2
if change_derection_2 == 'up' and not derection == 'down':
derection_2 = change_derection_2
if change_derection_2 == 'down' and not derection == 'up':
derection_2 = change_derection_2
if derection == 'left':
snakeposition[0] -= 20
if derection == 'right':
snakeposition[0] += 20
if derection == 'up':
snakeposition[1] -= 20
if derection == 'down':
snakeposition[1] += 20
if derection_2 == 'left':
snakeposition_2[0] -= 20
if derection_2 == 'right':
snakeposition_2[0] += 20
if derection_2 == 'up':
snakeposition_2[1] -= 20
if derection_2 == 'down':
snakeposition_2[1] += 20
snakelength.insert(0, list(snakeposition))
snakelength_2.insert(0, list[snakeposition_2])
if snakeposition[0] == square_purpose[0] and snakeposition[1] == square_purpose[1]:
square_position = 0
else:
snakelength.pop()
if snakeposition_2[0] == square_purpose[0] and snakeposition_2[1] == square_purpose[1]:
square_position = 0
else:
snakelength_2.pop()
if square_position == 0:
x = random.randrange(1, 32)
y = random.randrange(1, 24)
square_purpose = [int(x * 20), int(y * 20)]
square_position = 1
gamesurface.fill(black_colour)
for position in snakelength:
pygame.draw.rect(gamesurface, white_colour, Rect(position[0], position[1], 20, 20))
pygame.draw.rect(gamesurface, red_colour, Rect(square_purpose[0], square_purpose[1], 20, 20))
for position_2 in snakelength_2:
pygame.draw.rect(gamesurface, white_colour, Rect(position_2[0], position_2[1], 20, 20))
pygame.display.flip()
if snakeposition[0] < 0 or snakeposition[0] > 620:
GameOver(gamesurface)
if snakeposition[1] < 0 or snakeposition[1] > 460:
GameOver(gamesurface)
for snakebody in snakelength[1:]:
if snakeposition[0] == snakebody[0] and snakeposition[1] == snakebody[1]:
GameOver(gamesurface)
if snakeposition_2[0] < 0 or snakeposition_2[0] > 620:
GameOver(gamesurface)
if snakeposition_2[1] < 0 or snakeposition_2[1] > 460:
GameOver(gamesurface)
for snakebody_2 in snakelength_2[1:]:
if snakeposition_2[0] == snakebody_2 and snakeposition_2[1] == snakebody_2[1]:
GameOver(gamesurface)
ftpsClock.tick(7)
if __name__ == "__main__":
main()
Traceback (most recent call last):
File "C:\Users\A\Desktop\测试.py", line 206, in <module>
main()
File "C:\Users\A\Desktop\测试.py", line 177, in main
pygame.draw.rect(gamesurface, white_colour, Rect(position_2[0], position_2[1], 20, 20))
TypeError: There are no type variables left in list[[120, 120]]
146 行 list[] 没有这种写法 There are no type variables left in list[[120, 120]] 报错信息 写的很详细 你把中括号换成 小括号