不确定在哪里更新显示 - PyGame

时间:2017-02-05 02:22:41

标签: python pygame

我正在制作蛇游戏,在这个游戏中,当蛇击中一块食物时,蛇的长度会增加。我遇到的问题是,当我实施一些代码来帮助我延长蛇的长度并测试它时,蛇在幕后移动但没有出现。我知道我必须更新显示器,但我不知道在哪里。

PYTHON

# IMPORT
import pygame, random, time, sys

# GLOBALS
global screen, displayW, displayH
global clock, FPS, end, font
global snake, food, gameOverText

# SETGLOBALVALUES
def setGlobalValues():
    global screen, displayW, displayH
    global clock, FPS, end, font
    global snake, food, gameOverText

    displayW = 600
    displayH = 400
    screen = pygame.display.set_mode((displayW, displayH))

    clock = pygame.time.Clock()
    FPS = 15
    end = False
    font = pygame.font.SysFont("robotomono", 40)

    snake = Snake()
    food = Food()

    gameOverText = Text()
    gameOverText.text = "GAME OVER"
    gameOverText.color = (255, 0, 0)

# SNAKE
class Snake():
    def __init__(self, leadX=0, leadY=0, velX=0, velY=0, change=0, w=0, h=0, color=(), list=[], head=[]):
        self.leadX = displayW / 2
        self.leadY = displayH / 2
        self.velX = 0
        self.velY = 0
        self.change = 10
        self.w = 10
        self.h = 10
        self.list = []
        self.head = []
        self.color = (0, 0, 0)

    def draw(self):
        for XnY in self.list:
            pygame.draw.rect(screen, self.color, (XnY[0], XnY[1], self.w, self.h))

    def animate(self):
        self.leadX += self.velX
        self.leadY += self.velY

    def collision(self):
        # collision with wall
        if self.leadX < 0 or self.leadX + self.w > displayW or self.leadY < 0 or self.leadY + self.h > displayH:
            gameOverText.draw()
            exit()

        # collision with food
        if self.leadX >= food.x and self.leadX <= food.x + food.w and self.leadY >= food.y and self.leadY <= food.y:
            food.reset()

# FOOD
class Food():
    def __init__(self, x=0, y=0, w=0, h=0, color=()):
        self.w = 10
        self.h = 10
        self.x = round(random.randrange(0, displayW - self.w) / 10.0) * 10.0
        self.y = round(random.randrange(0, displayH - self.h) / 10.0) * 10.0
        self.color = (0, 200, 0)

    def draw(self):
        pygame.draw.rect(screen, self.color, (self.x, self.y, self.w, self.h))

    def reset(self):
        self.x = round(random.randrange(0, displayW - self.w) / 10.0) * 10.0
        self.y = round(random.randrange(0, displayH - self.h) / 10.0) * 10.0

        self.draw()

# TEXT
class Text():
    def __init__(self, x=0, y=0, text="", color=()):
        self.x = displayW / 2 - 100
        self.y = displayH / 2 - 50
        self.text = ""
        self.color = ()

    def draw(self):
        render = font.render(self.text, True, self.color)

        screen.fill((255, 255, 255))
        screen.blit(render, (self.x, self.y))
        pygame.display.update()

        time.sleep(2)

# MAIN
def main():
    pygame.init()

    setGlobalValues()
    setup()
    gameLoop()
    quit()

# GAMELOOP
def gameLoop():
    global end

    while not end:
        for event in pygame.event.get():
            # QUIT
            if event.type == pygame.QUIT:
                end = True

            # KEYDOWN
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    snake.velX -= snake.change
                    snake.velY = 0
                elif event.key == pygame.K_RIGHT:
                    snake.velX += snake.change
                    snake.velY = 0

                elif event.key == pygame.K_UP:
                    snake.velY -= snake.change
                    snake.velX = 0
                elif event.key == pygame.K_DOWN:
                    snake.velY += snake.change
                    snake.velX = 0

        draw()
        animate()
        collision()

        setFPS()

# DRAW
def draw():
    # fill background
    screen.fill((255, 255, 255))

    snake.head.append(snake.leadX)
    snake.head.append(snake.leadY)
    snake.list.append(snake.head)

    snake.draw()
    food.draw()

    # update
    pygame.display.update()

# ANIMATE
def animate():
    snake.animate()

# COLLISION
def collision():
    snake.collision()

# SETUP
def setup():
    pygame.display.set_caption("Snake")

# SETFPS
def setFPS():
    clock.tick(FPS)

# QUIT
def exit():
    pygame.quit()
    quit()

# CALL MAIN
if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:0)

update位置正确。

我将一些代码从draw()移到了snake.animate()并添加了make_longer = True/False,现在当它吃苹果时蛇变长了。

