Python 在这个简单的游戏中,我将如何显示分数?
我曾尝试使用blit函数将其粘贴到曲面上,但没有成功,因为我无法像使用玩家、敌人和食物变量那样使用曲面作为分数的来源。我还试着使用self.score=score((75575))但这不起作用,因为出于某种原因,它说“self”没有定义。如何在屏幕上显示分数Python 在这个简单的游戏中,我将如何显示分数?,python,pygame,Python,Pygame,我曾尝试使用blit函数将其粘贴到曲面上,但没有成功,因为我无法像使用玩家、敌人和食物变量那样使用曲面作为分数的来源。我还试着使用self.score=score((75575))但这不起作用,因为出于某种原因,它说“self”没有定义。如何在屏幕上显示分数 import os, sys import pygame from pygame.locals import * pygame.init() mainClock = pygame.time.Clock() WINDOWWIDTH = 1
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))
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
MOVESPEED = 6
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 = (255, 165, 0) # 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."""
self.score += points
self.reRender()
if player.colliderect(food):
return add
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
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)
score = Score((75, 575))
pygame.display.update()
mainClock.tick(40)
导入操作系统,系统
导入pygame
从pygame.locals导入*
pygame.init()
mainClock=pygame.time.Clock()
窗宽=1000
窗高=700
windowSurface=pygame.display.set_模式((窗口宽度,窗口高度),0,32)
pygame.display.set_标题(“避免!”)
黑色=(0,0,0)
红色=(255,0,0)
白色=(255,255,255)
绿色=(0,255,0)
player=pygame.Rect(500,300,40,40)
playerImage=pygame.Surface((40,40))
敌人=pygame.Rect(300、400、20、20)
enemyImage=pygame.Surface((20,20))
enemyImage.fill((红色))
food=pygame.Rect(300500,20,20)
foodImage=pygame.Surface((20,20))
食品图像填充((绿色))
moveLeft=False
moveRight=False
moveUp=False
向下移动=错误
移动速度=6
班级分数(pygame.sprite.sprite):
“得分的精灵。”
定义初始(自我,xy):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.xy=xy#save xy--当我们更改分数时,将把矩形放在它的中心
self.font=pygame.font.font(无,50)#加载默认字体,大小50
self.color=(255,165,0)#我们的rgb字体颜色
self.score=0#从零开始
self.reRender()#生成图像
def更新(自我):
通过
def添加(自身,点数):
“”“将给定的点数添加到分数中。”“”
self.score+=分数
self.reRender()
如果玩家.colliderect(食物):
返回添加
def重置(自):
“”“将分数重置为零。”“”
self.score=0
self.reRender()
def再渲染器(自):
“”“更新分数。渲染新图像并在初始坐标处重新居中。”“”
self.image=self.font.render(“%d”%(self.score),True,self.color)
self.rect=self.image.get_rect()
self.rect.center=self.xy
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
如果event.type==KEYDOWN:
如果event.key==K_LEFT:
moveRight=False
moveLeft=True
如果event.key==K_RIGHT:
moveLeft=False
moveRight=True
如果event.key==K_UP:
向下移动=错误
向上移动=真
如果event.key==K_DOWN:
moveUp=False
向下移动=真
如果event.type==KEYUP:
如果event.key==K_转义:
pygame.quit()
sys.exit()
如果event.key==K_LEFT:
moveRight=False
moveLeft=True
如果event.key==K_RIGHT:
moveLeft=False
moveRight=True
如果event.key==K_UP:
向下移动=错误
向上移动=真
如果event.key==K_DOWN:
moveUp=False
向下移动=真
窗台表面。填充(白色)
如果向下移动且player.bottom<窗口高度:
player.top+=移动速度
如果moveUp和player.top>0:
player.top-=移动速度
如果moveLeft和player.left>0:
player.left-=移动速度
如果moveRight和player.right<窗口宽度:
player.right+=移动速度
如果玩家.collide rect(敌人):
pygame.quit()
sys.exit()
windowSurface.blit(玩家图像,玩家)
窗口表面。blit(enemyImage,敌人)
窗口表面。blit(食品图像、食品)
分数=分数((75575))
pygame.display.update()
主时钟滴答(40)
您的问题不在于blit或pygame的任何其他部分;这与你对self关键字的使用有关。Self是对对象属性的引用。您不能在score类本身之外调用self.score 相反,在游戏开始时初始化一个分数对象,并根据需要添加到分数中。这就是它的工作原理:
# init Score object
my_score = Score((75, 575))
# add to score like this
my_score.add(5)
# access score attribute like this
# for simplicity, I print the value to the console
print my_score.score # not self.score!
你的问题不在于blit或pygame的任何其他部分;这与你对self关键字的使用有关。Self是对对象属性的引用。您不能在score类本身之外调用self.score 相反,在游戏开始时初始化一个分数对象,并根据需要添加到分数中。这就是它的工作原理:
# init Score object
my_score = Score((75, 575))
# add to score like this
my_score.add(5)
# access score attribute like this
# for simplicity, I print the value to the console
print my_score.score # not self.score!
print()将文本输出到stdout(也称为命令行)。我以为你的问题只是赛尔夫的问题,你理解pygame的部分
下面是我在网上找到的一个代码块,它执行您正在尝试执行的操作:
font = pygame.font.Font(None, 36)
text = font.render("Pummel The Chimp, And Win $$$", 1, (10, 10, 10))
textpos = text.get_rect(centerx=background.get_width()/2)
background.blit(text, textpos)
print()将文本输出到stdout(也称为命令行)。我以为你的问题只是赛尔夫的问题,你理解pygame的部分
下面是我在网上找到的一个代码块,它执行您正在尝试执行的操作:
font = pygame.font.Font(None, 36)
text = font.render("Pummel The Chimp, And Win $$$", 1, (10, 10, 10))
textpos = text.get_rect(centerx=background.get_width()/2)
background.blit(text, textpos)
谢谢你的解释。它仍然不会在屏幕上打印分数。您确定“打印”是正确的显示方式吗?谢谢您的解释。它仍然不会在屏幕上打印分数。您确定“打印”是正确的显示方式吗?但如何使文本动态?意思是如果玩家得到分数,我将如何更改它?font.render(“游戏分数是:%d”%(my_score.score),1,(10,10,10)。按照通常任何精灵的方式更新它。一种方法是每次更新分数时重新闪烁文本。因此,将闪烁文本的代码放在分数中的屏幕上。add()方法。但我如何使文本动态?意思是如果玩家得到分数,我将如何更改它?font.render(“游戏分数为:%d”%(my_score.score),1,(10,10,10)。以通常任何精灵的方式更新它。一种方法是每次更新时重新刷新文本