mercredi 10 janvier 2018

Python: How let the user choose to replay the game?

This was not an easy thing for a beginner like me to programme, but the task was to create an angry birds-like game where the user can input an angle and velocity at which to fire a ball and it hits a block. I have made a few while loops, and I had the game to a point that I liked. I then tried to loop the entire game code in order to let the user decide whether they wish to play on. When I tried this code, I have trouble getting the game code to restart, and the required labels appear after the while is broken, which does not make sense because I programmed them in the nested while. Please help me with this! Thank you! This is my complete code:

import numpy as np
from vpython import sphere, color, rate, canvas, vector, curve, label, box, cross, mag, random 
x0=0.0
y0=0.0
dt=0.01
g=9.81
xbox=10*random()+5
m=0.1
targetmass=100
collisiontime = 0.01


scene = canvas(width=640, height=480, center=vector(8,0,0),range=8)
ground = curve(pos=[(0,0,0),(16,0,0)],color=color.green)


r=0.05
t=0
y=0 
x=0
b=0
db=1
n=0
i=xbox
j=1
alpha=np.pi/2
xcollision=xbox+0.17
q=0


while True:
    target = box(pos=vector(xbox,1,0), height=2, width=0.5, depth=0.25, color=color.blue,origin=vector(xbox+0.5,0,0), axis=vector(0,0,0.5))
    bird = sphere(pos = vector(x0,y0,0),radius = 0.3,make_trail=True)
    degreetheta = float(input("Please input the initial angle in degrees: "))
    theta = np.radians(degreetheta)
    v0 = float(input("Please input the initial speed in metres/second: "))
    time=xbox/(v0*np.cos(theta))
    px = m*v0*np.cos(theta)
    py = m*v0*np.sin(theta)-m*g*time
    ycollision = v0*np.sin(theta)*time-0.5*g*time**2
    restoringT=(1/2)*targetmass*g
    downT=(py*(1+ycollision**2)**0.5)/0.01
    sideT=(px*(1+ycollision**2)**0.5)/0.01
    TotalRestoring=downT+restoringT
    while y>=0 and x<=xbox:
        rate(30.0)
        t=t+dt       
        x=x0+v0*t*np.cos(theta)
        y=y0+v0*t*np.sin(theta)-0.5*g*t**2
        bird.pos = vector(x,y,0)
    while x>xbox and n<1:
        if y>2:
            rate(30.0)
            t=t+dt       
            x=x0+v0*t*np.cos(theta)
            y=y0+v0*t*np.sin(theta)-0.5*g*t**2
            bird.pos = vector(x,y,0)
            label(pos=vector(5,7,0),text='You Miss!', font_size=100)
        elif sideT > restoringT:
            while b<10:
                rate(5)
                target.rotate(http://ift.tt/2APphmU)
                target.pos=vector(i,j,0)
                i=i+0.15*np.sin(alpha)
                j=j-0.15*np.cos(alpha)
                alpha=alpha-np.pi/20
                bird.pos=vector(xcollision,ycollision,0)
                ycollision=ycollision-0.1
                b=b+db
            label(pos=vector(5,7,0),text='You Win!', font_size=100)
        elif restoringT > sideT:
            label(pos=vector(5,7,0),text='You Lose!')
            while b<10:
                bird.pos=vector(xcollision,ycollision,0)
                ycollision=ycollision-0.1
                b=b+db
        else:
            break

        label(pos=vector(5,3,0),text='Type no-enter to exit.', font_size=100)
        n=n+1
    inp=input()
    if inp=="no":
        break
    else:
        continue

Aucun commentaire:

Enregistrer un commentaire