# IMPORT
import pygame, random, time, sys

# GLOBALS
global screen, displayW, displayH
global clock, FPS, end, font
global snake, food, gameOverText

# SETGLOBALVALUES
def setGlobalValues():
    global screen, displayW, displayH
    global clock, FPS, end, font
    global snake, food, gameOverText

    displayW = 600
    displayH = 400
    screen = pygame.display.set_mode((displayW, displayH))

    clock = pygame.time.Clock()
    FPS = 5
    end = False
    font = pygame.font.SysFont("robotomono", 40)

    snake = Snake()
    food = Food()

    gameOverText = Text()
    gameOverText.text = "GAME OVER"
    gameOverText.color = (255, 0, 0)

# SNAKE
class Snake():
    def __init__(self, leadX=0, leadY=0, velX=0, velY=0, change=0, w=0, h=0, color=(), list=[], head=[]):
        self.leadX = displayW / 2
        self.leadY = displayH / 2
        self.velX = 0
        self.velY = 0
        self.change = 10
        self.w = 10
        self.h = 10
        self.list = []
        self.head = []
        self.color = (0, 0, 0)

        self.make_longer = True

    def draw(self):
        for x, y in self.list:
            pygame.draw.rect(screen, self.color, (x, y, self.w, self.h))

    def animate(self):
        self.leadX += self.velX
        self.leadY += self.velY

        # add first element
        self.head = [self.leadX, self.leadY]  # <--
        self.list.insert(0, snake.head)       # <--

        if not self.make_longer:      # <--
            # remove last element     # <-- 
            del snake.list[-1]        # <--
        else:                         # <--
            self.make_longer = False  # <--

    def collision(self):
        # collision with wall
        if self.leadX < 0 or self.leadX + self.w > displayW or self.leadY < 0 or self.leadY + self.h > displayH:
            gameOverText.draw()
            exit()

        # collision with food
        if self.leadX >= food.x and self.leadX <= food.x + food.w and self.leadY >= food.y and self.leadY <= food.y:
            food.reset()
            self.make_longer = True # <--

# FOOD
class Food():

    def __init__(self, x=0, y=0, w=0, h=0, color=()):
        self.w = 10
        self.h = 10
        self.x = round(random.randrange(0, displayW - self.w) / 10.0) * 10.0
        self.y = round(random.randrange(0, displayH - self.h) / 10.0) * 10.0
        self.color = (0, 200, 0)

    def draw(self):
        pygame.draw.rect(screen, self.color, (self.x, self.y, self.w, self.h))

    def reset(self):
        self.x = round(random.randrange(0, displayW - self.w) / 10.0) * 10.0
        self.y = round(random.randrange(0, displayH - self.h) / 10.0) * 10.0

# TEXT
class Text():

    def __init__(self, x=0, y=0, text="", color=()):
        self.x = displayW / 2 - 100
        self.y = displayH / 2 - 50
        self.text = ""
        self.color = ()

    def draw(self):
        render = font.render(self.text, True, self.color)

        screen.fill((255, 255, 255))
        screen.blit(render, (self.x, self.y))
        pygame.display.update()

        time.sleep(2)

# MAIN
def main():
    pygame.init()

    setGlobalValues()
    setup()
    gameLoop()
    quit()

# GAMELOOP
def gameLoop():
    global end

    while not end:
        for event in pygame.event.get():
            # QUIT
            if event.type == pygame.QUIT:
                end = True

            # KEYDOWN
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    snake.velX -= snake.change
                    snake.velY = 0
                elif event.key == pygame.K_RIGHT:
                    snake.velX += snake.change
                    snake.velY = 0

                elif event.key == pygame.K_UP:
                    snake.velY -= snake.change
                    snake.velX = 0
                elif event.key == pygame.K_DOWN:
                    snake.velY += snake.change
                    snake.velX = 0

        draw()
        animate()
        collision()

        setFPS()

# DRAW
def draw():
    # fill background
    screen.fill((255, 255, 255))

    snake.draw()
    food.draw()

    # update
    pygame.display.update()

# ANIMATE
def animate():
    snake.animate()


# COLLISION
def collision():
    snake.collision()

# SETUP
def setup():
    pygame.display.set_caption("Snake")

# SETFPS
def setFPS():
    clock.tick(FPS)

# QUIT
def exit():
    pygame.quit()
    quit()

# CALL MAIN
if __name__ == "__main__":
    main()

BTW:绘制红头和黑尾

def draw(self):
    # draw head
    x, y = self.list[0]
    pygame.draw.rect(screen, (255, 0, 0), (x, y, self.w, self.h))

    # draw tail
    for x, y in self.list[1:]:
        pygame.draw.rect(screen, self.color, (x, y, self.w, self.h))