我有我的遊戲運行在一個真正的循環,我想能夠要求用戶「再次玩?」我已經有了一個用於顯示文本的矩形代碼,但是我需要一種方法讓用戶點擊矩形,或者選擇「是」,然後代碼再次運行它自己。Pygame重啓?
2
A
回答
1
有循環它自己的方法,從main()
運行中調用它,當遊戲結束並且有main()
詢問用戶他們是否想要再次播放時,如果是這樣的話,在將所有設置爲其默認
或
只是有要求再次明確的部分,重新初始化所有的變量,因爲默認值,如果用戶想再次發揮
0
要告訴我們,如果用戶在RECT點擊讓循環,只需在鼠標位置設置一個1乘以1的矩形,然後當他們點擊時,檢查是否爲th衝突正在發生。然後,就像DeadChex所說的那樣,通過讓它成爲自己的函數來再次調用循環。
0
運行我特別寫的這個函數。調整它到你的心臟的內容。如果玩家死亡(或無論發生什麼情況,以使這齣戲再次顯示文本),你只需要調用play_again()
import pygame
pygame.init()
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
bigfont = pygame.font.Font(None, 80)
smallfont = pygame.font.Font(None, 45)
def play_again():
text = bigfont.render('Play again?', 13, (0, 0, 0))
textx = SCREEN_WIDTH/2 - text.get_width()/2
texty = SCREEN_HEIGHT/2 - text.get_height()/2
textx_size = text.get_width()
texty_size = text.get_height()
pygame.draw.rect(screen, (255, 255, 255), ((textx - 5, texty - 5),
(textx_size + 10, texty_size +
10)))
screen.blit(text, (SCREEN_WIDTH/2 - text.get_width()/2,
SCREEN_HEIGHT/2 - text.get_height()/2))
clock = pygame.time.Clock()
pygame.display.flip()
in_main_menu = True
while in_main_menu:
clock.tick(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
in_main_menu = False
pygame.display.quit()
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x, y = event.pos
if x >= textx - 5 and x <= textx + textx_size + 5:
if y >= texty - 5 and y <= texty + texty_size + 5:
in_main_menu = False
break
play_again()
pygame.display.quit()
pygame.quit()
我希望這是你要找的答案。
0
在您的主程序循環中,創建一個新的event.key if語句,您希望玩家按下哪個鍵來重新啓動遊戲,然後在if語句內調用main()。這將重置所有回到方形1.
在我的例子中,我檢測用戶是否已經死亡並按下R鍵,然後重新啓動main()。
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.go_left()
if event.key == pygame.K_RIGHT:
player.go_right()
if event.key == pygame.K_UP:
player.jump()
if event.key == pygame.K_SPACE:
player.shoot()
if event.key == pygame.K_r and not player.alive():
main()
進一步在主循環中顯示一些文字,要求玩家按R鍵重生。
if not player.alive():
gameover = font.render("Press R to Respawn", False, (255, 255, 255))
rect = gameover.get_rect()
rect.center = screen.get_rect().center
screen.blit(gameover, rect)
0
我能想到的是:在代碼的末尾 把這樣的事情,如果他想打問用戶:
play_again = 1
while play_again:
play_again = int(raw_input("Play again? 0=No, 1=Yes"))
if play_again:
main()
else:
break
pygame.quit()
在我的例子,我把一個True
在while
我的main()
函數循環結束,以便main()
返回全部True
,這使得在play_again
循環內可以重複遊戲,如果用戶輸入已經1
並且退出如果0
。
0
相關問題
- 1. Pygame - 重力法
- 2. Pygame重力劇本
- 3. Pygame文本重疊
- 4. Pygame表面太重
- 5. pygame中的重力
- 6. Pygame和IDEs - 源代碼更改需要python重啓
- 7. 重新調整PyGame視頻
- 8. Pygame雪碧不會重置
- 9. Pygame - 防止雪碧重疊
- 10. Pygame,Python 2.7.3。重複障礙
- 11. 麻煩在Mac OSX上啓動pygame
- 12. Python Pygame隨機抽取非重疊圓
- 13. Pygame事件處理重要事件
- 14. 重新調整像素的大小pygame
- 15. PyGame PGU更新/重繪小部件
- 16. 在pygame中重置對象位置
- 17. Pygame - 重置我的主循環
- 18. 在pygame中下降的力學(重力)
- 19. 重啓
- 20. 重啓
- 21. 重新啓動後重新啓動
- 22. PyGame在重新連接時重新初始化USB MIDI設備
- 23. Loading pygame
- 24. Pygame sprite.spritecollide()
- 25. Pygame MemoryError
- 26. OBJFILELOADER - pygame
- 27. 在Mac重啓/啓動
- 28. Dokku重啓後啓動Mongo
- 29. pygame的 - pygame的不斷移動它們
- 30. pygame pygame高效地改變曲面