Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Updating and Displaying Score and Player | PyGame Project: Arcade Game
PyGame Project: Arcade Game
course content

Kursinnehåll

PyGame Project: Arcade Game

book
Updating and Displaying Score and Player

In this part of your game code, we're focusing on displaying the player's score and the player character itself, then updating the game screen to show these changes. Here’s a simplified breakdown:

Displaying the Score

main_display.blit(FONT.render(str(score), True, COLOR_WHITE), (WIDTH-50, 20)): This line takes the player's current score, converts it to a string (so it can be displayed as text), and renders it using a predefined font (FONT). The text is displayed in white (COLOR_WHITE) at a position near the top right corner of the game window (calculated by WIDTH-50, 20). This keeps the score visible to the player during the game.

Displaying the Player Character

main_display.blit(player, player_rect): Here, we're drawing the player's character (player) on the screen at the position defined by player_rect. This ensures that the player sees their character and can control it during the game.

Updating the Display

pygame.display.flip(): Finally, this command updates the entire screen with everything we've drawn so far. It essentially "flips" the display to show the new state with the updated score and player position. This happens each loop cycle, keeping the game visuals current.

Together, these steps ensure that the player always knows their score and can see their character moving around the screen, making the game interactive and engaging.

Uppgift

Swipe to start coding

  1. Display score: use FONT.render(str(score), True, COLOR_WHITE) to render the score as white text and main_display.blit to display it near the top right corner;
  2. Position score: place the score text at (WIDTH-50, 20), positioning it 50 pixels left from the right edge and 20 pixels down from the top of the screen;
  3. Show player: draw the player image at its current position with main_display.blit(player, player_rect);
  4. Update screen: refresh the entire screen to show the latest visuals with pygame.display.flip().

Lösning

Mark tasks as Completed
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 12
AVAILABLE TO ULTIMATE ONLY
Vi beklagar att något gick fel. Vad hände?
some-alt