
| importturtle | |
| importtime | |
| importrandom | |
| importmath | |
| importsys | |
| fromplaysoundimportplaysound | |
| width=1000 | |
| height=600 | |
| points=0 | |
| gas=150 | |
| gas_limit=500 | |
| max_dist=math.sqrt((width/2*width/2) + (height/2*height/2)) | |
| max_garbage=15 | |
| size=5 | |
| k=35 | |
| garbage_arr= [] | |
| filename='tomar.gif' | |
| file2='earth.gif' | |
| file3='sun.gif' | |
| file4='sat1.gif' | |
| dist_sun=1000 | |
| tomar=False | |
| screen=turtle.Screen() | |
| screen.title('VALHALLA: SPACE JUNK ELIMINATION GAME') | |
| screen.bgcolor('black') | |
| screen.setup(width=width, height=height) | |
| screen.tracer(0) | |
| screen.register_shape(filename) | |
| screen.register_shape(file2) | |
| screen.register_shape(file3) | |
| screen.register_shape(file4) | |
| #PLANETA | |
| planet=turtle.Turtle() | |
| planet.speed(0) | |
| planet.shape(file2) | |
| planet.penup() | |
| planet.goto(0,0) | |
| #SOL | |
| sun=turtle.Turtle() | |
| sun.speed(0) | |
| sun.shape(file3) | |
| sun.penup() | |
| sun.goto(width/2-50, height/2-50) | |
| #SCORE [][][][][][][][][][][][][][][] | |
| score=turtle.Turtle() | |
| score.speed(0) | |
| score.shape('square') | |
| score.color('white') | |
| score.penup() | |
| score.hideturtle() | |
| score.goto(-width/2+5,height/2-75) | |
| marcador="Debris Collection: {} \nEnergy: {}".format(points, gas) | |
| score.write(marcador, font=('Courier', 15, 'bold')) | |
| #PLAYER | |
| ship=turtle.Turtle() | |
| ship.speed(0) | |
| ship.penup() | |
| ship.shape(filename) | |
| #ship.shape('square') | |
| ship.color('green') | |
| ship.goto(-width/2+50,0) | |
| ship.accel=2 | |
| ship.step=20 | |
| ship.d=1000 | |
| ship.rot=10 | |
| #Asteroide 0 | |
| asteroid0=turtle.Turtle() | |
| asteroid0.speed(0) | |
| asteroid0.penup() | |
| asteroid0.shape(file4) | |
| asteroid0.r=110 | |
| asteroid0.x=asteroid0.r | |
| asteroid0.y=0 | |
| asteroid0.goto(asteroid0.x,asteroid0.y) | |
| asteroid0.vel=-1 | |
| asteroid0.delta=10 | |
| #Asteroide 1 | |
| asteroid1=turtle.Turtle() | |
| asteroid1.speed(0) | |
| asteroid1.penup() | |
| asteroid1.shape(file4) | |
| asteroid1.r=165 | |
| asteroid1.x=-asteroid1.r | |
| asteroid1.y=0 | |
| asteroid1.goto(asteroid1.x,asteroid1.y) | |
| asteroid1.vel=1 | |
| asteroid1.delta=5 | |
| #Asteroide 2 | |
| asteroid2=turtle.Turtle() | |
| asteroid2.speed(0) | |
| asteroid2.penup() | |
| asteroid2.shape(file4) | |
| asteroid2.r=225 | |
| asteroid2.x=0 | |
| asteroid2.y=asteroid2.r | |
| asteroid2.goto(asteroid2.x,asteroid2.y) | |
| asteroid2.vel=1 | |
| asteroid2.delta=1 | |
| defcreate_garbage(x, y): | |
| iflen(garbage_arr) <max_garbage: | |
| garbage=turtle.Turtle() | |
| garbage.speed(0) | |
| garbage.penup() | |
| garbage.shape('square') | |
| garbage.color('gray') | |
| garbage.goto(x,y) | |
| garbage.dx=random.randint(-10,10) | |
| garbage.dy=random.randint(-10,10) | |
| garbage_arr.append(garbage) | |
| defgravity(): | |
| dist_x=planet.xcor() -ship.xcor() | |
| dist_y=planet.ycor() -ship.ycor() | |
| x_dir=1 | |
| y_dir=1 | |
| ifdist_x<0: | |
| x_dir=-1 | |
| ifdist_y<0: | |
| y_dir=-1 | |
| d=math.sqrt(dist_x*dist_x+dist_y*dist_y) | |
| ship.d=d | |
| force=k*size/d | |
| ship.accel+=force | |
| ship.setpos(ship.xcor()+ship.accel*x_dir, ship.ycor()+ship.accel*y_dir) | |
| defcheck_garbage(arr): | |
| globalpoints | |
| globalmax_garbage | |
| forgarbageinarr: | |
| ifship.xcor() >garbage.xcor()-15andship.xcor() <garbage.xcor()+15: | |
| ifship.ycor() >garbage.ycor()-15andship.ycor() <garbage.ycor()+15: | |
| #collide and erase! | |
| garbage.hideturtle() | |
| arr.remove(garbage) | |
| points+=1 | |
| ifpoints%5==0: | |
| max_garbage-=1 | |
| ifmax_garbage<=3: | |
| win() | |
| arr[len(arr)-1].hideturtle() | |
| arr.pop() | |
| score.clear() | |
| score.goto(100-width/4,0) | |
| score.write("LEVEL {}".format(math.ceil(points/5)+1), font=('Courier', 42, 'bold')) | |
| playsound('bip2.wav') | |
| time.sleep(1.5) | |
| defmove_trash(arr): | |
| foriteminarr: | |
| x=item.xcor()+item.dx | |
| y=item.ycor()+item.dy | |
| if (x<=-width/2+10orx>=width/2-10): | |
| item.dx=item.dx*-1 | |
| if (y<=-height/2+10ory>=height/2-10): | |
| item.dy=item.dy*-1 | |
| item.setpos(x, y) | |
| defmove_asteroids(asteroid): | |
| asteroid.x=asteroid.x+asteroid.vel*asteroid.delta | |
| rad2=asteroid.r*asteroid.r | |
| asteroid.y=asteroid.vel*math.sqrt(rad2- (asteroid.x*asteroid.x)) | |
| ifasteroid.x==asteroid.rorasteroid.x==-asteroid.r: | |
| asteroid.vel=asteroid.vel*-1 | |
| asteroid.setpos(asteroid.x, asteroid.y) | |
| defup(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.sety(ship.ycor()+ship.step) | |
| gas-=1 | |
| defthrust(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.forward(ship.step) | |
| gas-=1 | |
| defdown(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.sety(ship.ycor()-ship.step) | |
| gas-=1 | |
| defright(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.setx(ship.xcor()+ship.step) | |
| gas-=1 | |
| defleft(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.setx(ship.xcor()-ship.step) | |
| gas-=1 | |
| defgameover(v): | |
| score.clear() | |
| score.goto(50-width/3,-50) | |
| score.write("GAME OVER\n{}".format(v), font=('Courier', 50, 'bold')) | |
| time.sleep(3) | |
| sys.exit() | |
| defrotR(): | |
| angle=ship.heading() | |
| #print(angle) | |
| ship.ht() | |
| ship.setheading(angle+ship.rot) | |
| ship.st() | |
| defrotL(): | |
| angle=ship.heading() | |
| #print(angle) | |
| ship.ht() | |
| ship.setheading(angle-ship.rot) | |
| ship.st() | |
| defwin(): | |
| score.clear() | |
| score.goto(25-width/3,height/4-250) | |
| score.write("The estimated population of\nparticles between 1 to 10 cm\nareapproximately 500,000\nNASA is very thankful\nfor your services!!!", font=('Courier', 30, 'bold')) | |
| playsound('win.wav') | |
| time.sleep(5) | |
| sys.exit() | |
| definit(): | |
| globaltomar | |
| tomar=nottomar | |
| screen.listen() | |
| screen.onkeypress(up, 'Up') | |
| screen.onkeypress(down, 'Down') | |
| screen.onkeypress(right, 'Right') | |
| screen.onkeypress(left, 'Left') | |
| screen.onkeypress(init, 'space') | |
| screen.onkeypress(rotR, "a") | |
| screen.onkeypress(rotL, "d") | |
| screen.onkeypress(thrust, "w") | |
| whileTrue: | |
| iftomar: | |
| win() | |
| move_asteroids(asteroid0) | |
| move_asteroids(asteroid1) | |
| move_asteroids(asteroid2) | |
| #Colisiones con los asteroides | |
| arr= ['Calipso', 'Cloudsat', 'Glory'] | |
| num=random.randint(0,2) | |
| ifship.xcor() >asteroid1.xcor()-8andship.xcor() <asteroid1.xcor()+8: | |
| ifship.ycor() >asteroid1.ycor()-8andship.ycor() <asteroid1.ycor()+8: | |
| #choco con asteroide1 | |
| gameover("You crashed with\n"+arr[num]) | |
| ifship.xcor() >asteroid2.xcor()-8andship.xcor() <asteroid2.xcor()+8: | |
| ifship.ycor() >asteroid2.ycor()-8andship.ycor() <asteroid2.ycor()+8: | |
| #choco con asteroide2 | |
| gameover("You crashed with\n"+arr[num]) | |
| ifship.xcor() >asteroid0.xcor()-8andship.xcor() <asteroid0.xcor()+8: | |
| ifship.ycor() >asteroid0.ycor()-8andship.ycor() <asteroid0.ycor()+8: | |
| #choco con asteroide0 | |
| gameover("You crashed with\n"+arr[num]) | |
| #Colision con el planeta | |
| ifship.d<85: #choco | |
| gameover("") | |
| x=random.randint(0,width/2-30) | |
| y=random.randint(0,height/2-30) | |
| prob=random.randint(0,100) | |
| ifprob>85: #agregar una basura espacial | |
| create_garbage(x, y) | |
| elifprob>70: | |
| create_garbage(-x,y) | |
| elifprob>50: | |
| create_garbage(x,-y) | |
| elifprob>35: | |
| create_garbage(-x,-y) | |
| move_trash(garbage_arr) | |
| dist_sun=math.ceil(math.sqrt((sun.xcor()-ship.xcor())**2+ (sun.ycor()-ship.ycor())**2)/ship.step)-5 | |
| #solar panel | |
| ifdist_sun<=2: | |
| gameover("") | |
| elifdist_sun<=4: | |
| score.color('dark orange') | |
| ifgas<gas_limit: | |
| gas+=3 | |
| elifdist_sun<=7: | |
| score.color('forest green') | |
| ifgas<gas_limit: | |
| gas+=1 | |
| elifgas<50: | |
| score.color('red') | |
| else: | |
| score.color('white') | |
| #collect garbage | |
| check_garbage(garbage_arr) | |
| gas_to_rect="" | |
| forxinrange(0,gas,50): | |
| gas_to_rect=gas_to_rect+"[]" | |
| gravity() | |
| score.clear() | |
| score.goto(-width/2+5,height/2-75) | |
| marcador="Debris Collection: {} \nEnergy: {} \nTrip to Sun: {}".format(points, gas_to_rect, dist_sun) | |
| score.write(marcador, font=('Courier', 15, 'bold')) | |
| else: | |
| score.clear() | |
| score.color('white') | |
| score.goto(25-width/3,0) | |
| score.write("The space need you!\nPress your SpaceBar", font=('Courier', 38, 'bold')) | |
| time.sleep(0.08) | |
| screen.update() |





| importturtle | |
| importtime | |
| importrandom | |
| importmath | |
| importsys | |
| fromplaysoundimportplaysound | |
| width=1000 | |
| height=600 | |
| points=0 | |
| gas=150 | |
| gas_limit=500 | |
| max_dist=math.sqrt((width/2*width/2) + (height/2*height/2)) | |
| max_garbage=15 | |
| size=5 | |
| k=35 | |
| garbage_arr= [] | |
| filename='tomar.gif' | |
| file2='earth.gif' | |
| file3='sun.gif' | |
| file4='sat1.gif' | |
| dist_sun=1000 | |
| tomar=False | |
| screen=turtle.Screen() | |
| screen.title('VALHALLA: SPACE JUNK ELIMINATION GAME') | |
| screen.bgcolor('black') | |
| screen.setup(width=width, height=height) | |
| screen.tracer(0) | |
| screen.register_shape(filename) | |
| screen.register_shape(file2) | |
| screen.register_shape(file3) | |
| screen.register_shape(file4) | |
| #PLANETA | |
| planet=turtle.Turtle() | |
| planet.speed(0) | |
| planet.shape(file2) | |
| planet.penup() | |
| planet.goto(0,0) | |
| #SOL | |
| sun=turtle.Turtle() | |
| sun.speed(0) | |
| sun.shape(file3) | |
| sun.penup() | |
| sun.goto(width/2-50, height/2-50) | |
| #SCORE [][][][][][][][][][][][][][][] | |
| score=turtle.Turtle() | |
| score.speed(0) | |
| score.shape('square') | |
| score.color('white') | |
| score.penup() | |
| score.hideturtle() | |
| score.goto(-width/2+5,height/2-75) | |
| marcador="Debris Collection: {} \nEnergy: {}".format(points, gas) | |
| score.write(marcador, font=('Courier', 15, 'bold')) | |
| #PLAYER | |
| ship=turtle.Turtle() | |
| ship.speed(0) | |
| ship.penup() | |
| ship.shape(filename) | |
| #ship.shape('square') | |
| ship.color('green') | |
| ship.goto(-width/2+50,0) | |
| ship.accel=2 | |
| ship.step=20 | |
| ship.d=1000 | |
| ship.rot=10 | |
| #Asteroide 0 | |
| asteroid0=turtle.Turtle() | |
| asteroid0.speed(0) | |
| asteroid0.penup() | |
| asteroid0.shape(file4) | |
| asteroid0.r=110 | |
| asteroid0.x=asteroid0.r | |
| asteroid0.y=0 | |
| asteroid0.goto(asteroid0.x,asteroid0.y) | |
| asteroid0.vel=-1 | |
| asteroid0.delta=10 | |
| #Asteroide 1 | |
| asteroid1=turtle.Turtle() | |
| asteroid1.speed(0) | |
| asteroid1.penup() | |
| asteroid1.shape(file4) | |
| asteroid1.r=165 | |
| asteroid1.x=-asteroid1.r | |
| asteroid1.y=0 | |
| asteroid1.goto(asteroid1.x,asteroid1.y) | |
| asteroid1.vel=1 | |
| asteroid1.delta=5 | |
| #Asteroide 2 | |
| asteroid2=turtle.Turtle() | |
| asteroid2.speed(0) | |
| asteroid2.penup() | |
| asteroid2.shape(file4) | |
| asteroid2.r=225 | |
| asteroid2.x=0 | |
| asteroid2.y=asteroid2.r | |
| asteroid2.goto(asteroid2.x,asteroid2.y) | |
| asteroid2.vel=1 | |
| asteroid2.delta=1 | |
| defcreate_garbage(x, y): | |
| iflen(garbage_arr) <max_garbage: | |
| garbage=turtle.Turtle() | |
| garbage.speed(0) | |
| garbage.penup() | |
| garbage.shape('square') | |
| garbage.color('gray') | |
| garbage.goto(x,y) | |
| garbage.dx=random.randint(-10,10) | |
| garbage.dy=random.randint(-10,10) | |
| garbage_arr.append(garbage) | |
| defgravity(): | |
| dist_x=planet.xcor() -ship.xcor() | |
| dist_y=planet.ycor() -ship.ycor() | |
| x_dir=1 | |
| y_dir=1 | |
| ifdist_x<0: | |
| x_dir=-1 | |
| ifdist_y<0: | |
| y_dir=-1 | |
| d=math.sqrt(dist_x*dist_x+dist_y*dist_y) | |
| ship.d=d | |
| force=k*size/d | |
| ship.accel+=force | |
| ship.setpos(ship.xcor()+ship.accel*x_dir, ship.ycor()+ship.accel*y_dir) | |
| defcheck_garbage(arr): | |
| globalpoints | |
| globalmax_garbage | |
| forgarbageinarr: | |
| ifship.xcor() >garbage.xcor()-15andship.xcor() <garbage.xcor()+15: | |
| ifship.ycor() >garbage.ycor()-15andship.ycor() <garbage.ycor()+15: | |
| #collide and erase! | |
| garbage.hideturtle() | |
| arr.remove(garbage) | |
| points+=1 | |
| ifpoints%5==0: | |
| max_garbage-=1 | |
| ifmax_garbage<=3: | |
| win() | |
| arr[len(arr)-1].hideturtle() | |
| arr.pop() | |
| score.clear() | |
| score.goto(100-width/4,0) | |
| score.write("LEVEL {}".format(math.ceil(points/5)+1), font=('Courier', 42, 'bold')) | |
| playsound('bip2.wav') | |
| time.sleep(1.5) | |
| defmove_trash(arr): | |
| foriteminarr: | |
| x=item.xcor()+item.dx | |
| y=item.ycor()+item.dy | |
| if (x<=-width/2+10orx>=width/2-10): | |
| item.dx=item.dx*-1 | |
| if (y<=-height/2+10ory>=height/2-10): | |
| item.dy=item.dy*-1 | |
| item.setpos(x, y) | |
| defmove_asteroids(asteroid): | |
| asteroid.x=asteroid.x+asteroid.vel*asteroid.delta | |
| rad2=asteroid.r*asteroid.r | |
| asteroid.y=asteroid.vel*math.sqrt(rad2- (asteroid.x*asteroid.x)) | |
| ifasteroid.x==asteroid.rorasteroid.x==-asteroid.r: | |
| asteroid.vel=asteroid.vel*-1 | |
| asteroid.setpos(asteroid.x, asteroid.y) | |
| defup(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.sety(ship.ycor()+ship.step) | |
| gas-=1 | |
| defthrust(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.forward(ship.step) | |
| gas-=1 | |
| defdown(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.sety(ship.ycor()-ship.step) | |
| gas-=1 | |
| defright(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.setx(ship.xcor()+ship.step) | |
| gas-=1 | |
| defleft(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.setx(ship.xcor()-ship.step) | |
| gas-=1 | |
| defgameover(v): | |
| score.clear() | |
| score.goto(50-width/3,-50) | |
| score.write("GAME OVER\n{}".format(v), font=('Courier', 50, 'bold')) | |
| time.sleep(3) | |
| sys.exit() | |
| defrotR(): | |
| angle=ship.heading() | |
| #print(angle) | |
| ship.ht() | |
| ship.setheading(angle+ship.rot) | |
| ship.st() | |
| defrotL(): | |
| angle=ship.heading() | |
| #print(angle) | |
| ship.ht() | |
| ship.setheading(angle-ship.rot) | |
| ship.st() | |
| defwin(): | |
| score.clear() | |
| score.goto(25-width/3,height/4-250) | |
| score.write("The estimated population of\nparticles between 1 to 10 cm\nareapproximately 500,000\nNASA is very thankful\nfor your services!!!", font=('Courier', 30, 'bold')) | |
| playsound('win.wav') | |
| time.sleep(5) | |
| sys.exit() | |
| definit(): | |
| globaltomar | |
| tomar=nottomar | |
| screen.listen() | |
| screen.onkeypress(up, 'Up') | |
| screen.onkeypress(down, 'Down') | |
| screen.onkeypress(right, 'Right') | |
| screen.onkeypress(left, 'Left') | |
| screen.onkeypress(init, 'space') | |
| screen.onkeypress(rotR, "a") | |
| screen.onkeypress(rotL, "d") | |
| screen.onkeypress(thrust, "w") | |
| whileTrue: | |
| iftomar: | |
| win() | |
| move_asteroids(asteroid0) | |
| move_asteroids(asteroid1) | |
| move_asteroids(asteroid2) | |
| #Colisiones con los asteroides | |
| arr= ['Calipso', 'Cloudsat', 'Glory'] | |
| num=random.randint(0,2) | |
| ifship.xcor() >asteroid1.xcor()-8andship.xcor() <asteroid1.xcor()+8: | |
| ifship.ycor() >asteroid1.ycor()-8andship.ycor() <asteroid1.ycor()+8: | |
| #choco con asteroide1 | |
| gameover("You crashed with\n"+arr[num]) | |
| ifship.xcor() >asteroid2.xcor()-8andship.xcor() <asteroid2.xcor()+8: | |
| ifship.ycor() >asteroid2.ycor()-8andship.ycor() <asteroid2.ycor()+8: | |
| #choco con asteroide2 | |
| gameover("You crashed with\n"+arr[num]) | |
| ifship.xcor() >asteroid0.xcor()-8andship.xcor() <asteroid0.xcor()+8: | |
| ifship.ycor() >asteroid0.ycor()-8andship.ycor() <asteroid0.ycor()+8: | |
| #choco con asteroide0 | |
| gameover("You crashed with\n"+arr[num]) | |
| #Colision con el planeta | |
| ifship.d<85: #choco | |
| gameover("") | |
| x=random.randint(0,width/2-30) | |
| y=random.randint(0,height/2-30) | |
| prob=random.randint(0,100) | |
| ifprob>85: #agregar una basura espacial | |
| create_garbage(x, y) | |
| elifprob>70: | |
| create_garbage(-x,y) | |
| elifprob>50: | |
| create_garbage(x,-y) | |
| elifprob>35: | |
| create_garbage(-x,-y) | |
| move_trash(garbage_arr) | |
| dist_sun=math.ceil(math.sqrt((sun.xcor()-ship.xcor())**2+ (sun.ycor()-ship.ycor())**2)/ship.step)-5 | |
| #solar panel | |
| ifdist_sun<=2: | |
| gameover("") | |
| elifdist_sun<=4: | |
| score.color('dark orange') | |
| ifgas<gas_limit: | |
| gas+=3 | |
| elifdist_sun<=7: | |
| score.color('forest green') | |
| ifgas<gas_limit: | |
| gas+=1 | |
| elifgas<50: | |
| score.color('red') | |
| else: | |
| score.color('white') | |
| #collect garbage | |
| check_garbage(garbage_arr) | |
| gas_to_rect="" | |
| forxinrange(0,gas,50): | |
| gas_to_rect=gas_to_rect+"[]" | |
| gravity() | |
| score.clear() | |
| score.goto(-width/2+5,height/2-75) | |
| marcador="Debris Collection: {} \nEnergy: {} \nTrip to Sun: {}".format(points, gas_to_rect, dist_sun) | |
| score.write(marcador, font=('Courier', 15, 'bold')) | |
| else: | |
| score.clear() | |
| score.color('white') | |
| score.goto(25-width/3,0) | |
| score.write("The space need you!\nPress your SpaceBar", font=('Courier', 38, 'bold')) | |
| time.sleep(0.08) | |
| screen.update() |
Para conocer mas de nuestro proyecto:
Happy Coding!
| importturtle | |
| importtime | |
| importrandom | |
| importmath | |
| importsys | |
| width=1000 | |
| height=600 | |
| points=0 | |
| gas=250 | |
| gas_limit=500 | |
| max_dist=math.sqrt((width/2*width/2) + (height/2*height/2)) | |
| max_garbage=7 | |
| size=5 | |
| k=25 | |
| garbage_arr= [] | |
| filename='nian.gif' | |
| dist_sun=0 | |
| screen=turtle.Screen() | |
| screen.title('VALHALLA: SPACE JUNK ELIMINATION GAME') | |
| screen.bgcolor('black') | |
| screen.setup(width=width, height=height) | |
| screen.tracer(0) | |
| screen.register_shape(filename) | |
| #PLANETA | |
| planet=turtle.Turtle() | |
| planet.speed(0) | |
| planet.shape('circle') | |
| planet.penup() | |
| planet.shapesize(stretch_wid=size, stretch_len=size) | |
| planet.color('blue') | |
| planet.goto(0,0) | |
| #SOL | |
| sun=turtle.Turtle() | |
| sun.speed(0) | |
| sun.shape('circle') | |
| sun.penup() | |
| sun.shapesize(stretch_wid=9, stretch_len=9) | |
| sun.color('orange') | |
| sun.goto(width/2-70, height/2-70) | |
| #SCORE | |
| score=turtle.Turtle() | |
| score.speed(0) | |
| score.shape('square') | |
| score.color('white') | |
| score.penup() | |
| score.hideturtle() | |
| score.goto(-width/2+5,height/2-75) | |
| marcador="Debris Collection: {} \nEnergy: {}".format(points, gas) | |
| score.write(marcador, font=('Courier', 15, 'bold')) | |
| #PLAYER | |
| ship=turtle.Turtle() | |
| ship.speed(0) | |
| ship.penup() | |
| ship.shape(filename) | |
| ship.goto(-width/2+50,0) | |
| ship.accel=2 | |
| ship.step=20 | |
| ship.d=1000 | |
| #Asteroide 0 | |
| asteroid0=turtle.Turtle() | |
| asteroid0.speed(0) | |
| asteroid0.penup() | |
| asteroid0.shape('circle') | |
| asteroid0.color('crimson') | |
| asteroid0.r=110 | |
| asteroid0.x=asteroid0.r | |
| asteroid0.y=0 | |
| asteroid0.goto(asteroid0.x,asteroid0.y) | |
| asteroid0.vel=-1 | |
| asteroid0.delta=5 | |
| #Asteroide 1 | |
| asteroid1=turtle.Turtle() | |
| asteroid1.speed(0) | |
| asteroid1.penup() | |
| asteroid1.shape('circle') | |
| asteroid1.color('medium purple') | |
| asteroid1.r=165 | |
| asteroid1.x=-asteroid1.r | |
| asteroid1.y=0 | |
| asteroid1.goto(asteroid1.x,asteroid1.y) | |
| asteroid1.vel=1 | |
| asteroid1.delta=10 | |
| #Asteroide 2 | |
| asteroid2=turtle.Turtle() | |
| asteroid2.speed(0) | |
| asteroid2.penup() | |
| asteroid2.shape('circle') | |
| asteroid2.color('teal') | |
| asteroid2.r=225 | |
| asteroid2.x=0 | |
| asteroid2.y=asteroid2.r | |
| asteroid2.goto(asteroid2.x,asteroid2.y) | |
| asteroid2.vel=1 | |
| asteroid2.delta=5 | |
| defcreate_garbage(x, y): | |
| iflen(garbage_arr) <max_garbage: | |
| garbage=turtle.Turtle() | |
| garbage.speed(0) | |
| garbage.penup() | |
| garbage.shape('square') | |
| garbage.color('gray') | |
| garbage.goto(x+50,y) | |
| garbage_arr.append(garbage) | |
| defgravity(): | |
| dist_x=planet.xcor() -ship.xcor() | |
| dist_y=planet.ycor() -ship.ycor() | |
| x_dir=1 | |
| y_dir=1 | |
| ifdist_x<0: | |
| x_dir=-1 | |
| ifdist_y<0: | |
| y_dir=-1 | |
| d=math.sqrt(dist_x*dist_x+dist_y*dist_y) | |
| ship.d=d | |
| force=k*size/d | |
| ship.accel+=force | |
| ship.setpos(ship.xcor()+ship.accel*x_dir, ship.ycor()+ship.accel*y_dir) | |
| defcheck_garbage(arr): | |
| globalpoints | |
| forgarbageinarr: | |
| ifship.xcor() >garbage.xcor()-15andship.xcor() <garbage.xcor()+15: | |
| ifship.ycor() >garbage.ycor()-15andship.ycor() <garbage.ycor()+15: | |
| #collide and erase! | |
| garbage.hideturtle() | |
| arr.remove(garbage) | |
| points+=1 | |
| defmove_trash(arr): | |
| foriteminarr: | |
| item.setpos(item.xcor()+random.randrange(-25,25), item.ycor()+random.randrange(-25,25)) | |
| defmove_asteroids(asteroid): | |
| asteroid.x=asteroid.x+asteroid.vel*asteroid.delta | |
| rad2=asteroid.r*asteroid.r | |
| asteroid.y=asteroid.vel*math.sqrt(rad2- (asteroid.x*asteroid.x)) | |
| ifasteroid.x==asteroid.rorasteroid.x==-asteroid.r: | |
| asteroid.vel=asteroid.vel*-1 | |
| asteroid.setpos(asteroid.x, asteroid.y) | |
| defup(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.sety(ship.ycor()+ship.step) | |
| gas-=1 | |
| defdown(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.sety(ship.ycor()-ship.step) | |
| gas-=1 | |
| defright(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.setx(ship.xcor()+ship.step) | |
| gas-=1 | |
| defleft(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.setx(ship.xcor()-ship.step) | |
| gas-=1 | |
| defgameover(): | |
| score.clear() | |
| score.goto(100-width/4,0) | |
| score.write("GAME OVER", font=('Courier', 40, 'bold')) | |
| time.sleep(2) | |
| sys.exit() | |
| screen.listen() | |
| screen.onkeypress(up, 'Up') | |
| screen.onkeypress(down, 'Down') | |
| screen.onkeypress(right, 'Right') | |
| screen.onkeypress(left, 'Left') | |
| whileTrue: | |
| move_asteroids(asteroid0) | |
| move_asteroids(asteroid1) | |
| move_asteroids(asteroid2) | |
| #Colisiones con los asteroides | |
| ifship.xcor() >asteroid1.xcor()-5andship.xcor() <asteroid1.xcor()+5: | |
| ifship.ycor() >asteroid1.ycor()-5andship.ycor() <asteroid1.ycor()+5: | |
| #choco con asteroide1 | |
| gameover() | |
| ifship.xcor() >asteroid2.xcor()-5andship.xcor() <asteroid2.xcor()+5: | |
| ifship.ycor() >asteroid2.ycor()-5andship.ycor() <asteroid2.ycor()+5: | |
| #choco con asteroide2 | |
| gameover() | |
| ifship.xcor() >asteroid0.xcor()-5andship.xcor() <asteroid0.xcor()+5: | |
| ifship.ycor() >asteroid0.ycor()-5andship.ycor() <asteroid0.ycor()+5: | |
| #choco con asteroide0 | |
| gameover() | |
| #Colision con el planeta | |
| ifship.d<50 : #choco | |
| gameover() | |
| x=random.randint(0,width/2) | |
| y=random.randint(0,height/2) | |
| prob=random.randint(0,100) | |
| ifprob>85: #agregar una basura espacial | |
| create_garbage(x, y) | |
| elifprob>70: | |
| create_garbage(-x,y) | |
| elifprob>50: | |
| create_garbage(x,-y) | |
| elifprob>35: | |
| create_garbage(-x,-y) | |
| move_trash(garbage_arr) | |
| #solar panel | |
| if (ship.xcor()>sun.xcor()-100andship.xcor()<sun.xcor()+100) and (ship.ycor()>sun.ycor()-100andship.ycor()<sun.ycor()+100): | |
| ifgas<gas_limit: | |
| gas+=5 | |
| #collect garbage | |
| check_garbage(garbage_arr) | |
| dist_sun=math.ceil(math.sqrt((sun.xcor()-ship.xcor())**2+ (sun.ycor()-ship.ycor())**2)/ship.step)-5 | |
| gravity() | |
| score.clear() | |
| marcador="Debris Collection: {} \nEnergy: {} \nTrip to Sun: {}".format(points, gas, dist_sun) | |
| score.write(marcador, font=('Courier', 15, 'bold')) | |
| time.sleep(0.08) | |
| screen.update() |


| importturtle | |
| importtime | |
| importrandom | |
| importmath | |
| importsys | |
| width=1000 | |
| height=600 | |
| points=0 | |
| gas=250 | |
| gas_limit=500 | |
| max_dist=math.sqrt((width/2*width/2) + (height/2*height/2)) | |
| max_garbage=7 | |
| size=5 | |
| k=25 | |
| garbage_arr= [] | |
| filename='nian.gif' | |
| dist_sun=0 | |
| screen=turtle.Screen() | |
| screen.title('VALHALLA: SPACE JUNK ELIMINATION GAME') | |
| screen.bgcolor('black') | |
| screen.setup(width=width, height=height) | |
| screen.tracer(0) | |
| screen.register_shape(filename) | |
| #PLANETA | |
| planet=turtle.Turtle() | |
| planet.speed(0) | |
| planet.shape('circle') | |
| planet.penup() | |
| planet.shapesize(stretch_wid=size, stretch_len=size) | |
| planet.color('blue') | |
| planet.goto(0,0) | |
| #SOL | |
| sun=turtle.Turtle() | |
| sun.speed(0) | |
| sun.shape('circle') | |
| sun.penup() | |
| sun.shapesize(stretch_wid=9, stretch_len=9) | |
| sun.color('orange') | |
| sun.goto(width/2-70, height/2-70) | |
| #SCORE | |
| score=turtle.Turtle() | |
| score.speed(0) | |
| score.shape('square') | |
| score.color('white') | |
| score.penup() | |
| score.hideturtle() | |
| score.goto(-width/2+5,height/2-75) | |
| marcador="Debris Collection: {} \nEnergy: {}".format(points, gas) | |
| score.write(marcador, font=('Courier', 15, 'bold')) | |
| #PLAYER | |
| ship=turtle.Turtle() | |
| ship.speed(0) | |
| ship.penup() | |
| ship.shape(filename) | |
| ship.goto(-width/2+50,0) | |
| ship.accel=2 | |
| ship.step=20 | |
| ship.d=1000 | |
| #Asteroide 0 | |
| asteroid0=turtle.Turtle() | |
| asteroid0.speed(0) | |
| asteroid0.penup() | |
| asteroid0.shape('circle') | |
| asteroid0.color('crimson') | |
| asteroid0.r=110 | |
| asteroid0.x=asteroid0.r | |
| asteroid0.y=0 | |
| asteroid0.goto(asteroid0.x,asteroid0.y) | |
| asteroid0.vel=-1 | |
| asteroid0.delta=5 | |
| #Asteroide 1 | |
| asteroid1=turtle.Turtle() | |
| asteroid1.speed(0) | |
| asteroid1.penup() | |
| asteroid1.shape('circle') | |
| asteroid1.color('medium purple') | |
| asteroid1.r=165 | |
| asteroid1.x=-asteroid1.r | |
| asteroid1.y=0 | |
| asteroid1.goto(asteroid1.x,asteroid1.y) | |
| asteroid1.vel=1 | |
| asteroid1.delta=10 | |
| #Asteroide 2 | |
| asteroid2=turtle.Turtle() | |
| asteroid2.speed(0) | |
| asteroid2.penup() | |
| asteroid2.shape('circle') | |
| asteroid2.color('teal') | |
| asteroid2.r=225 | |
| asteroid2.x=0 | |
| asteroid2.y=asteroid2.r | |
| asteroid2.goto(asteroid2.x,asteroid2.y) | |
| asteroid2.vel=1 | |
| asteroid2.delta=5 | |
| defcreate_garbage(x, y): | |
| iflen(garbage_arr) <max_garbage: | |
| garbage=turtle.Turtle() | |
| garbage.speed(0) | |
| garbage.penup() | |
| garbage.shape('square') | |
| garbage.color('gray') | |
| garbage.goto(x+50,y) | |
| garbage_arr.append(garbage) | |
| defgravity(): | |
| dist_x=planet.xcor() -ship.xcor() | |
| dist_y=planet.ycor() -ship.ycor() | |
| x_dir=1 | |
| y_dir=1 | |
| ifdist_x<0: | |
| x_dir=-1 | |
| ifdist_y<0: | |
| y_dir=-1 | |
| d=math.sqrt(dist_x*dist_x+dist_y*dist_y) | |
| ship.d=d | |
| force=k*size/d | |
| ship.accel+=force | |
| ship.setpos(ship.xcor()+ship.accel*x_dir, ship.ycor()+ship.accel*y_dir) | |
| defcheck_garbage(arr): | |
| globalpoints | |
| forgarbageinarr: | |
| ifship.xcor() >garbage.xcor()-15andship.xcor() <garbage.xcor()+15: | |
| ifship.ycor() >garbage.ycor()-15andship.ycor() <garbage.ycor()+15: | |
| #collide and erase! | |
| garbage.hideturtle() | |
| arr.remove(garbage) | |
| points+=1 | |
| defmove_trash(arr): | |
| foriteminarr: | |
| item.setpos(item.xcor()+random.randrange(-25,25), item.ycor()+random.randrange(-25,25)) | |
| defmove_asteroids(asteroid): | |
| asteroid.x=asteroid.x+asteroid.vel*asteroid.delta | |
| rad2=asteroid.r*asteroid.r | |
| asteroid.y=asteroid.vel*math.sqrt(rad2- (asteroid.x*asteroid.x)) | |
| ifasteroid.x==asteroid.rorasteroid.x==-asteroid.r: | |
| asteroid.vel=asteroid.vel*-1 | |
| asteroid.setpos(asteroid.x, asteroid.y) | |
| defup(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.sety(ship.ycor()+ship.step) | |
| gas-=1 | |
| defdown(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.sety(ship.ycor()-ship.step) | |
| gas-=1 | |
| defright(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.setx(ship.xcor()+ship.step) | |
| gas-=1 | |
| defleft(): | |
| globalgas | |
| ifgas>0: | |
| ship.accel=1 | |
| ship.setx(ship.xcor()-ship.step) | |
| gas-=1 | |
| defgameover(): | |
| score.clear() | |
| score.goto(100-width/4,0) | |
| score.write("GAME OVER", font=('Courier', 40, 'bold')) | |
| time.sleep(2) | |
| sys.exit() | |
| screen.listen() | |
| screen.onkeypress(up, 'Up') | |
| screen.onkeypress(down, 'Down') | |
| screen.onkeypress(right, 'Right') | |
| screen.onkeypress(left, 'Left') | |
| whileTrue: | |
| move_asteroids(asteroid0) | |
| move_asteroids(asteroid1) | |
| move_asteroids(asteroid2) | |
| #Colisiones con los asteroides | |
| ifship.xcor() >asteroid1.xcor()-5andship.xcor() <asteroid1.xcor()+5: | |
| ifship.ycor() >asteroid1.ycor()-5andship.ycor() <asteroid1.ycor()+5: | |
| #choco con asteroide1 | |
| gameover() | |
| ifship.xcor() >asteroid2.xcor()-5andship.xcor() <asteroid2.xcor()+5: | |
| ifship.ycor() >asteroid2.ycor()-5andship.ycor() <asteroid2.ycor()+5: | |
| #choco con asteroide2 | |
| gameover() | |
| ifship.xcor() >asteroid0.xcor()-5andship.xcor() <asteroid0.xcor()+5: | |
| ifship.ycor() >asteroid0.ycor()-5andship.ycor() <asteroid0.ycor()+5: | |
| #choco con asteroide0 | |
| gameover() | |
| #Colision con el planeta | |
| ifship.d<50 : #choco | |
| gameover() | |
| x=random.randint(0,width/2) | |
| y=random.randint(0,height/2) | |
| prob=random.randint(0,100) | |
| ifprob>85: #agregar una basura espacial | |
| create_garbage(x, y) | |
| elifprob>70: | |
| create_garbage(-x,y) | |
| elifprob>50: | |
| create_garbage(x,-y) | |
| elifprob>35: | |
| create_garbage(-x,-y) | |
| move_trash(garbage_arr) | |
| #solar panel | |
| if (ship.xcor()>sun.xcor()-100andship.xcor()<sun.xcor()+100) and (ship.ycor()>sun.ycor()-100andship.ycor()<sun.ycor()+100): | |
| ifgas<gas_limit: | |
| gas+=5 | |
| #collect garbage | |
| check_garbage(garbage_arr) | |
| dist_sun=math.ceil(math.sqrt((sun.xcor()-ship.xcor())**2+ (sun.ycor()-ship.ycor())**2)/ship.step)-5 | |
| gravity() | |
| score.clear() | |
| marcador="Debris Collection: {} \nEnergy: {} \nTrip to Sun: {}".format(points, gas, dist_sun) | |
| score.write(marcador, font=('Courier', 15, 'bold')) | |
| time.sleep(0.08) | |
| screen.update() |