New Perspectives on HTML5, CSS3, and JavaScript
6th Edition
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Patrick M. Carey
Chapter8: Enhancing A Website With Multimedia: Working With Sound, Video, And Animation
Section: Chapter Questions
Problem 5CP4
icon
Related questions
Question

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)

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Fundamentals of Boolean Algebra and Digital Logics
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Np Ms Office 365/Excel 2016 I Ntermed
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:
9781337508841
Author:
Carey
Publisher:
Cengage