Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Turtle in Python I need some assistance for how to write the program. People say that turtle is generally easier so I hope to get

Turtle in Python

I need some assistance for how to write the program. People say that turtle is generally easier so I hope to get an accurate and faster response.

(Question at the bottom)

Current code needed for problem:

import turtle, math, randomclass Ball(turtle.Turtle): def __init__(self, px, py, vx, vy): turtle.Turtle.__init__(self) self.vx = vx self.vy = vy self.shape('circle') self.turtlesize(0.3) self.penup() self.setpos(px, py) def move(self): self.vy = self.vy - 0.981 new_px = self.xcor() + self.vx new_py = self.ycor() + self.vy if new_py 0: new_py = self.ycor() * 0.75 self.vy = -0.75 * self.vy self.setpos(new_px, new_py) def hit(self): print('space bar pressed') self.vx = -self.vx self.vy = 15class Game: def __init__(self): turtle.delay(0) turtle.tracer(0, 0) turtle.setworldcoordinates(-500, -500, 500, 500) self.ball = Ball( random.randint(-100, 100), random.randint(30, 100), random.randint(-12, 6), random.randint(4, 10) ) turtle.update() self.gameloop() turtle.onkeypress(self.ball.hit, "space") turtle.listen() turtle.mainloop() def gameloop(self): self.ball.move() turtle.update() turtle.ontimer(self.gameloop, 30)if __name__ == '__main__': Game()

Here is the problem:

image text in transcribedimage text in transcribed
Have Game's _init_ also draw a net (21 line that is 30 units tall) and a court (a line from (-400, 0) to (400, 0).) If the ball hits the net, or if it bounces twice on the same side, then the point is over and the ball should reset. Add code to the gameloop method that can detect if the ball hits the net. If this happens, have the ball reset to a new random initial position and velocity. You may want to create a separate Ball method for this, to avoid having redundant reset code in multiple places. Note: Actually checking whether the ball hits the net requires a bit. of math, but you can approximate this each step by checking whether the ball passes over the x : 0 line, and ends at a y-position below 30. If the ball bounces twice on the same side of the net, then the point is over and the ball should reset. Keep track of the number of times a ball bounces inside of the ball's move method, but. make sure to reset. this value to 0 every time the ball crosses from one side of the net to the other. Once you've implemented these two, the game will get signicantly harder! Feel free to tweak the values in the initialization and in the hit method to make it slightly easier to play

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_2

Step: 3

blur-text-image_3

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

Transport Operations

Authors: Allen Stuart

2nd Edition

978-0470115398, 0470115394

Students also viewed these Programming questions

Question

Answered: 1 week ago

Answered: 1 week ago