[英]Python prints "function <name> at <address>" rather than function output
我正在研究 pygame 並嘗試創建一個分數 function。但是如果我按照示例做同樣的事情,它就不起作用。
我第三次一步一步地做了所有事情,但是它不起作用。
我需要一個文本來顯示我當前在游戲中花費的時間。 然后我會把它變成計分板。
我的 Function 用於顯示分數:
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)
為文本創建字體:
myfont = pygame.font.Font('font/Pixeltype.ttf', 50)
所有屏幕顯示:(特別是第三部分#Functions 部分)
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')
出了什么問題::當我啟動游戲時,pygame 將文本顯示為“0x103e3ab90 處的函數 display_score”,當我嘗試打印它時,終端顯示“無”
我錯過了什么?
您的代碼有兩處錯誤:
>>> 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))
您的 function 將無限遞歸。 修復是制作一個 function compute_score
來計算分數並從display_score
調用它,並記住括號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.