pygame
Question: How can i incoperate user directional modifications to my code below please, that is, have the user move the images from their keyboard themselves .
import pygame
pygame.init()
screenSize = width,height = 240,180
display = pygame.display.set_mode(screenSize)
starImage = pygame.image.load("star.gif")
starBox = starImage.get_rect()
# add a second star
starImage2 = pygame.image.load("star.gif")
starBox2 = starImage2.get_rect()
keepPlaying = True
speed = [10,5]
speed2 = [5,10]
while keepPlaying:
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepPlaying = False
# redraw the background
black = 0,0,0 display.fill(black)
# draw the stars on the screen
starBox = starBox.move(speed)
starBox2 = starBox2.move(speed2)
# update their speed, changing direction if needed
if starBox.left < 0 or starBox.right > width:
speed[0] = - speed[0]
if starBox.top < 0 or starBox.bottom > height:
speed[1] = - speed[1]
if starBox2.left < 0 or starBox2.right > width:
speed2[0] = - speed2[0]
if starBox2.top < 0 or starBox2.bottom > height:
speed2[1] = - speed2[1
collide = True
if starBox.left > starBox2.right:
collide = False
elif starBox.right < starBox2.left:
collide = False
elif starBox.top > starBox2.bottom:
collide = False
elif starBox.bottom < starBox2.top:
collide = False
else:
print ("Boom!")
# update the display
display.blit(starImage, starBox)
display.blit(starImage2, starBox2)
pygame.display.flip()
pygame.time.delay(100)
Step by step
Solved in 2 steps