简体   繁体   English

如何在Pygame上显示分数?

[英]How to display score on Pygame?

I'm a new programmer working on a memory game for my computer science summative.The game goes like this: the computer displays random boxes at random positions and then the user has to guess where the boxes are and click on it. 我是一名新程序员,致力于为我的计算机科学总结性的记忆游戏,游戏的过程是这样的:计算机在随机位置显示随机的盒子,然后用户必须猜测盒子的位置并单击它。

I'm basically done, except right now I'm trying to create like 5 different levels that range in level of difficulty. 我基本上已经完成了,除了现在我正尝试创建难度范围不同的5个不同级别。 eg level 1 will display like 2 boxes and level 2 will display like 5, etc. And then if the user gets through all levels they can play again. 例如,级别1将显示为2个框,级别2将显示为5,依此类推。然后,如果用户通过所有级别,则可以再次玩。 I know its a lot but I really want to get an A on this. 我非常了解它,但我真的很想在此获得A。

But right now I'm stuck because it doesnt really work until I try to close the window, and even then it only goes halfway. 但是现在我被困住了,因为直到我尝试关闭窗口,它才真正起作用,即使那样它也只能进行一半。 I'm thinking its how I defined the functions but I'm not to certain. 我在考虑如何定义函数,但不确定。 Any help would be appreciated. 任何帮助,将不胜感激。

import pygame , sys
import random
import time

size=[500,500]
pygame.init()
screen=pygame.display.set_mode(size)


# Colours
LIME = (0,255,0) 
RED = (255, 0, 0)
BLACK = (0,0,0)
PINK = (255,102,178)
SALMON = (255,192,203)
WHITE = (255,255,255)
LIGHT_PINK = (255, 181, 197)
SKY_BLUE = (176, 226, 255)
screen.fill(BLACK)

# Width and Height of game box
width=50
height=50



# Margin between each cell
margin = 5

rows = 20
columns = 20


# Set title of screen
pygame.display.set_caption("Spatial Recall")

# Used to manage how fast the screen updates
clock=pygame.time.Clock()


coord=[]

# Create a 2 dimensional array. A two dimesional
# array is simply a list of lists.
def resetGrid():
    grid = []
    for row in range(rows):
        # Add an empty array that will hold each cell
        # in this row
        grid.append([])
        for column in range(columns):
            grid[row].append(0) # Append a cell  
    return grid

def displayAllPink(pygame):
    for row in range(rows):
        for column in range(columns):
            color = LIGHT_PINK
            pygame.draw.rect(screen,color,[(margin+width)*column + margin,(margin+height)*row+margin,width,height])
            pygame.display.flip()      

def displayOtherColor(pygame,grid):
    coord = []
    for i in range(random.randint(2,5)):
        x = random.randint(2, rows-1)
        y = random.randint(2, columns-1)                
        color = LIME    
        pygame.draw.rect(screen,color,[(margin+width)*y + margin,(margin+height)*x+margin,width,height])
        coord.append((x,y))  
        grid[x][y] = 1
        pygame.display.flip() 
    time.sleep(1)
    return coord

def runGame(gameCount,coord,pygame,grid):
    pygame.event.clear()
    pygame.display.set_caption("Spatial Recall: Level "+ str(gameCount))
    pygame.time.set_timer(pygame.USEREVENT,1000)
    time = 0
    #clock.tick( 
            # -------- Main Program Loop -----------
    #Loop until the user clicks the close button.
    done = False
    while done==False:    
        event = pygame.event.wait() # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done=True # Flag that we are done so we exit this loop
            pygame.event.clear()
            print "Game ",gameCount, "ends"
        elif event.type == pygame.USEREVENT:
            time = time + 1
            pygame.display.set_caption("Spatial Recall: Level "+ str(gameCount) + " Time: "+ str(time))
            if time == 100:
                done = True
                pygame.display.set_caption("Time out, moving to next level")
                pygame.event.clear()
                return False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # User clicks the mouse. Get the position
            pos = pygame.mouse.get_pos()
            # Change the x/y screen coordinates to grid coordinates
            column=pos[0] // (width+margin)
            row=pos[1] // (height+margin)
            if (row,column) in coord:
                print coord
                coord.remove((row,column))
                print coord
                color = LIME
                pygame.draw.rect(screen,color,[(margin+width)*column + margin,(margin+height)*row+margin,width,height])
                if coord == []:
                    done=True 
                    pygame.display.set_caption("Time out, moving to next level")
                    pygame.event.clear()
                    return True
            else:
                color = RED
                pygame.draw.rect(screen,color,[(margin+width)*column + margin,(margin+height)*row+margin,width,height])
            pygame.display.flip() 


def startTheGame(gameCount):
    grid = resetGrid()
    displayAllPink(pygame)
    coord = displayOtherColor(pygame,grid)
    displayAllPink(pygame)
    runGame(gameCount,coord,pygame,grid)

for i in range(2):
    startTheGame(i+1)
pygame.quit ()

the main problem why it is not working at the moment is: 目前无法正常运作的主要问题是:

  • your globals rows and columns are set to 20 but your board has only 9 fields, this is why most randomly selected coords are off the board 您的全局变量rowscolumns设置为20,但您的面板只有9个字段,这就是为什么大多数随机选择的coords都不在coords上的原因

then, you do not control that the same coord is chosen 2 times. 那么,您将无法控制选择同一coord 2次。

in general, I would advise choosing better names, especially for displayOtherColor which assembles your target coordinates for each level. 通常,我建议您选择更好的名称,尤其是对于displayOtherColor ,它会为每个级别组合目标坐标。

for your question how to display score, I would propose setting it as caption, as you are already doing with the running time. 对于您如何显示分数的问题,我建议将其设置为标题,因为您已经在运行时间了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM