在捆绑蛇尾img之后我很难过,有人可以解释一种可能的方法来附加它,所以在每个块添加后它都会停留在最后。
蛇尾是## Snake Images下的可变尾巴
import pygame
import random
import time
pygame.init()
White = (255,255,255)
Black = (0,0,0)
Green = (0,155,0)
Lgreen = (219,255,219)
Red = (255,0,0)
Dwidth = 800
Dheight = 600
GameDisplay = pygame.display.set_mode((Dwidth,Dheight))
pygame.display.set_caption("Snake")
##Other Images
icon = pygame.image.load('apple.png')
pygame.display.set_icon(icon)
Logo = pygame.image.load('Snakelogo.png')
##Apple Images
appleimg = pygame.image.load('apple.png')
##Snake Images
img = pygame.image.load('snake.png')
tail = pygame.image.load('snakebot.png')
Clock = pygame.time.Clock()
blockS = 20
AppleThickness = 30
FPS = 17.5
direction = "right"
smallfont = pygame.font.SysFont("comicsansms", 25)
medfont = pygame.font.SysFont("comicsansms", 50)
largefont = pygame.font.SysFont("comicsansms", 80)
def pause():
paused = True
message("Paused",
Black,
-100,
size="large")
message("Press SPACE again to continue or Q to quit.",
Black,
25)
pygame.display.update()
while paused:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
paused = False
elif event.key == pygame.K_q:
pygame.quit()
quit()
Clock.tick(5)
def score(score):
text = smallfont.render("Score: "+str(score), True, Black)
GameDisplay.blit(text,[0,0])
def randAppleGen():
randAppleX = round(random.randrange(0,Dwidth-AppleThickness))#/10.0)*10.0
randAppleY = round(random.randrange(0,Dheight-AppleThickness))#/10.0)*10.0
return randAppleX,randAppleY
randAppleX,randAppleY = randAppleGen()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
intro = False
if event.key == pygame.K_q:
pygame.quit()
quit()
GameDisplay.fill(Lgreen)
GameDisplay.blit(Logo,[0,100,800,170])
message("The objective of the game is to eat red apples,",
Black,
-30)
message("the more apples you eat the longer you get and",
Black,
10)
message("if you run into yourself or the edges you die.",
Black,
50)
message("Press C to play, SPACE to pause, or Q to quit.",
Black,
180)
pygame.display.update()
Clock.tick(15)
def snake(blockS,snakeList):
if direction == "right":
head = pygame.transform.rotate(img, 270)
if direction == "left":
head = pygame.transform.rotate(img, 90)
if direction == "up":
head = img
if direction == "down":
head = pygame.transform.rotate(img, 180)
GameDisplay.blit(head,(snakeList[-1][0],snakeList[-1][1]))
for XnY in snakeList[:-1]:
pygame.draw.rect(GameDisplay,Green,[XnY[0],XnY[1],blockS, blockS])
def text_objects(text,color,size):
if size == "small":
textSurface = smallfont.render(text,True,color)
elif size == "medium":
textSurface = medfont.render(text,True,color)
elif size == "large":
textSurface = largefont.render(text,True,color)
return textSurface, textSurface.get_rect()
def message(msg,color,y_displace=0, size = "small"):
textSurf, textRect = text_objects(msg,color,size)
textRect.center = (Dwidth/2),(Dheight/2) +y_displace
GameDisplay.blit(textSurf,textRect)
def gameloop():
global direction
direction = "right"
GameEX = False
GameOver = False
Hor_X = Dwidth/2
Ver_Y = Dheight/2
MoveS = 10
MoveDU = 0
snakeList = []
snakeLength = 1
randAppleX,randAppleY = randAppleGen()
while not GameEX:
if GameOver == True:
message("Game Over",
Red,
y_displace=-50,
size="large")
message("Press C to play again or Q to Quit", Black,50, size = "medium")
pygame.display.update()
while GameOver == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameEX = True
GameOver = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
GameEX = True
GameOver = False
if event.key == pygame.K_c:
gameloop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameEX = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
MoveS = -blockS
MoveDU = 0
direction = "left"
elif event.key == pygame.K_RIGHT:
MoveS = blockS
MoveDU = 0
direction = "right"
elif event.key == pygame.K_UP:
MoveDU = -blockS
MoveS = 0
direction = "up"
elif event.key == pygame.K_DOWN:
MoveDU = blockS
MoveS = 0
direction = "down"
elif event.key == pygame.K_SPACE:
pause()
if Hor_X >= Dwidth or Hor_X < 0 or Ver_Y >= Dheight or Ver_Y < 0:
GameOver = True
Hor_X += MoveS
Ver_Y += MoveDU
GameDisplay.fill(Lgreen)
GameDisplay.blit(appleimg,(randAppleX, randAppleY))
snakeHead = []
snakeHead.append(Hor_X)
snakeHead.append(Ver_Y)
snakeList.append(snakeHead)
if len(snakeList) > snakeLength:
del snakeList[0]
for eachSegment in snakeList[:-1]:
if eachSegment == snakeHead:
GameOver = True
snake(blockS,snakeList)
score(snakeLength-1)
pygame.display.update()
if Hor_X > randAppleX and Hor_X < randAppleX + AppleThickness or Hor_X + blockS > randAppleX and Hor_X + blockS < randAppleX + AppleThickness:
if Ver_Y > randAppleY and Ver_Y < randAppleY + AppleThickness:
randAppleX,randAppleY = randAppleGen()
snakeLength += 1
elif Ver_Y + blockS > randAppleY and Ver_Y + blockS < randAppleY + AppleThickness:
randAppleX,randAppleY = randAppleGen()
snakeLength += 1
Clock.tick(FPS)
pygame.quit()
quit()
game_intro()
gameloop()
将它插入到snake函数中就像我将它附加到结尾那样接近〜
for XnY in snakeList[0]:
GameDisplay.blit(tail,(snakeList[0][0],snakeList[0][1]))
答案 0 :(得分:0)
我刚刚将我的snakeLength增加到2,并在添加
后解决了我的问题for XnY in snakeList[0]:
GameDisplay.blit(tail,(snakeList[0][0],snakeList[0][1]))
进入蛇的功能。