有人可以帮助我使蛇的身体正确地跟随头部吗?

时间:2018-07-05 17:52:23

标签: python python-3.x pygame

除了像跟随蛇一样的蛇形身体以外,它还能做我想要的一切。这是我的第一个python游戏程序,我对编程还很陌生,我似乎无法弄清楚使用我的代码来执行该操作的逻辑,这使我发疯,有人可以帮忙吗? 可能有一些未使用的变量,也有一些无用的变量,在我理解逻辑之后,我会仔细研究它们。

import pygame
import time
import random

pygame.init()
global display_height
global display_width

display_height = 600
display_width = 600
class slither(object):

    def __init__(self,top):
        self.top=top
        self.lead_x = display_height/2
        self.lead_y = display_width/2
        self.lead_x_change = 0
        self.lead_y_change = 0
        self.gameOver = False
        self.gameLoop = False
        self.FPS=10
        self.brick_size=10
        self.apple_y=0
        self.apple_x=0
        self.body_var = [10]
        self.inc = 0
        self.var = 10
        self.list_body = []
        self.a_copy = 0
        self.loop = 0
        self.direction_text = ''
        self.tag = []

        self.black = (0,0,0)
        self.white = (255,255,255)
        self.red = (255,0,0)
        self.font = pygame.font.SysFont(None, 30)
        self.clock = pygame.time.Clock()


        self.main()
        self.message('',(0,0,0))
        self.gameover()
        self.apple()
        self.add_body()



    def create_move(self, a, b):
        pygame.draw.rect(screen, self.black , [a,b,self.brick_size,self.brick_size])

    def message(self, msg, color):
        screen_text=self.font.render(msg, True, color)
        screen.blit(screen_text, [display_height/3, display_width/3])
        pygame.display.update()

    def random_number(self):
        self.apple_y = round(random.randint(10, display_width - self.brick_size)/10.0)*10.0
        self.apple_x = round(random.randint(10, display_height - self.brick_size)/10.0)*10.0
    def apple(self):
        self.create_move(self.apple_x, self.apple_y)

    def add_body(self):
        a = self.lead_x
        b = self.lead_y
        self.create_move(a+self.inc, b)
        for i in self.body_var:
            self.tag.append('snake'+str(i))
            self.create_move(a+i, b)
            self.var += 10


    def move_body(self):
        a = self.lead_x
        b = self.lead_y
        if self.direction_text == 'left':
            self.create_move(self.lead_x_change-self.brick_size, b)
        elif self.direction_text == 'right':
            self.create_move(self.lead_x_change+self.brick_size, b)
        elif self.direction_text == 'up':
            self.create_move(a, self.lead_y_change-self.brick_size)
        elif self.direction_text == 'down':
            self.create_move(a, self.lead_y_change+self.brick_size)


    def inc_body(self):
        self.inc += self.brick_size

    def gameover(self):
        while self.gameOver == False:
            screen.fill(self.white)
            self.message('Game over \nPress any to continue or W to quit ', self.black)
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_w:
                        screen.fill(self.white)
                        self.message('Game Over, You lose', self.black)
                        time.sleep(2)
                        pygame.quit()
                        quit()
                    else:
                        self.lead_x=display_height/2
                        self.lead_y=display_width/2
                        self.random_number()
                        self.main()


    def main(self):
        while self.gameLoop == False:
            screen.fill(self.white)
            self.create_move(self.lead_x,self.lead_y)
            self.apple()
            self.add_body()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT and self.direction_text != 'right':
                        self.direction_text = 'left'
                        self.lead_x_change = -self.brick_size
                        self.lead_y_change = 0
                    elif event.key == pygame.K_RIGHT and self.direction_text != 'left':
                        self.direction_text = 'right'
                        self.lead_x_change = self.brick_size
                        self.lead_y_change = 0
                    elif event.key == pygame.K_UP and self.direction_text != 'down':
                        self.direction_text = 'up'
                        self.lead_y_change = -self.brick_size
                        self.lead_x_change = 0
                    elif event.key == pygame.K_DOWN and self.direction_text != 'up':
                        self.direction_text = 'down'
                        self.lead_y_change = self.brick_size
                        self.lead_x_change = 0
            if self.lead_x_change == -self.brick_size:
                self.lead_x -= self.brick_size
                self.create_move(self.lead_x, self.lead_y)
            elif self.lead_x_change == self.brick_size:
                self.lead_x += self.brick_size
                self.create_move(self.lead_x, self.lead_y)
            if self.lead_y_change == -self.brick_size:
                self.lead_y -= self.brick_size
                self.create_move(self.lead_x, self.lead_y)
            elif self.lead_y_change == self.brick_size:
                self.lead_y += self.brick_size
                self.create_move(self.lead_x, self.lead_y)
            if self.lead_x == display_height or self.lead_x < 0 or self.lead_y == display_width or self.lead_y < 0 :
                self.gameover()

            if self.lead_x == self.apple_x and self.lead_y == self.apple_y:
                self.body_var.append(self.body_var[self.loop]+self.brick_size)
                self.random_number()
                self.inc_body()
                self.loop += 1

            pygame.display.update()
            self.clock.tick(self.FPS)


screen=pygame.display.set_mode((display_height,display_width))
pygame.display.set_caption('Snake')
slither(0).main()
pygame.quit()
quit()

预先感谢

0 个答案:

没有答案