Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help me build a pygame that does the following. 6 0 : Apply to your gravity and wind project.Number of joysticks: 1 Joystick 0

please help me build a pygame that does the following. 60: Apply to your gravity and wind project.Number of joysticks: 1
Joystick 0
Joystick name: Nintendo Switch Pro Controller
GUID: 030000007e0500000920000000026800
Number of axes: 6
Axis 0 value: 0.008
Axis 1 value: -0.011
Axis 2 value: 0.003
Axis 3 value: -0.011
Axis 4 value: 0.000
Axis 5 value: 0.000
Number of buttons: 16
Button 0 value: 0
Button 1 value: 0
Button 2 value: 0
Button 3 value: 0
Button 4 value: 0
Button 5 value: 0
Button 6 value: 0
Button 7 value: 0
Button 8 value: 0
Button 9 value: 0
Button 10 value: 0
Button 11 value: 0
Button 12 value: 0
Button 13 value: 0
Button 14 value: 0
Button 15 value: 0
Number of hats: 0
10: gravity toggled on/off (you pick)
10: left wind applied momentarily (Axis 0,0..-1)
10: right wind applied momentarily (Axis 3,-1..0)
10: Force up (Axis 1,0..-1, maps to 0..your gravity amount)
10: ball returns to the center of the screen (A/0 Button)
10: LT + Y quits game import pygame
import random
# Initialize Pygame
pygame.init()
WIDTH, HEIGHT =800,600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
# Load the click sound
click_sound = pygame.mixer.Sound("collision.mp3")
# Create ball class and implement addforce to it
class Ball:
def __init__(self, x, y, radius, mass, color):
self.location =[x, y]
self.vector =[0,0]
self.acceleration =[0,0]
self.radius = radius
self.mass = mass
self.color = color
def addForce(self, force):
fx, fy = force
self.acceleration[0]+= fx / self.mass
self.acceleration[1]+= fy / self.mass
# Update the ball class
def update(self):
self.vector[0]+= self.acceleration[0]
self.vector[1]+= self.acceleration[1]
self.location[0]+= self.vector[0]
self.location[1]+= self.vector[1]
self.acceleration =[0,0] # Reset acceleration
# Add boundary collision detection
if self.location[0]=0 or self.location[0]>= WIDTH:
self.vector[0]=-self.vector[0]
if self.location[1]=0 or self.location[1]>= HEIGHT:
self.vector[1]=-self.vector[1]
# Draws the balls on my screen
def draw(self, screen):
pygame.draw.circle(screen, self.color, self.location, self.radius)
balls =[]
for i in range(10):
radius = random.randrange(10,50)
x = random.randrange(radius, WIDTH - radius)
y = random.randrange(radius, HEIGHT - radius)
mass = radius **2
color =(random.randrange(0,256), random.randrange(0,256), random.randrange(0,256))
ball = Ball(x, y, radius, mass, color)
balls.append(ball)
def on_click(pos):
# Play the click sound
click_sound.play()
# Apply force towards the clicked position
for ball in balls:
dx = pos[0]- ball.location[0]
dy = pos[1]- ball.location[1]
ball.addForce((dx *0.1, dy *0.1))
# Values for star background
stars =[]
for i in range(1000):
x = random.randrange(0, WIDTH)
y = random.randrange(0, HEIGHT)
stars.append((x, y))
# Draws stars on background
def draw_stars(screen, stars):
for star in stars:
pygame.draw.rect(screen,(255,255,255), pygame.Rect(star[0], star[1],1,1))
# Game loop with on click call
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
on_click(pygame.mouse.get_pos())
screen.fill((0,0,d 0))
draw_stars(screen, stars)
for ball in balls:
ball.update()
ball.draw(screen)
pygame.display.flip()
clock.tick(60)*****the output should look just like the photo provided
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mysql Examples Explanations Explain Examples

Authors: Harry Baker ,Ray Yao

1st Edition

B0CQK9RN2J, 979-8872176237

More Books

Students also viewed these Databases questions

Question

Define conformity and obedience. Then, provide an example of each.

Answered: 1 week ago