I wanted to make a game, where player can move to any arrow and get a point for so.
I can render the text, but cannot update it.
I researched but no answer is there.
My code was:
import pygame, sys
from pygame.locals import *
import random
import time
pygame.init()
FPS = 60
FramePerSec = pygame.time.Clock()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (200, 200, 200)
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
running = True
display = pygame.display.set_mode((400,600))
display.fill(WHITE)
pygame.display.set_caption("Game")
class Up(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("up.png")
self.rect = self.image.get_rect()
self.rect.center=(40,0)
self._layer = self.rect.bottom
def move(self):
self.rect.move_ip(0,10)
if (self.rect.bottom > 600):
self.rect.top = 0
self.rect.center = (random.randint(30, 370), 0)
def draw(self, surface):
surface.blit(self.image, self.rect)
class Down(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("down.png")
self.rect = self.image.get_rect()
self.rect.center=(40,0)
self._layer = self.rect.bottom
def move(self):
self.rect.move_ip(0,10)
if (self.rect.bottom > 600):
self.rect.top = 0
self.rect.center = (random.randint(30, 370), 0)
def draw(self, surface):
surface.blit(self.image, self.rect)
class Left(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("left.png")
self.rect = self.image.get_rect()
self.rect.center=(random.randint(40,SCREEN_WIDTH-40),0)
self._layer = self.rect.bottom
def move(self):
self.rect.move_ip(0,10)
if (self.rect.bottom > 600):
self.rect.top = 0
self.rect.center = (random.randint(30, 370), 0)
def draw(self, surface):
surface.blit(self.image, self.rect)
class Right(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("right.png")
self.rect = self.image.get_rect()
self.rect.center=(random.randint(40,SCREEN_WIDTH-40),0)
self._layer = self.rect.bottom
def move(self):
self.rect.move_ip(0,10)
if (self.rect.bottom > 600):
self.rect.top = 0
self.rect.center = (random.randint(30, 370), 0)
def draw(self, surface):
surface.blit(self.image, self.rect)
E1 = Up()
E2 = Down()
E3 = Left()
E4 = Right()
global rt, score
rt, score = 2, 0,
text = f"Score: {score}"
def change(val):
global rt
rt = val
def getpoint():
global score, text
score += 1
text = f"Score: {score}"
font = pygame.font.SysFont("Arial.ttf", 18)
img = font.render(f'Score: {score}', True, BLACK)
display.blit(img, (20, 20))
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Player.png")
self.rect = self.image.get_rect()
self.rect.center = (160, 520)
def update(self):
pressed_keys = pygame.key.get_pressed()
if self.rect.left > 0:
if pressed_keys[K_LEFT]:
self.rect.move_ip(-10, 0)
if self.rect.right < SCREEN_WIDTH:
if pressed_keys[K_RIGHT]:
self.rect.move_ip(10, 0)
collide = {
"1": "self.rect.colliderect(E1.rect)",
"2": "self.rect.colliderect(E2.rect)",
"3": "self.rect.colliderect(E3.rect)",
"4": "self.rect.colliderect(E4.rect)"
}
if eval(collide[str(rt)]):
change(random.randint(1, 4))
getpoint()
def draw(self, surface):
surface.blit(self.image, self.rect)
P1 = Player()
font = pygame.font.SysFont(None, 48)
img = font.render(text, True, BLACK)
rect = img.get_rect()
rect.topleft = (20, 20)
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
P1.update()
eval(f"E{rt}.move()")
display.fill(WHITE)
display.blit(img, rect)
P1.draw(display)
eval(f"E{rt}.draw(display)")
pygame.display.update()
FramePerSec.tick(FPS)
pygame.quit()
My game assets:
down.png:
up.png:
left.png:
right.png:
Player.png:
Output: Score displayed does not change
I need to get the score in screen changing, the variable "score" changes.
>Solution :
It is not enough to change the variable score
. The rendered txt does not magically change when you change the score. The image
surface is not somehow tied toe the vaiable score
. You have to render the text again.
def getpoint():
global score, text, img
score += 1
text = f"Score: {score}"
img = font.render(text , True, BLACK)