Programming Homework Help

University of Nairobi Simulating Gravity and Projectile Motion Project

 

The program requires me to do two things:

  • Add more player control to the game by allowing the player to change the angle of their shot. This reflects the original tennis for two, where each player’s controller was a knob to represent the angle of the shot, and a button. The angle of a player’s paddle should control how much force is applied in the x and y directions.
  • Add more player control to the game by allowing each player to hit the ball harder or softer. There are a few ways to handle this, you can have different keys for “soft hit” and “hard hit” or you can have some keys that control power up and power down.

First, we already have the code(As shown below) which simulates projectile motion, which has a ball class that includes init method(which sets the shape and initial position and velocity), move method(which sets how it moves) and hit method(which prints “hit”, reverses velocity and set velocity to 15) and a Game class which randomly set the position of the ball and run the game. We draw the motion of the ball using turtleimport turtle

import random

import math

class Ball(turtle.Turtle):

def __init__(self, px, py, vx, vy):

turtle.Turtle.__init__(self)

self.setpos(px, py)

self.vx = vx

self.vy = vy

self.shape(‘circle’)

self.turtlesize(0.3)

self.penup()

self.setpos(px, py)

self.bounce = 0

turtle.update()

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 = 0.75 * self.ycor()

self.vy = -0.75 * self.vy

self.bounce = self.bounce + 1

if self.xcor() < 0 and new_px > 0:

self.bounce = 0

if self.xcor() > 0 and new_px < 0:

self.bounce = 0

if self.bounce == 0 or self.bounce == 1:

self.setpos(new_px, new_py)

else:

self.bounce = 0

self.reset()

px = new_px

py = new_py

turtle.update()

def hit(self):

self.vx = -1 * vx

self.vy = 15

def reset(self):

px = random.uniform(-100, 100)

py = random.uniform(30, 100)

self.vx = random.choice([random.uniform(-12, -6), random.uniform(6, 12)])

self.vy = random.uniform(4, 10)

self.setpos(px, py)

turtle.update()

class Game:

def __init__(self):

t = turtle.Turtle()

t.penup()

turtle.setworldcoordinates(-500, -500, 500, 500)

t = turtle.Turtle()

t.penup()

t.setpos(-400, 0)

t.pendown()

t.goto(400, 0)

t.penup()

t.setpos(0, 0)

t.left(90)

t.pendown()

t.forward(30)

turtle.delay(0)

turtle.tracer(0,0)

px = random.uniform(-100, 100)

py = random.uniform(30, 100)

vx = random.choice([random.uniform(-12, -6), random.uniform(6, 12)])

vy = random.uniform(4, 10)

self.ball = Ball(px, py, vx, vy)

turtle.update()

self.gameloop()

turtle.onkeypress(self.ball.hit, “space”)

turtle.listen()

turtle.mainloop()

def gameloop(self):

if self.ball.xcor() == 0 and self.ball.ycor() < 30:

self.ball.reset()

self.ball.move()

turtle.ontimer(self.gameloop, 30)

turtle.update()

def main():

Game()

if __name__ == ‘__main__’:

main()