Skip to content

Commit

Permalink
Add Space Counter practice problem
Browse files Browse the repository at this point in the history
  • Loading branch information
hpanchal092 committed May 28, 2022
1 parent 035b6f5 commit 4f5fd96
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions games/chapter3/practice/SpaceCounter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Build off of your previous code (the code from QuitPygameProblem.py)
# Draw the provided rectangle onto the screen.
# Move the object up when either the W or up arrow key is pressed;
# right when either the D or right arrow is pressed; etc.

import pygame

pygame.init()
42 changes: 42 additions & 0 deletions games/chapter3/solutions/SpaceCounter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Create a program that increments a counter every time the space bar is
# pressed. This counter should be displayed as text on the pygame window.

import pygame

pygame.init()
screen = pygame.display.set_mode((400, 400))

font = pygame.font.SysFont("arial", 70)

display_counter = 0

run = True

while run:

# Render the "display_counter" to the screen
show_counter = font.render(str(display_counter), True, (255, 192, 203))

# Makes Screen Black
screen.fill((0, 0, 0))

# Prints Data on Screen
screen.blit(show_counter, (30, 30))

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

# Checks to see if key is pressed
if event.type == pygame.KEYDOWN:

# Checks to see if the space is pressed
if event.key == pygame.K_SPACE:

# Adds one to the counter
display_counter += 1

# Updates the data
pygame.display.update()

pygame.quit()

0 comments on commit 4f5fd96

Please sign in to comment.