Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help I am trying to get my pygame to display what is in the picture and keep getting the error joystick is not defined

Please help I am trying to get my pygame to display what is in the picture and keep getting the error "joystick is not defined" code is provided. import pygame
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT =800,600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
# Load sound
click_sound = pygame.mixer.Sound("collision.mp3")
# Ball class
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
def update(self):
# Update motion
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
# Boundary collision
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]
def draw(self, screen):
pygame.draw.circle(screen, self.color, (int(self.location[0]), int(self.location[1])), self.radius)
# Initialize balls
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)
# Initialize joystick
pygame.joystick.init()
joystick_count = pygame.joystick.get_count()
if joystick_count >0:
joystick = pygame.joystick.Joystick(0)
joystick.init()
print(f"Number of joysticks: {joystick_count}")
print(f"Joystick name: {joystick.get_name()}")
print(f"GUID: {joystick.get_guid()}")
print(f"Number of axes: {joystick.get_numaxes()}")
print(f"Number of buttons: {joystick.get_numbuttons()}")
print(f"Number of hats: {joystick.get_numhats()}")
# Game loop
running = True
gravity_enabled = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.JOYBUTTONDOWN:
# A button to reset ball position
if joystick.get_button(0): # A Button
for ball in balls:
ball.location =[WIDTH //2, HEIGHT //2]
ball.vector =[0,0]
# LT + Y Button to quit game
if joystick.get_button(6) and joystick.get_button(3): # LT + Y Buttons
running = False
# Read joystick axes for wind/force application
left_wind = joystick.get_axis(0)
up_force = joystick.get_axis(1)
right_wind = joystick.get_axis(3)
# Apply forces based on joystick input
for ball in balls:
if left_wind >0.1:
ball.addForce((-1,0))
if right_wind -0.1:
ball.addForce((1,0))
if up_force -0.1:
force_up =-up_force *10 # Adjust multiplier as needed
ball.addForce((0,-force_up))
screen.fill((0,0,0))
for ball in balls:
ball.update()
ball.draw(screen)
pygame.display.flip()
clock.tick(60)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
ubric:
0: UI shown above is implemented and responds to all controlle
0 : Apply to your gravity and wind project.
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)
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

Sql All In One For Dummies 7 Books In One

Authors: Allen G Taylor ,Richard Blum

4th Edition

1394242298, 978-1394242290

More Books

Students also viewed these Databases questions

Question

Prepare a short profile of Lucy Clifford ?

Answered: 1 week ago

Question

Prepare a short profile of Rosa parks?

Answered: 1 week ago

Question

Prepare a short profile of victor marie hugo ?

Answered: 1 week ago