Python/Pygame 如何让我的 Score Text 永远更新自己? [英] Python/Pygame How to make my Score Text update itself forever?

查看:96
本文介绍了Python/Pygame 如何让我的 Score Text 永远更新自己?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个游戏,其中(目前)得分每秒增加 1 分.

但是,每当我运行程序时,使用我当前的代码(我相信它每秒都会更改变量),文本不会更改.它只是保持在 0.

这是我的代码:(我在这个问题中提供了文字来解释我的代码,比如评论.)

第 1 部分:导入 PyGame 和其他标准程序代码.

导入系统,pygame从 pygame.locals 导入 *pygame.init()尺寸 = 宽度,高度 = 800,500屏幕 = pygame.display.set_mode(size)

第 2 部分:设置窗口标题和颜色变量.

pygame.display.set_caption("One Score (Created by - Not Real Working Lamp Productions:)")白色 = (255,255,255)

第 3 部分:声明变量分数".我给了它一个单独的代码示例,因为它与问题有很大关系.

score = 0

第 4 部分:填充屏幕并声明默认字体变量.

screen.fill (白色)myfont = pygame.font.SysFont("monospace", 16)

第 5 部分:免责声明文本(或者是声明文本,我不太确定.)

disclaimertext = myfont.render("Copyright, 2013, Not Real Working Lamp Productions.", 1, (0,0,0))screen.blit(免责声明,(5, 480))

第 6 部分:添加乐谱文本(可能是最关键的部分.)

scoretext = myfont.render("Score = "+str(score), 1, (0,0,0))screen.blit(scoretext, (5, 10))

第 7 节:while 循环(可能是最关键的部分.)

while 1:对于 pygame.event.get() 中的事件:pygame.display.flip()如果 event.type == pygame.QUIT:sys.exit()pygame.time.wait(100)分数 = 分数 + 1

那么在我的代码中应该放什么?(我需要屏幕不断更新分数,因为它从while 1:"循环中每一秒都在变化.)

谢谢.

解决方案

我不确定 pygame 是如何构建其逻辑的,但通常 while true: 游戏循环处理一些任务:

  • 处理用户输入
  • 更新游戏状态
  • 渲染状态.

所以在你的 while 1 循环中你应该这样做,并且按照这个顺序(顺序非常重要).

您希望确保处理来自用户的任何输入,更新游戏状态,然后将其呈现给用户!

更新

一个基本的谷歌搜索告诉我你应该打电话

scoretext = myfont.render("Score = "+str(score), 1, (0,0,0))screen.blit(scoretext, (5, 10))

循环的每次迭代

修订

导入系统导入pygame从 pygame.locals 导入 *pygame.init()尺寸 = 宽度,高度 = 800,500屏幕 = pygame.display.set_mode(size)pygame.display.set_caption("测试")myfont = pygame.font.SysFont("monospace", 16)白色 = (255,255,255)分数 = 0为真:pygame.display.flip()对于 pygame.event.get() 中的事件:# 我删除计时器只是为了我的测试如果 event.type == pygame.QUIT: sys.exit()屏幕填充(白色)disclaimertext = myfont.render("一些免责声明...", 1, (0,0,0))screen.blit(免责声明,(5, 480))scoretext = myfont.render("Score {0}".format(score), 1, (0,0,0))screen.blit(scoretext, (5, 10))得分 += 1

请注意,我填充了屏幕并在每个循环中重新绘制:https://stackoverflow.com/a/1637386/1072724

<块引用>

您无法撤消写在另一个图形顶部的图形超过你可以撤消在顶部绘制的粉笔插图同一块板上的另一个粉笔插图.

通常在图形中完成的是您对黑板 - 清除所有内容,下次只重新绘制您的内容想保留.

I am currently creating a game in which (for now) the score gains 1 point every second.

However, with my current code (which I believe DOES change the variable every second) whenever I run the program, the text doesn't change. It just stays at 0.

Here is my code: (I've provided text in this question to explain my code, like comments.)

SECTION 1: Importing PyGame and other standard procedure code.

import sys, pygame
from pygame.locals import *
pygame.init()
size = width, height = 800,500
screen = pygame.display.set_mode(size)

SECTION 2: Setting the window caption and a color variable.

pygame.display.set_caption("One Score (Created by - Not Really Working Lamp Productions:)")
WHITE = (255,255,255)

SECTION 3: Declaring the variable "score". I've given it a separate code sample, because it's heavily involved with the problem.

score = 0

SECTION 4: Filling the screen and declaring the default font variable.

screen.fill (WHITE)
myfont = pygame.font.SysFont("monospace", 16)

SECTION 5: Disclaimer Text (Or is it claimer text, I'm not quite sure.)

disclaimertext = myfont.render("Copyright, 2013, Not Really Working Lamp Productions.", 1, (0,0,0))
screen.blit(disclaimertext, (5, 480))

SECTION 6: Adding the score text (POSSIBLY THE MOST CRUCIAL PART.)

scoretext = myfont.render("Score = "+str(score), 1, (0,0,0))
screen.blit(scoretext, (5, 10))

SECTION 7: The while loop (POSSIBLY THE MOST CRUCIAL PART.)

while 1:         
    for event in pygame.event.get():
        pygame.display.flip()
        if event.type == pygame.QUIT:sys.exit()
        pygame.time.wait(100)
        score = score + 1

So where in my code do I put what? (I need the screen to constantly update the score as it changes every second from the "while 1:" loop.)

Thank you.

解决方案

I'm not sure how pygame structures its logic, but typically the while true: game loop handles a few tasks:

  • processing user input
  • updating the state of the game
  • rendering the state.

So in your while 1 loop you should do so, and in that order (the order is extremely important).

You want to ensure that you handle any input from your users, update the state of the game, and then present that to the user!

update

A basic google search tells me you should call

scoretext = myfont.render("Score = "+str(score), 1, (0,0,0))
screen.blit(scoretext, (5, 10))

every iteration of the loop

Revision

import sys
import pygame
from pygame.locals import *

pygame.init()
size = width, height = 800,500
screen = pygame.display.set_mode(size)
pygame.display.set_caption("testing")
myfont = pygame.font.SysFont("monospace", 16)
WHITE = (255,255,255)

score = 0

while True:
    pygame.display.flip()
    for event in pygame.event.get():
        # I remove the timer just for my testing
        if event.type == pygame.QUIT: sys.exit()

    screen.fill(WHITE)

    disclaimertext = myfont.render("Some disclaimer...", 1, (0,0,0))
    screen.blit(disclaimertext, (5, 480))

    scoretext = myfont.render("Score {0}".format(score), 1, (0,0,0))
    screen.blit(scoretext, (5, 10))
    score += 1

Notice that I fill the screen and redraw it each loop: https://stackoverflow.com/a/1637386/1072724

You can't undo one graphic written over the top of another graphic any more than you can undo one chalk illustration drawn over the top of another chalk illustration on the same board.

What is typically done in graphics is what you'd do with the chalkboard - clear the whole lot, and next time only redraw what you want to keep.

这篇关于Python/Pygame 如何让我的 Score Text 永远更新自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