import time import turtle import math import random time_before = 0 window_width = 600 window_height = 600 window_left = -(window_width / 2) window_right = window_width / 2 window_bottom = -(window_height / 2) window_top = window_height / 2 # 7 numbers: x,y, vx,vy, angle, turn_rate, size ship = [0] * 7 # INDEX NAMES: # These variables give names to the indexes in the above list X = 0 Y = X + 1 VX = Y + 1 VY = VX + 1 ANGLE = VY + 1 RATE = ANGLE + 1 SIZE = RATE + 1 # This list will hold all the rock motion records rocks = [] # And this list will hold the bullet motion records bullets = [] # Notice how the index names make the code more readable def move_object(motion, T): motion[X] += motion[VX] * T motion[Y] += motion[VY] * T motion[ANGLE] += motion[RATE] * T def wrap_around(motion): if motion[X] > window_right: motion[X] = window_left elif motion[X] < window_left: motion[X] = window_right elif motion[Y] > window_top: motion[Y] = window_bottom elif motion[Y] < window_bottom: motion[Y] = window_top def move_rocks(T): for rock in rocks: wrap_around(rock) move_object(rock, T) def draw_rock(motion_record): x = motion_record[X] y = motion_record[Y] angle = motion_record[ANGLE] size = motion_record[SIZE] turtle.penup() turtle.goto(x, y) turtle.setheading(angle) turtle.forward(size) turtle.pendown() turtle.left(60) for side in range(6): turtle.left(60) turtle.forward(size) def draw_rocks(): for rock in rocks: draw_rock(rock) def draw_bullet(bullet): turtle.penup() turtle.goto(bullet[X], bullet[Y]) turtle.dot(bullet[SIZE]) def draw_objects(): turtle.tracer(False) turtle.hideturtle() turtle.clear() draw_rocks() draw_bullets() turtle.tracer(True) def collided(motion1, motion2): x1 = motion1[X] y1 = motion1[Y] radius1 = motion1[SIZE] x2 = motion2[X] y2 = motion2[Y] radius2 = motion2[SIZE] dx = x1 - x2 dy = y1 - y2 distance = math.sqrt(dx * dx + dy * dy) if distance < (radius1 + radius2): return True else: return False def move_objects(T): move_rocks(T) move_bullets(T) remove_offscreen_bullets() def update_scene(): global time_before time_now = time.time() time_elapsed = time_now - time_before move_objects(time_elapsed) check_rock_bullet_hit() draw_objects() time_before = time_now turtle.ontimer(update_scene, 100) ####################################################################### # THE FUNCTIONS YOU MUST MODIFY ARE BELOW THIS LINE # # | | | | | | | | | | | | | | | | | | # v v v v v v v v v v v v v v v v v v def random_motion(): x = random.randrange(window_left, window_right) y = random.randrange(window_bottom, window_top) vx = random.random() * 200 - 100 vy = random.random() * 200 - 100 rotation_rate = random.random() * 200 - 100 return [x, y, vx, vy, 0, rotation_rate, 20] def make_rocks(): global rocks rocks.append(random_motion()) def move_bullets(T): return def remove_offscreen_bullets(): return def fire_bullet(): global bullets angle = ship[ANGLE] gun_offset = ship[SIZE] speed = 40 # 40 pixels per second bullet = [0] * 7 # (ux uy) is a unit vector pointing in the ship's direction ux = math.cos(math.radians(angle)) uy = math.sin(math.radians(angle)) bullet[X] = ship[X] + gun_offset * ux bullet[Y] = ship[Y] + gun_offset * uy bullet[VX] = ux * speed bullet[VY] = uy * speed bullet[SIZE] = 5 bullets.append(bullet) def draw_bullets(): return def check_rock_bullet_hit(): return def turn_ship_left(): return def turn_ship_right(): return def main(): global time_before turtle.setup(window_width, window_height, 0,0) turtle.Screen() turtle.onkey(fire_bullet, ' ') turtle.ontimer(update_scene, 100) time_before = time.time() make_rocks() turtle.listen() turtle.mainloop() # ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ # | | | | | | | | | | | | | | | | | | # # THE FUNCTIONS YOU MUST MODIFY ARE ABOVE THIS LINE ####################################################################### if __name__ == '__main__': main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question