05 Aug PMCA Sunday 14:00 Python Homework 21.08.01.
Question:
In the previous class we wrote the code to move the circle in left, right, up and down using the W-A-S-D keys.
Continue with the code, and try to also add the code to move the circle in left, right , up and down using the LEFT-RIGHT-UP-DOWN arrow keyboard keys.
Also, write code to change the color of the circle when you press the SPACE keyboard key.
The code from last week is below:
# Control the animation with our keys
# Move the circle by using W-A-S-D keys
# create a pygame window with a circle
# make sure you create the circle inside the while true loop
# create the variable circle_x, circle_y, speed_x and speed_y
import pygame, sys, random
pygame.init()
screen_height = 480
screen_width = 640
screen=pygame.display.set_mode([screen_width, screen_height])
screen.fill([150,222,232])
pygame.display.flip()
#create the variables
circle_x = 100
circle_y = 200
speed_x = 5
speed_y = 5
circle_radius = 50
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# this if statement will be true when the user
# pushes a key down on the keyboard
if event.type == pygame.KEYDOWN:
# if the key that is down is the w key
# than I will update the circle_y position so that it looks
# like the circle is moving up
if event.key == pygame.K_w:
circle_y = circle_y - speed_y
# write condition to move the circle down when the s key is pressed
# write condition to move the circle left when the "a" key is pressed
# if statement will be true if the key that is pressed is "a"
if event.key == pygame.K_a:
circle_x = circle_x - speed_x
# write condition to move the circle right when the "d" key is pressed
# step 1 is clear the previous animation (fill the screen again )
screen.fill([150,222,232])
# last step is always to draw the circle at new position
pygame.draw.circle(screen, [240,123,121], [circle_x,circle_y], circle_radius)
pygame.display.flip()
clock.tick(24)
Sorry, the comment form is closed at this time.