d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Simple Python Help 30fg
Add Reply New Topic New Poll
Member
Posts: 653
Joined: Mar 8 2012
Gold: 893.00
Nov 15 2012 07:45pm
I'm making a cannonball game for my csc 101 course and Im having trouble finishing it up, I am using python and Wing IDE if anyone could finish this for me ill give them 30 FG.

"Write a program to play a cannonball game (see figure below). The cannonball starts on the left side of the window at location (0,0). On the right side of the window, we have a group of five rectangles defining the traget: the green rectangles represent 1 point, the orange rectangles represent 3 points and the red rectangle represents 5 points.

In a single round of the game, the player can throw up to three cannonballs or accumulate a minimum of seven points --which ever condition comes first. Your program shall update the points earned by the user with each throw.

When the game is over, your program shall display the final score and ask the user if he or she wants to play again.

For the details on the inputs and motion of the cannonball, see lab 6.

The specific heights and width of the rectangles in the target are up to you. However, their composition must resemble the one in the figure below, and

The width of the green and orange rectangles are the same; and
The width of the red rectangle is one-and-a-half the width of an orange rectangle.
Define and use the following functions:

draw_rectangle(x1,y1,x2,y2,color):
This function draws a rectangle with opposite corners (x1,y1) and (x2,y2) filled with the specified color.
draw_target(): This function draws the five rectangles forming the target area.
point_in_rectangle(x1,y1,x2,y2,xp,yp):
This function returns True if the point (xp, yp) is inside the rectangle defined by corners (x1,y1) and (x2, y2).
hit_target(xp, yp):
This function returns 1, 3 or 5 if the point (xp, yp) is inside the green, orange or red rectangles, respectively. Otherwise, this function returns zero.
fire_cannonball(wind, gravity):
This function calculates and displays the trajectory of the cannonball given wind and gravity information. The cannonball is animated until it hits any of the rectangles in the target, hits the ground, or it is lost. This function returns zero if the cannonball is lost or hits the ground, or the value of the target rectangle hit by the cannonball."


Heres what I have so far, its almost done I just need the hit_target function finished and the rest of the stuff definined in main().



import random
import turtle

def draw_rectangle(x1,y1,x2,y2,color):
height = y2 - y1
width = x2 - x1

turtle.penup()
turtle.goto(x1, y1)
turtle.pendown()
turtle.color(color)
turtle.begin_fill()
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.end_fill()
turtle.left(90)



def draw_target():
turtle.setup(800,400)
turtle.setworldcoordinates(0,0,700,1000)
draw_rectangle(400,0,450,100, "green")
draw_rectangle(451,0,501,75, "orange")
draw_rectangle(502,0,577,50, "red")
draw_rectangle(578,0,628,75, "orange")
draw_rectangle(629,0,679,100, "green")
draw_rectangle(-2,-2,1400,0, "black")

def point_in_rectangle(x1,y1,x2,y2,xp,yp):
if xp > x1 and xp < x2 and yp > y1 and yp < y2:
tf_result = True
else:
tf_result = False
return tf_result

def hit_target(xp, yp):
if point_in_rectangle(400,0,50,100,xp,yp) :
points = 1
elif point_in_rectangle(451,0, 501,75, xp,yp):
points = 3
elif xp >= 502 and xp <= 577 and yp >= 0 and yp <= 50:
points = 5
elif xp >= 578 and xp <= 628 and yp >= 0 and yp <= 75:
points = 3
elif xp >= 629 and xp <= 679 and yp >= 0 and yp <= 100:
points = 1
elif xp >= -2 and xp <= 1400 and yp >= -2 and yp <= 0:
points = 0
return points



def fire_cannonball(wind, gravity):
print("The wind velocity is" , wind)
firing_angle = eval(input ("Input Firing angle:"))
force = eval(input ("Input Force:"))

dx = force * math.cos(math.radians(firing_angle))
dy = force * math.sin(math.radians(firing_angle))

xp = 0
yp = 0
dt = .05

GRAVITY = -9.81
turtle.showturtle()
while yp >= 0:
xp = xp + dx * dt
yp = yp + dy * dt

dx = dx + wind
dy = dy + GRAVITY
turtle.goto(xp, yp)

points = hit_target(xp, yp)
if points != 0:
break
turtle.stamp()

return points

def main():

GRAVITY = -9.81
more = 'Y'
total_score = 0
while more == 'Y':
turle.clear()
draw_target()
wind = random.randint(-5,5)
score_this_game = fire_cannonball()
#add score_this_game to total_score

#ask "play again?" in variable more

main()
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 16 2012 12:24am
30fg will get you hello world, with your name on it

This post was edited by irimi on Nov 16 2012 12:24am
Member
Posts: 25,794
Joined: Oct 7 2005
Gold: 9,510.00
Nov 29 2012 02:47am
Quote (irimi @ Nov 15 2012 10:24pm)
30fg will get you hello world, with your name on it


^ this
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll