重造蛇,无法做出更多细分

时间:2019-02-26 21:25:52

标签: python pygame

我正在pygame中重新制作蛇,我需要使其更长。我编写了一段代码,当我吃了一个苹果后,它移动了蛇的第二段:

if sa == 2:
    pygame.draw.rect(screen, GREEN, pygame.Rect(sx,sy,tilesize,tilesize))

在“游戏循环”的顶部,我有:

sx = x
sy = y

我想添加更多的段,但不知道如何在不必为每个可能的段创建函数的情况下执行此操作。 我想要一个代码,它告诉每个新细分市场去向,或者有人是否有更好的主意(我确定他们有更好的主意),请发帖代替


到目前为止,这是我的代码:

import pygame, sys, random
from pygame.locals import *
pygame.init()
movement_x = movement_y = 0
RED = (240, 0, 0)
GREEN = (0, 255, 0)
ran = [0,25,50,75,100,125,150,175,200,225,250,275,300,325,350,375,400,425,450,475,500]
ax = 0
ay = 0
x = 0
y = 0
sa = 0
sizex = 500
sizey = 500
tilesize = 25
screen = pygame.display.set_mode((sizex,sizey))
pygame.display.set_caption('Snake')
pygame.display.set_icon(pygame.image.load('images/tile.png'))
tile = pygame.image.load('images/tile.png')
tile = pygame.transform.scale(tile, (tilesize, tilesize))
x2 = 0
pag = 0
clock = pygame.time.Clock()
sx = 0
sy = 0
vel_x = 0
vel_y = 0
ap = True
while True:
    sx = x
    sy = y
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    for row in range(sizex):
        for column in range(sizey):
            screen.blit(tile,(column*tilesize, row*tilesize,tilesize,tilesize))
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_UP:
                vel_y = -25
                vel_x = 0
            elif event.key == K_DOWN:
                vel_y = 25
                vel_x = 0
            elif event.key == K_LEFT:
                vel_x = - 25
                vel_y = 0
            elif event.key == K_RIGHT:
                vel_x= 25
                vel_y = 0
            elif event.key == K_y:
                pag = 1
            elif event.key == K_n:
                pag = 2


    inBounds = pygame.Rect(0, 0, sizex, sizey).collidepoint(x+vel_x, y+vel_y)
    if inBounds:
        y += vel_y
        x += vel_x
    else:
        basicFont = pygame.font.SysFont(None, 48)
        text = basicFont.render('Game Over! Play again? y/n', True, GREEN, RED)
        textRect = text.get_rect()
        textRect.centerx = screen.get_rect().centerx
        textRect.centery = screen.get_rect().centery
        pygame.draw.rect(screen, RED, (textRect.left - 20, textRect.top - 20, textRect.width + 40, textRect.height + 40))
        screen.blit(text, textRect)
        ay = -25
        ax = -25
        x = -25
        y = -25
        if pag == 1:
            pag = 0
            inBounds = True
            x = 0
            y = 0
            vel_x = 0
            vel_y = 0
            ax = random.choice(ran)
            ay = random.choice(ran)
            pygame.draw.rect(screen, RED, pygame.Rect(ax,ay,tilesize,tilesize))
        if pag == 2:
            pygame.quit()
            sys.exit()

    if ap:
        pygame.draw.rect(screen, RED, pygame.Rect(ax,ay,tilesize,tilesize))
    if x == ax and y == ay:
        pygame.draw.rect(screen, RED, pygame.Rect(ax,ay,tilesize,tilesize))
        ax = random.choice(ran)
        ay = random.choice(ran)
        sa += 1
    if sa == 2:
        pygame.draw.rect(screen, GREEN, pygame.Rect(sx,sy,tilesize,tilesize))
    pygame.draw.rect(screen, GREEN, pygame.Rect(x,y,tilesize,tilesize))
    pygame.display.update()
    clock.tick(100)

2 个答案:

答案 0 :(得分:1)

想想蠕虫如何移动,它会抬起头,然后在整个身体中引起向下运动的变化。每个身体节段都移动到上一个节段的位置。

您的“蛇”基本上是矩形+位置的列表。当蛇头移动时,第二个身体矩形会移动到移动前头部所占据的空间中。第三部分从第二,第四到第三等移入空间,等等。

处理此问题的一种方法是将所有“蛇形零件”保留在列表中,并遍历所有条目,依次更改位置。确定头部移动的方式,然后从尾部开始,将每个片段移动到上一个片段的位置。

这使我们获得了一个简单的功能:

snake_parts = []   # /N/ Pygame rects 

def slither( new_head_coord ):
    # Move each body part to the location of the previous part
    # So we iterate over the tail-parts in reverse
    for i in range( len( snake_parts )-1, 0, -1 ):
        x, y = snake_parts[i-1].centerx, snake_parts[i-1].centery
        snake_parts[i].center = ( x, y )
    # Move the head
    snake_parts[0].centre = new_head_coord

答案 1 :(得分:1)

正如评论所言,创建蛇类是对蛇的功能进行分组的好主意。

要代表蛇,您需要考虑一个容纳它的数据结构。您可以将其表示为点数组。对于游戏循环中的每个刻度,您可以在数组的末尾附加一个新点,然后删除第一个点。这代表您的蛇的动作。新的点将是“头”(这是数组中的最后一个点),并将被放置在当前头的位置+ 1(无论您沿哪个方向)。

要绘制蛇,只需在点数组上循环并在每个点绘制一个正方形即可。