在游戏中的记分牌没有让玩家整理或甚至没有展示他们

问题描述 投票:1回答:2

我花了大约2个星期在pygame上创建一个游戏,我几乎完成了我现在想要添加的唯一部分是第一名和最后一名的得分板(我这样做的原因是因为我不太确定如何创造中间排名或地方)。

我已经创建了一种方法来检查如果四个玩家中的一个是第一个的。首先,它检查玩家所在轨道的哪一部分(顶部,底部,左侧或右侧)。然后它检查球员圈数是否大于或等于其他球员圈数,一旦你越过终点线,这会增加。然后它会检查您经历了多少个细分。一旦您浏览屏幕的左上角或右侧部分,将会增加分段数量。然后根据玩家在屏幕上的位置将比较x位置或y位置(例如,如果玩家位于屏幕顶部,则将检查xpos(x位置)是否>所有其他xpos “其他玩家”其余部分显示在代码中。

diagram

if player_one.speed_y <= -1 and player_one.ypos > 30 and player_one.ypos < 662 and player_one.xpos < 100 and player_one.xpos > 0: #SHOWS WHICH PART OF TEH TRACK THE CHARACTER IS ON
        direction_one = "left" #meaning you are on the left side of the track
        if segment_lock_one == False:
            segment_amount_one += 1
            segment_lock_one = True

    elif player_one.speed_y >= 1 and player_one.xpos < 1200 and player_one.xpos > 1161 and player_one.ypos > 30 and player_one.ypos < 662:
        direction_one = "right"
        if segment_lock_one == False:
            segment_amount_one += 1
            segment_lock_one = True

    elif player_one.speed_x >= 1 and player_one.ypos > 0  and player_one.ypos < 30 and player_one.xpos > 100 and player_one.xpos < 1161:
        direction_one = "top"
        if segment_lock_one == False:
            segment_amount_one += 1
            segment_lock_one = True

    elif player_one.speed_x <= -1 and player_one.ypos < 700 and player_one.ypos > 600 and player_one.xpos > 100 and player_one.xpos < 1161:
        direction_one = "bottom"
        if segment_lock_one == False:
            segment_amount_one += 1
            segment_lock_one = True

    elif player_one.xpos >= 0 and player_one.xpos <= 100 and player_one.ypos >= 0 and player_one.ypos <= 40:
        direction_one = "corner top left"
        segment_lock_one = False

    elif player_one.xpos >= 1170 and player_one.xpos <= 1200 and player_one.ypos >= 0 and player_one.ypos <= 40:
        direction_one = "corner top right"
        segment_lock_one = False

    elif player_one.xpos >= 1161 and player_one.xpos <= 1200 and player_one.ypos >= 660 and player_one.ypos <= 700:
        direction_one = "corner bottom right"
        segment_lock_one = False

    elif player_one.xpos <= 100 and player_one.xpos >= 0 and player_one.ypos <= 700 and player_one.ypos >= 660:
        direction_one = "corner bottom left"
        segment_lock_one = False


    #CHECKING FOR FIRST PLACE IN EACH OF THE PLAYERS    
    #CHECK IF PLAYER ONE IS FIRST
    if direction_one == "left":#CHECKS POSITION #CHECKS ALL OF THE IF STATEMENTS TO SEE IF IT IS FIRST OR NOT
        if player_one_laps >= player_two_laps and player_one_laps >= player_three_laps and player_one_laps >= player_four_laps:#CHECKS HOW MANY LAPS HAVE BEEN DONE AND COMPARES IT TO THE OTHERS
            if segment_amount_one >= segment_amount_two and segment_amount_one >= segment_amount_three and segment_amount_one >= segment_amount_four:#HOW MANY SEGMENTS HAVE BEEN COVERED
                if player_one.ypos < player_two.ypos and player_one.ypos < player_three.ypos and player_one.ypos < player_four.ypos:#IF IT IS LESS THAN ALL OF THE OTHERS ON THAT CERTAIN PART OF THE TRACK
                    print("1st on left (player one)")
                    basicfont = pygame.font.SysFont('OCRB', 40)
                    texto = basicfont.render('1st: ' + (" ") +  str(one_type), True, (0, 125, 255), (0, 0, 0))
                    logo_first = Player(200, 400, one_character_pic, 60, 60)
                    logo_first.draw()
                    textrecto = texto.get_rect()
                    textrecto.centerx = screen.get_rect().centerx
                    textrecto.centery = screen.get_rect().centery
                    screen.blit(texto, (200, 600))

    if direction_one == "top":
        if player_one_laps >= player_two_laps and player_one_laps >= player_three_laps and player_one_laps >= player_four_laps:
            if segment_amount_one >= segment_amount_two and segment_amount_one >= segment_amount_three and segment_amount_one >= segment_amount_four:
                if player_one.xpos > player_two.xpos and player_one.xpos > player_three.xpos and player_one.xpos > player_four.xpos:
                    print("1st on top (player one)")
                    basicfont = pygame.font.SysFont('OCRB', 40)
                    texto = basicfont.render('1st: ' + (" ") +  str(one_type), True, (0, 125, 255), (0, 0, 0))
                    logo_first = Player(200, 400, one_character_pic, 60, 60)
                    logo_first.draw()
                    textrecto = texto.get_rect()
                    textrecto.centerx = screen.get_rect().centerx
                    textrecto.centery = screen.get_rect().centery
                    screen.blit(texto, (200, 600))



    if direction_one == "right":
        if player_one_laps >= player_two_laps and player_one_laps >= player_three_laps and player_one_laps >= player_four_laps:
            if segment_amount_one >= segment_amount_two and segment_amount_one >= segment_amount_three and segment_amount_one >= segment_amount_four:
                if player_one.ypos > player_two.ypos and player_one.ypos > player_three.ypos and player_one.ypos > player_four.ypos:
                    print("1st on right (player one)")
                    basicfont = pygame.font.SysFont('OCRB', 40)
                    texto = basicfont.render('1st: ' + (" ") +  str(one_type), True, (0, 125, 255), (0, 0, 0))
                    logo_first = Player(200, 400, one_character_pic, 60, 60)
                    logo_first.draw()
                    textrecto = texto.get_rect()
                    textrecto.centerx = screen.get_rect().centerx
                    textrecto.centery = screen.get_rect().centery
                    screen.blit(texto, (200, 600))

    if direction_one == "bottom":
        if player_one_laps >= player_two_laps and player_one_laps >= player_three_laps and player_one_laps >= player_four_laps:
            if segment_amount_one >= segment_amount_two and segment_amount_one >= segment_amount_three and segment_amount_one >= segment_amount_four:
                if player_one.xpos < player_two.xpos and player_one.xpos < player_three.xpos and player_one.xpos < player_four.xpos:
                    print("1st on bottom (player one)")
                    basicfont = pygame.font.SysFont('OCRB', 40)
                    texto = basicfont.render('1st: ' + (" ") +  str(one_type), True, (0, 125, 255), (0, 0, 0))
                    logo_first = Player(200, 400, one_character_pic, 60, 60)
                    logo_first.draw()
                    textrecto = texto.get_rect()
                    textrecto.centerx = screen.get_rect().centerx
                    textrecto.centery = screen.get_rect().centery
                    screen.blit(texto, (200, 600))

    elif direction_one == "corner top left" or direction_one == "corner top right" or direction_one == "corner bottom right" or direction_one == "corner bottom left":
        pass


################################################################################################
    #CHECK IF PLAYER TWO IS FIRST
    if player_two.xpos <= 100 and player_two.xpos >= 0 and player_two.ypos >= 30 and player_two.ypos <= 660:#BASICALLY THE DIRECTION AND CHECKS IF IT IS ON THE CORRECT PART OF THE TRACK #on that certain part of the track
        if player_two_laps >= player_three_laps and player_two_laps >= player_one_laps and player_two_laps >= player_four_laps: #how many laps have been covered
            if segment_amount_two >= segment_amount_one and segment_amount_two >= segment_amount_three and segment_amount_two >= segment_amount_four:#checks how many segments have been covered (should be 12 altogether at the end)
                if player_two.ypos < player_one.ypos and player_two.ypos < player_three.ypos and player_two.ypos < player_four.ypos:#checks if the player is furthest ahead
                    print("1st on left")
                    basicfont = pygame.font.SysFont('OCRB', 40)
                    texto = basicfont.render('1st: ' + (" ") +  str(two_type), True, (0, 125, 255), (0, 0, 0))
                    logo_first = Player(200, 400, two_character_pic, 60, 60)
                    logo_first.draw()
                    textrecto = texto.get_rect()
                    textrecto.centerx = screen.get_rect().centerx
                    textrecto.centery = screen.get_rect().centery
                    screen.blit(texto, (200, 600))

    if player_two.ypos >= 0 and player_two.ypos <= 30 and player_two.xpos >= 100 and player_two.xpos <= 1100:
        if player_two_laps >= player_three_laps and player_two_laps >= player_one_laps and player_two_laps >= player_four_laps:
            if segment_amount_two >= segment_amount_one and segment_amount_two >= segment_amount_three and segment_amount_two >= segment_amount_four:
                if player_two.xpos > player_one.xpos and player_two.xpos > player_three.xpos and player_two.xpos > player_four.xpos:
                    print("1st on top")
                    basicfont = pygame.font.SysFont('OCRB', 40)
                    texto = basicfont.render('1st: ' + (" ") +  str(two_type), True, (0, 125, 255), (0, 0, 0))
                    logo_first = Player(200, 400, two_character_pic, 60, 60)
                    logo_first.draw()
                    textrecto = texto.get_rect()
                    textrecto.centerx = screen.get_rect().centerx
                    textrecto.centery = screen.get_rect().centery
                    screen.blit(texto, (200, 600))


    if player_two.xpos >= 1151 and player_two.xpos <= 1200 and player_two.ypos >= 30 and player_two.ypos <= 660:
        if player_two_laps >= player_three_laps and player_two_laps >= player_one_laps and player_two_laps >= player_four_laps:
            if segment_amount_two >= segment_amount_one and segment_amount_two >= segment_amount_three and segment_amount_two >= segment_amount_four:
                if player_two.ypos > player_one.ypos and player_two.ypos > player_three.ypos and player_two.ypos > player_four.ypos:
                    print("1st on right")
                    basicfont = pygame.font.SysFont('OCRB', 40)
                    texto = basicfont.render('1st: ' + (" ") +  str(two_type), True, (0, 125, 255), (0, 0, 0))
                    logo_first = Player(200, 400, two_character_pic, 60, 60)
                    logo_first.draw()
                    textrecto = texto.get_rect()
                    textrecto.centerx = screen.get_rect().centerx
                    textrecto.centery = screen.get_rect().centery
                    screen.blit(texto, (200, 600))

    if player_two.ypos >= 600 and player_two.ypos <= 700 and player_two.xpos >= 100 and player_two.xpos <= 1100:
        if player_two_laps >= player_three_laps and player_two_laps >= player_one_laps and player_two_laps >= player_four_laps:
            if segment_amount_two >= segment_amount_one and segment_amount_two >= segment_amount_three and segment_amount_two >= segment_amount_four:
                if player_two.xpos < player_one.xpos and player_two.xpos < player_three.xpos and player_two.xpos < player_four.xpos:            
                    print("1st on bottom")
                    basicfont = pygame.font.SysFont('OCRB', 40)
                    texto = basicfont.render('1st: ' + (" ") +  str(two_type), True, (0, 125, 255), (0, 0, 0))
                    logo_first = Player(200, 400, two_character_pic, 60, 60)
                    logo_first.draw()
                    textrecto = texto.get_rect()
                    textrecto.centerx = screen.get_rect().centerx
                    textrecto.centery = screen.get_rect().centery
                    screen.blit(texto, (200, 600))


    if player_two.ypos < 20 and player_two.ypos > 0 and player_two.xpos < 80 and player_two.xpos > 0: #TOP LEFT CORNER
        pass

    if player_two.ypos > 0 and player_two.ypos < 20 and player_two.xpos > 1200 and player_two.xpos < 1250: #TOP RIGHT CORNER
        pass

    if player_two.xpos > 1200 and player_two.xpos < 1250 and player_two.ypos > 680 and player_two.ypos < 700: #BOTTOM RIGHT CORNER
        pass

    if player_two.xpos < 80 and player_two.xpos > 0 and player_two.ypos > 670 and player_two.ypos < 700: #BOTTOM LEFT CORNER
        pass

结果是玩家在第一次出现时无法显示,任何其他玩家在最后时刻都无法显示,并且在赛道的某些部分没有玩家出现。

python pygame leaderboard
2个回答
2
投票

您为哪个玩家赢得的支票很长,如果您确实想要将排行榜更改为包含中间位置,那么您将有很多工作更新此代码。

我建议的是向你的玩家类添加一些东西,比如player.score,当玩家沿着地图移动时会增加。每次你越过终点线时,得分可能会增加100*lapNumber10*sectionOfCourse每次你越过一个检查站和distanceAlongSection每一帧移动。这样你就可以检查你所有的球员他们有什么score你会知道所有球员的排名。

class player()
    def update(self):
        currentSection = checkSection(self.pos)
        if(currentSection = (pastSection + 1)%totalSectionsPerLap): # NEW CHECKPOINT
            self.score += 10
            self.pastSection = currentSection
            if(currentSection == section[0]): #NEW LAP
                self.lap +=1
                self.score += 100

2
投票

如果轨道始终是凸多边形,我将使用播放器之间的角度,并以轨道中心的顶点开始。

然后你可以使用圈* 360 +角来比较玩家的进度。 As illustrated here

快速而丑陋的示例代码,解决方案是方法get_angle和Player.get_angular_score:

import pygame
import sys
import time
import math
from collections import namedtuple

Point = namedtuple('Point', ('x','y'))


def get_angle(point, other):
    dx = other.x - point.x
    dy = other.y - point.y
    return math.degrees(math.atan2(dy, dx))


class Player:
    def __init__(self, x, y):
        self.x = x
        self.y = x
        self.laps = 0

    def get_angular_score(self, other: Point, start_angle):
        player_angle = get_angle(self, other)
        angle_between_player_and_start = player_angle - start_angle
        angle_between_player_and_start %= 360 # handle negative angle
        return angle_between_player_and_start + 360 * self.laps

def main():
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    center = Point(320, 240)
    start = Point(50, 50)
    pygame.font.init()
    font = pygame.font.SysFont("Verdana", 30)
    player = Player(*start)
    start_angle = get_angle(start, center)
    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    player.laps += 1

        keys=pygame.key.get_pressed()

        if keys[pygame.K_UP]:
            player.y -= 2
        if keys[pygame.K_DOWN]:
            player.y += 2
        if keys[pygame.K_LEFT]:
            player.x -= 2
        if keys[pygame.K_RIGHT]:
            player.x += 2

        screen.fill((0,0,0))
        pygame.draw.circle(screen, (255, 255, 0), (player.x, player.y), 4)
        pygame.draw.line(screen, (0, 0, 255), start, center)

        coords = font.render("x: {}, y: {}".format(player.x, player.y), True, (255,255,255))
        angle = font.render("Player score: {}".format(player.get_angular_score(center, start_angle)), True, (0, 255, 0))

        screen.blit(coords, (5,5))
        screen.blit(angle, (5, 400))

        pygame.display.update()
        time.sleep(.02)

if __name__=="__main__":
    main()

© www.soinside.com 2019 - 2024. All rights reserved.