如何添加分数跟踪器?

时间:2019-06-04 22:53:08

标签: python pygame

所以我希望它每次蛇吃糖果时都计算分数。

我没有做太多尝试,但是我试图找到现有代码并将其添加到我的代码中,但这只是打破了游戏规则。我还尝试通过观看教程来制作自己的记分板,但我不知道代码的开头或结尾应该放在哪里。

import pygame
import random

score = 0

welcome = ("Welcome to our snake game")

print(welcome)


class cube:
    height = 20
    w = 500
    def __init__(movee,start,x=1,y=0,color=(0,0,0)):
        movee.pos = start
        movee.x = 1
        movee.y = 0
        movee.color = color


    def move(movee, x, y):
        movee.x = x
        movee.y = y
        movee.pos = (movee.pos[0] + movee.x, movee.pos[1] + movee.y)

    def draw(movee, surface, eyes=False):
        leng = movee.w // movee.height
        i = movee.pos[0]
        j = movee.pos[1]

        pygame.draw.rect(surface, movee.color, (i*leng+1,j*leng+1, leng-2, leng-2))



class snake:
    body = []
    turns = {}
    def __init__(movee, color, pos):
        movee.color = color
        movee.head = cube(pos)
        movee.body.append(movee.head)

    def move(movee):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

            keys = pygame.key.get_pressed()

            for key in keys:
                if keys[pygame.K_LEFT]:
                    movee.x = -1
                    movee.y = 0
                    movee.turns[movee.head.pos[:]] = [movee.x, movee.y]

                elif keys[pygame.K_RIGHT]:
                    movee.x = 1
                    movee.y = 0
                    movee.turns[movee.head.pos[:]] = [movee.x, movee.y]

                elif keys[pygame.K_UP]:
                    movee.x = 0
                    movee.y = -1
                    movee.turns[movee.head.pos[:]] = [movee.x, movee.y]

                elif keys[pygame.K_DOWN]:
                    movee.x = 0
                    movee.y = 1
                    movee.turns[movee.head.pos[:]] = [movee.x, movee.y]

        for i, c in enumerate(movee.body):
            p = c.pos[:]
            if p in movee.turns:
                turn = movee.turns[p]
                c.move(turn[0],turn[1])
                if i == len(movee.body)-1:
                    movee.turns.pop(p)
            else:
                if c.x == -1 and c.pos[0] <= 0: c.pos = (c.height-1, c.pos[1])
                elif c.x == 1 and c.pos[0] >= c.height-1: c.pos = (0,c.pos[1])
                elif c.y == 1 and c.pos[1] >= c.height-1: c.pos = (c.pos[0], 0)
                elif c.y == -1 and c.pos[1] <= 0: c.pos = (c.pos[0],c.height-1)
                else: c.move(c.x,c.y)




    def add(movee):
        tail = movee.body[-1]
        dx, dy = tail.x, tail.y

        if dx == 1 and dy == 0:
            movee.body.append(cube((tail.pos[0]-1,tail.pos[1])))
        elif dx == -1 and dy == 0:
            movee.body.append(cube((tail.pos[0]+1,tail.pos[1])))
        elif dx == 0 and dy == 1:
            movee.body.append(cube((tail.pos[0],tail.pos[1]-1)))
        elif dx == 0 and dy == -1:
            movee.body.append(cube((tail.pos[0],tail.pos[1]+1)))

        movee.body[-1].x = dx
        movee.body[-1].y = dy


    def draw(movee, surface):
        for i, c in enumerate(movee.body):
            if i ==0:
                c.draw(surface, True)
            else:
                c.draw(surface)

def drawingAGrid(w, height, surface):
    sizein = w // height

    x = 0
    y = 0

    for l in range(height):
        x = x + sizein
        y = y + sizein






def redrawGrid(surface):
    global height, width, s, snack
    surface.fill((255,255,255))
    s.draw(surface)
    snack.draw(surface)
    drawingAGrid(width, height, surface)
    pygame.display.update()



def Candy(height, item):

    positions = item.body

    while True:
        x = random.randrange(height)
        y = random.randrange(height)
        if len(list(filter(lambda z:z.pos == (x,y), positions))) > 0:
            continue
        else:
            break

    return (x,y)



def gameloop():
    global width, height, s, snack, x_pos, y_pos, reset
    width = 500
    height = 20
    win = pygame.display.set_mode((width, width))
    s = snake((255, 0, 0), (10, 10))
    snack = cube(Candy(height, s), color=(0, 0, 0))
    flag = True


    clock = pygame.time.Clock()
    x_pos, y_pos = s.body[0].pos

    while flag:

        pygame.time.delay(50)
        clock.tick(7)
        s.move()
        x, y = s.body[0].pos

        if not -1 <= x - x_pos <= 1 or not -1 <= y - y_pos <= 1:
            movee.reset((10,10))



        x_pos, y_pos = s.body[0].pos
        if s.body[0].pos == snack.pos:
            s.add()
            snack = cube(Candy(height, s), color=(0, 0, 0))









        redrawGrid(win)




gameloop()

我只想像一个记分牌,在任何一个角落计数得分。

1 个答案:

答案 0 :(得分:1)

使用pygame.freetype渲染文本。例如,创建一个pygame.freetype.SysFont对象:

import pygame.freetype
pygame.init()
font = pygame.freetype.SysFont('Times New Roman', 30)

分数是身体部位的数量。使用str将数字转换为文本,然后使用.render()将文本转换为pygame.Surface对象:

score = len(s.body)
text_surf, text_rect = font.render(str(score), (255, 0, 0), size=30) 

在窗口的边界处定义边距,计算文本位置(例如,右下角),并将文本.blit移到窗口的表面:

margin = 10
text_pos = (width - text_rect.width - margin, width - text_rect.height - margin)
surface.blit(text_surf, text_pos)

功能redrawGrid

def redrawGrid(surface):
    global height, width, s, snack
    surface.fill((255,255,255))
    s.draw(surface)
    snack.draw(surface)
    drawingAGrid(width, height, surface)

    score = len(s.body)
    text_surf, text_rect = font.render(str(score), (255, 0, 0), size=30) 
    margin = 10
    text_pos = (width - text_rect.width - margin, width - text_rect.height - margin)
    surface.blit(text_surf, text_pos)

    pygame.display.update()