Advertisement
here2share

# CodeSkulptor_online_visual_demo_0002.py

Jan 11th, 2015
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.61 KB | None | 0 0
  1. # CodeSkulptor_online_visual_demo_0002.py ### Not a Pys60 project, but does great for simulations
  2. # http://www.codeskulptor.org/#user39_p2T1m0LY9e_0.py
  3.  
  4. # implementation of Spaceship
  5.  
  6. ################################################################
  7. #
  8. # PLEASE CHANGE THE SOUND_FORMAT TO "mp3" IF SOUND DOESN'T WORK
  9. #
  10. ################################################################
  11. import simplegui
  12. import math
  13. import random
  14.  
  15. # globals for user interface
  16. SOUND_FORMAT = "ogg"
  17. WIDTH = 800
  18. HEIGHT = 600
  19. EXPLOSION_WIDTH = 128
  20. score = 0
  21. lives = 3
  22. time = 0.5
  23. rock_num = 0
  24. started = False
  25.  
  26. class ImageInfo:
  27.     def __init__(self, center, size, radius = 0, lifespan = None, animated = False):
  28.         self.center = center
  29.         self.size = size
  30.         self.radius = radius
  31.         if lifespan:
  32.             self.lifespan = lifespan
  33.         else:
  34.             self.lifespan = float('inf')
  35.         self.animated = animated
  36.  
  37.     def get_center(self):
  38.         return self.center
  39.  
  40.     def get_size(self):
  41.         return self.size
  42.  
  43.     def get_radius(self):
  44.         return self.radius
  45.  
  46.     def get_lifespan(self):
  47.         return self.lifespan
  48.  
  49.     def get_animated(self):
  50.         return self.animated
  51.  
  52.  
  53. # art assets created by Kim Lathrop, may be freely re-used in non-commercial projects, please credit Kim
  54.  
  55. # debris images - debris1_brown.png, debris2_brown.png, debris3_brown.png, debris4_brown.png
  56. #                 debris1_blue.png, debris2_blue.png, debris3_blue.png, debris4_blue.png, debris_blend.png
  57. debris_info = ImageInfo([320, 240], [640, 480])
  58. debris_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/debris2_blue.png")
  59.  
  60. # nebula images - nebula_brown.png, nebula_blue.png
  61. nebula_info = ImageInfo([400, 300], [800, 600])
  62. nebula_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/nebula_blue.png")
  63.  
  64. # splash image
  65. splash_info = ImageInfo([200, 150], [400, 300])
  66. splash_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/splash.png")
  67.  
  68. # ship image
  69. ship_info = ImageInfo([45, 45], [90, 90], 35)
  70. ship_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/double_ship.png")
  71.  
  72. # missile image - shot1.png, shot2.png, shot3.png
  73. missile_info = ImageInfo([5,5], [10, 10], 3, 50)
  74. missile_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/shot2.png")
  75.  
  76. # asteroid images - asteroid_blue.png, asteroid_brown.png, asteroid_blend.png
  77. asteroid_info = ImageInfo([45, 45], [90, 90], 40)
  78. asteroid_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/asteroid_blue.png")
  79.  
  80. # animated explosion - explosion_orange.png, explosion_blue.png, explosion_blue2.png, explosion_alpha.png
  81. explosion_info = ImageInfo([64, 64], [128, 128], 17, 24, True)
  82. explosion_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/explosion_alpha.png")
  83.  
  84. # sound assets purchased from sounddogs.com, please do not redistribute
  85. # .ogg versions of sounds are also available, just replace .mp3 by .ogg
  86. soundtrack = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/soundtrack."+SOUND_FORMAT)
  87. missile_sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/missile."+SOUND_FORMAT)
  88. missile_sound.set_volume(.5)
  89. ship_thrust_sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/thrust."+SOUND_FORMAT)
  90. explosion_sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/explosion."+SOUND_FORMAT)
  91.  
  92. # helper functions to handle transformations
  93. def angle_to_vector(ang):
  94.     return [math.cos(ang), math.sin(ang)]
  95.  
  96. def dist(p, q):
  97.     return math.sqrt((p[0] - q[0]) ** 2 + (p[1] - q[1]) ** 2)
  98.  
  99.  
  100. # Ship class
  101. class Ship:
  102.  
  103.     def __init__(self, pos, vel, angle, image, info):
  104.         self.pos = [pos[0], pos[1]]
  105.         self.vel = [vel[0], vel[1]]
  106.         self.thrust = False
  107.         self.angle = angle
  108.         self.angle_vel = 0
  109.         self.image = image
  110.         self.image_center = info.get_center()
  111.         self.image_size = info.get_size()
  112.         self.radius = info.get_radius()
  113.  
  114.     def draw(self,canvas):
  115.         if self.thrust:
  116.             canvas.draw_image(self.image, [self.image_center[0] + self.image_size[0], self.image_center[1]] , self.image_size,
  117.                               self.pos, self.image_size, self.angle)
  118.         else:
  119.             canvas.draw_image(self.image, self.image_center, self.image_size,
  120.                               self.pos, self.image_size, self.angle)
  121.         # canvas.draw_circle(self.pos, self.radius, 1, "White", "White")
  122.  
  123.     def update(self):
  124.         # update angle
  125.         self.angle += self.angle_vel
  126.  
  127.         # update position
  128.         self.pos[0] = (self.pos[0] + self.vel[0]) % WIDTH
  129.         self.pos[1] = (self.pos[1] + self.vel[1]) % HEIGHT
  130.  
  131.         # update velocity
  132.         if self.thrust:
  133.             acc = angle_to_vector(self.angle)
  134.             self.vel[0] += acc[0] * .1
  135.             self.vel[1] += acc[1] * .1
  136.  
  137.         self.vel[0] *= .99
  138.         self.vel[1] *= .99
  139.  
  140.     def set_thrust(self, on):
  141.         self.thrust = on
  142.         if on:
  143.             ship_thrust_sound.rewind()
  144.             ship_thrust_sound.play()
  145.         else:
  146.             ship_thrust_sound.pause()
  147.  
  148.     def increment_angle_vel(self):
  149.         self.angle_vel += .05
  150.  
  151.     def decrement_angle_vel(self):
  152.         self.angle_vel -= .05
  153.  
  154.     def shoot(self):
  155.         global a_missile
  156.         forward = angle_to_vector(self.angle)
  157.         missile_pos = [self.pos[0] + self.radius * forward[0], self.pos[1] + self.radius * forward[1]]
  158.         missile_vel = [self.vel[0] + 6 * forward[0], self.vel[1] + 6 * forward[1]]
  159.         missile_group.add(Sprite(missile_pos, missile_vel, self.angle, 0, missile_image, missile_info, missile_sound))
  160.         #a_missile = Sprite(missile_pos, missile_vel, self.angle, 0, missile_image, missile_info, missile_sound)
  161.  
  162.  
  163.  
  164. # Sprite class
  165. class Sprite:
  166.     def __init__(self, pos, vel, ang, ang_vel, image, info, sound = None):
  167.         self.pos = [pos[0],pos[1]]
  168.         self.vel = [vel[0],vel[1]]
  169.         self.angle = ang
  170.         self.angle_vel = ang_vel
  171.         self.image = image
  172.         self.image_center = info.get_center()
  173.         self.image_size = info.get_size()
  174.         self.radius = info.get_radius()
  175.         self.lifespan = info.get_lifespan()
  176.         self.animated = info.get_animated()
  177.         self.age = 0
  178.         if sound:
  179.             sound.rewind()
  180.             sound.play()
  181.  
  182.     def draw(self, canvas):
  183.         draw_center = self.image_center
  184.         if self.animated:
  185.             draw_center = [self.image_center[0] + self.age * EXPLOSION_WIDTH, self.image_center[1]]
  186.         canvas.draw_image(self.image, draw_center, self.image_size,
  187.                           self.pos, self.image_size, self.angle)
  188.  
  189.     def update(self):
  190.         # update angle
  191.         self.angle += self.angle_vel
  192.  
  193.         # update position
  194.         self.pos[0] = (self.pos[0] + self.vel[0]) % WIDTH
  195.         self.pos[1] = (self.pos[1] + self.vel[1]) % HEIGHT
  196.  
  197.         self.age += 1
  198.  
  199.     def collide(self, other_object):
  200.         if dist(self.pos, other_object.pos) < self.radius + other_object.radius:
  201.             return True
  202.         else:
  203.             return False
  204.  
  205. def group_collide(group, other_object):
  206.     group_copy = set(group)
  207.     collide_num = 0
  208.     for an_object in group_copy:
  209.         if an_object.collide(other_object):
  210.             group.remove(an_object)
  211.             collide_num += 1
  212.     return collide_num
  213.  
  214. def group_group_collide(group1, group2):
  215.     group_copy = set(group1)
  216.     collide_num = 0
  217.     for an_obj1 in group_copy:
  218.         if group_collide(group2, an_obj1):
  219.             explosion_group.add(Sprite(an_obj1.pos, [0, 0], 0, 0, explosion_image, explosion_info))
  220.             group1.remove(an_obj1)
  221.             collide_num += 1
  222.     return collide_num
  223.  
  224. def process_sprite_group(sprite_group, canvas):
  225.     for a_sprite in sprite_group:
  226.         a_sprite.draw(canvas)
  227.         a_sprite.update()
  228.         if a_sprite.age > a_sprite.lifespan:
  229.             sprite_group.remove(a_sprite)
  230.  
  231. # key handlers to control ship  
  232. def keydown(key):
  233.     #if started == False:
  234.         #return None
  235.     if key == simplegui.KEY_MAP['left']:
  236.         my_ship.decrement_angle_vel()
  237.     elif key == simplegui.KEY_MAP['right']:
  238.         my_ship.increment_angle_vel()
  239.     elif key == simplegui.KEY_MAP['up']:
  240.         my_ship.set_thrust(True)
  241.     elif key == simplegui.KEY_MAP['space']:
  242.         my_ship.shoot()
  243.  
  244. def keyup(key):
  245.     if key == simplegui.KEY_MAP['left']:
  246.         my_ship.increment_angle_vel()
  247.     elif key == simplegui.KEY_MAP['right']:
  248.         my_ship.decrement_angle_vel()
  249.     elif key == simplegui.KEY_MAP['up']:
  250.         my_ship.set_thrust(False)
  251.  
  252. # mouseclick handlers that reset UI and conditions whether splash image is drawn
  253. def click(pos):
  254.     global started, lives, score
  255.     center = [WIDTH / 2, HEIGHT / 2]
  256.     size = splash_info.get_size()
  257.     inwidth = (center[0] - size[0] / 2) < pos[0] < (center[0] + size[0] / 2)
  258.     inheight = (center[1] - size[1] / 2) < pos[1] < (center[1] + size[1] / 2)
  259.     lives = 3
  260.     score = 0
  261.     if (not started) and inwidth and inheight:
  262.         started = True
  263.  
  264. def draw(canvas):
  265.     global time, started, lives, score
  266.  
  267.     # animiate background
  268.     time += 1
  269.     center = debris_info.get_center()
  270.     size = debris_info.get_size()
  271.     wtime = (time / 8) % center[0]
  272.     canvas.draw_image(nebula_image, nebula_info.get_center(), nebula_info.get_size(), [WIDTH / 2, HEIGHT / 2], [WIDTH, HEIGHT])
  273.     canvas.draw_image(debris_image, [center[0] - wtime, center[1]], [size[0] - 2 * wtime, size[1]],
  274.                                 [WIDTH / 2 + 1.25 * wtime, HEIGHT / 2], [WIDTH - 2.5 * wtime, HEIGHT])
  275.     canvas.draw_image(debris_image, [size[0] - wtime, center[1]], [2 * wtime, size[1]],
  276.                                 [1.25 * wtime, HEIGHT / 2], [2.5 * wtime, HEIGHT])
  277.  
  278.     # draw ship and sprites
  279.     my_ship.draw(canvas)
  280.     my_ship.update()
  281.     process_sprite_group(rock_group, canvas)
  282.     process_sprite_group(missile_group, canvas)
  283.     process_sprite_group(explosion_group, canvas)
  284.     if group_collide(rock_group, my_ship):
  285.         lives -= 1
  286.         explosion_group.add(Sprite(my_ship.pos, [0, 0], 0, 0, explosion_image, explosion_info))
  287.         if lives <= 0:
  288.             started = False
  289.             rock_group.intersection_update(set())
  290.             #lives = 3
  291.             #score = 0
  292.     # draw UI
  293.     canvas.draw_text("Lives", [50, 50], 22, "White")
  294.     canvas.draw_text("Score", [680, 50], 22, "White")
  295.     canvas.draw_text(str(lives), [50, 80], 22, "White")
  296.     canvas.draw_text(str(score), [680, 80], 22, "White")
  297.  
  298.     missile_hit_number = group_group_collide(rock_group, missile_group)
  299.     score += missile_hit_number * 10
  300.     # draw splash screen if not started
  301.     if not started:
  302.         canvas.draw_image(splash_image, splash_info.get_center(),
  303.                           splash_info.get_size(), [WIDTH / 2, HEIGHT / 2],
  304.                           splash_info.get_size())
  305.  
  306. # timer handler that spawns a rock    
  307. def rock_spawner():
  308.     #global rock_num
  309.     if len(rock_group) >= 12 or started == False:
  310.         return None
  311.     rock_pos = [random.randrange(0, WIDTH), random.randrange(0, HEIGHT)]
  312.     rock_vel = [random.random() * .6 - .3, random.random() * .6 - .3]
  313.     rock_avel = random.random() * .2 - .1
  314.     rock_group.add(Sprite(rock_pos, rock_vel, 0, rock_avel, asteroid_image, asteroid_info))
  315.     soundtrack.rewind()
  316.     #soundtrack.play()
  317.     #rock_num += 1
  318.     #a_rock = Sprite(rock_pos, rock_vel, 0, rock_avel, asteroid_image, asteroid_info)
  319.  
  320. # initialize stuff
  321. frame = simplegui.create_frame("Asteroids", WIDTH, HEIGHT)
  322.  
  323. # initialize ship and two sprites
  324. my_ship = Ship([WIDTH / 2, HEIGHT / 2], [0, 0], 0, ship_image, ship_info)
  325. rock_group = set()
  326. #a_rock = Sprite([WIDTH / 3, HEIGHT / 3], [1, 1], 0, .1, asteroid_image, asteroid_info)
  327. missile_group = set()
  328. explosion_group = set()
  329. #a_missile = Sprite([2 * WIDTH / 3, 2 * HEIGHT / 3], [-1,1], 0, 0, missile_image, missile_info, missile_sound)
  330.  
  331.  
  332. # register handlers
  333. frame.set_keyup_handler(keyup)
  334. frame.set_keydown_handler(keydown)
  335. frame.set_mouseclick_handler(click)
  336. frame.set_draw_handler(draw)
  337.  
  338. timer = simplegui.create_timer(1000.0, rock_spawner)
  339.  
  340. soundtrack.play()
  341. # get things rolling
  342. timer.start()
  343. frame.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement