Im studying pygame and trying to create a score function. But it doesn't work if I do same thing as the examples.
I did everything step by step third time however its doesnt work.
I need a text that shows me current spent time in game. Then I will turn it to score board.
My Function For Displaying Score:
def display_score():
current_time = pygame.time.get_ticks()
score_surf = myfont.render(f'{display_score}',False,(64,64,64))
score_rect = score_surf.get_rect(center = (400, 50))
screen.blit(score_surf,score_rect)
Create Font For Text:
myfont = pygame.font.Font('font/Pixeltype.ttf', 50)
All of Screen Displays: ( Spesificly section three #Functions Part )
if game_active:
# Layouts
screen.blit(sky,(0,0))
screen.blit(ground,(0,300))
# Player
screen.blit(player,player_rect)
gravity += 1
player_rect.y += gravity
if player_rect.bottom >= 300: player_rect.bottom = 300
# Functions
display_score()
# Snail
screen.blit(snail,snail_rect)
snail_rect.x -= 3
if snail_rect.right <= 0: snail_rect.left = 800
# collision
if player_rect.colliderect(snail_rect):
game_active = False
else:
screen.fill('Yellow')
Whats Wrong: : When I launched game, pygame displays text as "function display_score at 0x103e3ab90", when I try to print it terminal shows "None"
What am I missing?
There are two things wrong with your code:
>>> def func_hello(): print("hello")
...
>>> func_hello
<function func_hello at 0x7f8ab8698700>
>>> func_hello()
hello
score_surf = myfont.render(f'{display_score}',False,(64,64,64))
your function will become infinitely recursive. The fix is making a function compute_score
that computes the score and calling it from display_score
, and remembering the parenthesis.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.