使用 blit 方法创建动态文本 [英] Creating Dynamic Text Using the blit method
问题描述
我正在创建一个基本游戏,其中一个黑色方块在屏幕上移动.还有另外两个方块,一个绿色方块和一个红色方块.当黑色方块与红色方块碰撞时它会退出程序,当它与绿色方块碰撞时它会添加到您的积分计数器中.我已经把红色方块部分放下了,但是我在使用精灵和类创建动态文本时遇到了麻烦.我之前问过的一个问题中的某个人告诉我,我应该将分数 blit 到屏幕上,然后每次方块与绿色方块相撞时重新 blit 它.但是,我不知道如何正确执行此操作.这是我的代码:
I'm creating a basic game where a black square moves around the screen. There are two other squares, a green square and a red square. When the black square collides with the red square it quits the program, when it collides with the green square it adds to your points counter. I've got the red square part down but I'm having trouble creating dynamic text using sprites and classes. Someone from a previous question I asked told me I should blit the score to the screen and then re-blit it every time the square collided with the green square. I don't know how to properly do this however. Here is my code:
import os, sys
import pygame
from pygame.locals import *
pygame.init()
mainClock = pygame.time.Clock()
WINDOWWIDTH = 1000
WINDOWHEIGHT = 700
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption("Avoid!")
BLACK = (0, 0, 0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
player = pygame.Rect(500, 300, 40, 40)
playerImage = pygame.Surface((40, 40))
enemy = pygame.Rect(300, 400, 20, 20)
enemyImage = pygame.Surface((20, 20))
enemyImage.fill((RED))
food = pygame.Rect(300, 500 , 20, 20)
foodImage = pygame.Surface((20, 20))
foodImage.fill((GREEN))
class Score(pygame.sprite.Sprite):
"""A sprite for the score."""
def __init__(self, xy):
pygame.sprite.Sprite.__init__(self)
self.xy = xy # save xy -- will center our rect on it when we change the score
self.font = pygame.font.Font(None, 50) # load the default font, size 50
self.color = (BLACK) # our font color in rgb
self.score = 0 # start at zero
self.reRender() # generate the image
def update(self):
pass
def add(self, points):
"""Adds the given number of points to the score."""
if player.colliderect(food):
my_score.score += points
self.reRender()
def reset(self):
"""Resets the scores to zero."""
self.score = 0
self.reRender()
def reRender(self):
"""Updates the score. Renders a new image and re-centers at the initial coordinates."""
self.image = self.font.render("%d"%(self.score), True, self.color)
self.rect = self.image.get_rect()
self.rect.center = self.xy
my_score = Score((75, 575))
print my_score.score
font = pygame.font.Font(None, 36)
text = font.render("%d" % (my_score.score), 1, (10, 10, 10))
textpos = text.get_rect(centerx=windowSurface.get_width()/2)
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
MOVESPEED = 6
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
moveRight = False
moveLeft = True
if event.key == K_RIGHT:
moveLeft = False
moveRight = True
if event.key == K_UP:
moveDown = False
moveUp = True
if event.key == K_DOWN:
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT:
moveRight = False
moveLeft = True
if event.key == K_RIGHT:
moveLeft = False
moveRight = True
if event.key == K_UP:
moveDown = False
moveUp = True
if event.key == K_DOWN:
moveUp = False
moveDown = True
windowSurface.fill(WHITE)
if moveDown and player.bottom < WINDOWHEIGHT:
player.top += MOVESPEED
if moveUp and player.top > 0:
player.top -= MOVESPEED
if moveLeft and player.left > 0:
player.left -= MOVESPEED
if moveRight and player.right < WINDOWWIDTH:
player.right +=MOVESPEED
if player.colliderect(enemy):
pygame.quit()
sys.exit()
windowSurface.blit(playerImage, player)
windowSurface.blit(enemyImage, enemy)
windowSurface.blit(foodImage, food)
windowSurface.blit(text, textpos)
score = Score((75, 575))
pygame.display.update()
mainClock.tick(40)
我是否将 windowSurface.blit(text, textpos) 放入 Score 类中的 add 函数中?大方块碰到绿色小方块后如何提高分数?
Do I put windowSurface.blit(text, textpos) into the add function within the Score class? How do I increase the score once the big square touches the little green square?
推荐答案
老实说,我不认为你真的需要一个单独的分数类,你可以有一个 add score 方法,每当大方块和小方块碰撞:
Honestly, I don't think you really need a separate class for the score, you can just have an add score method which is called whenever the rects for the big and little squares collide:
def addScore():
global score
score += 1 #just increasing thescore
if pygame.font: #checking if the font loading correctly, this section is taken directly from the Chimp Line by Line tutorial
font = pygame.font.Font(None, 36)
text = font.render("score: " + str(score), 1, (10, 10, 10))
textpos = text.get_rect(centerx=background.get_width()/2)
background.blit(text, textpos)
注意:我没有在 python 中测试过这个
Note: I haven't tested this myself within python
这对你有用吗?
这篇关于使用 blit 方法创建动态文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!