如何解决如何在 pygame 中添加分数?
import pygame
from pygame.constants import( QUIT,KEYDOWN,KEYUP,K_LEFT,K_RIGHT,K_ESCAPE,K_SPACE,MOUSEBUTTONDOWN)
import os
from random import randint
import time
import sys
class Settings:
w_width = 800
w_height = 600
w_border = 50
file_path = os.path.dirname(os.path.abspath(__file__))
image_path = os.path.join(file_path,"pictures")
class Background(object):
def __init__(self,filename):
self.imageo = pygame.image.load(os.path.join(Settings.image_path,filename))
self.image = pygame.transform.scale(self.imageo,(Settings.w_width,Settings.w_height)).convert()
self.rect = self.image.get_rect()
def draw(self,screen):
screen.blit(self.image,self.rect)
这里我已经尝试创建一个 score 类,它应该创建一个字体和分数计数器。
class score(object):
def __init__(self):
self.black = 0,0
self.count = 0
self.font = pygame.font.SysFont("comicsans",50,True,True)
self.text = self.font.render("score : "+str(self.count),1,self.black)
此功能应显示分数,但我收到错误消息。类型错误:blit 的目标位置无效。我不知道如何解决这个问题
def show_score(self,screen):
screen.blit(self.text,100,100)
def score_up(self):
self.count += 1
class Bubble(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.width = 50
self.height = 300
self.image = pygame.image.load(os.path.join(Settings.image_path,"Blase.png ")).convert_alpha()
self.image = pygame.transform.scale(self.image,(25,25))
self.rect_br = (Settings.w_width//2) + 100,(Settings.w_height//2) + 100
self.rect = self.image.get_rect()
self.mousex,self.mousey = pygame.mouse.get_pos()
self.cx = self.width - 25
self.cy = self.height - 25
self.rect.left = randint(Settings.w_border,Settings.w_width - Settings.w_border)
self.rect.top = randint(Settings.w_border,Settings.w_height - (Settings.w_border * 2))
def update(self):
pass
def scale_up(self):
self.scale["width"] += 2
self.scale["height"] += 2
def scale_down(self):
self.scale["width"] -= 2
self.scale["height"] -= 2
def events(self):
pass
def draw(self,self.rect )
这里是一切发生的主类
class Game(object):
def __init__(self):
self.screen = pygame.display.set_mode((Settings.w_width,Settings.w_height))
self.clock = pygame.time.Clock()
self.runn = False
self.score = score()
self.background = Background("Hintergrund.png")
self.bubble = pygame.sprite.Group()
def run(self):
self.runn = True
while self.runn:
self.clock.tick(60)
self.watch_for_events()
self.draw()
self.events()
def events(self):
if len(self.bubble)< 10:
self.bubble.add(Bubble())
time.sleep(0.5)
def draw(self):
self.background.draw(self.screen)
self.bubble.draw(self.screen)
self.score.show_score(self.screen)
pygame.display.flip()
def watch_for_events(self):
for event in pygame.event.get():
if event.type == QUIT:
self.runn = False
if event.type == MOUSEBUTTONDOWN:
for bubble in self.bubble:
if bubble.rect.collidepoint(event.pos):
bubble.kill()
if __name__ == '__main__':
os.environ['SDL_VIDEO_WINDOWS_POS'] = "50,1100"
pygame.init()
game = Game()
game.run()
pygame.quit()
解决方法
blit
的第二个参数必须是带有 x 和 y 坐标的元组:
screen.blit(self.text,100,100)
screen.blit(self.text,(100,100))
如果乐谱发生变化,您需要重新渲染乐谱文本Surface:
class Score(object):
# [...]
def score_up(self):
self.count += 1
self.text = self.font.render("Score : "+str(self.count),1,self.black)
类Score
:
class Score(object):
def __init__(self):
self.black = 0,0
self.count = 0
self.font = pygame.font.SysFont("comicsans",50,True,True)
self.text = self.font.render("Score : "+str(self.count),self.black)
def show_score(self,screen):
screen.blit(self.text,100))
def score_up(self):
self.count += 1
self.text = self.font.render("Score : "+str(self.count),self.black)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。