Coursera_An Introduction to Interactive Programming in Python_Mini-project # 7 Spaceship
来源:互联网 发布:c语言编写一个计算器 编辑:程序博客网 时间:2024/06/08 06:16
http://www.codeskulptor.org/#user38_mT9p7Gz3Un_9.py
# program for Spaceshipimport simpleguiimport mathimport random# globals for user interfaceWIDTH = 800HEIGHT = 600score = 0lives = 3time = 0.5friction_para = 0.05vol = 7class ImageInfo: def __init__(self, center, size, radius = 0, lifespan = None, animated = False): self.center = center self.size = size self.radius = radius if lifespan: self.lifespan = lifespan else: self.lifespan = float('inf') self.animated = animated def get_center(self): return self.center def get_size(self): return self.size def get_radius(self): return self.radius def get_lifespan(self): return self.lifespan def get_animated(self): return self.animated # debris images - debris1_brown.png, debris2_brown.png, debris3_brown.png, debris4_brown.png# debris1_blue.png, debris2_blue.png, debris3_blue.png, debris4_blue.png, debris_blend.pngdebris_info = ImageInfo([320, 240], [640, 480])debris_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/debris2_blue.png")# nebula images - nebula_brown.png, nebula_blue.pngnebula_info = ImageInfo([400, 300], [800, 600])nebula_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/nebula_blue.f2014.png")# splash imagesplash_info = ImageInfo([200, 150], [400, 300])splash_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/splash.png")# ship imageship_info = ImageInfo([45, 45], [90, 90], 35)ship_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/double_ship.png")# missile image - shot1.png, shot2.png, shot3.pngmissile_info = ImageInfo([5,5], [10, 10], 3, 50)missile_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/shot2.png")# asteroid images - asteroid_blue.png, asteroid_brown.png, asteroid_blend.pngasteroid_info = ImageInfo([45, 45], [90, 90], 40)asteroid_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/asteroid_blue.png")# animated explosion - explosion_orange.png, explosion_blue.png, explosion_blue2.png, explosion_alpha.pngexplosion_info = ImageInfo([64, 64], [128, 128], 17, 24, True)explosion_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/explosion_alpha.png")# sound assets purchased from sounddogs.com, please do not redistributesoundtrack = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/soundtrack.mp3")missile_sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/missile.mp3")missile_sound.set_volume(.5)ship__sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/thrust.mp3")explosion_sound = simplegui.load_sound("http://commondatastorage.googleapis.com/codeskulptor-assets/sounddogs/explosion.mp3")# helper functions to handle transformationsdef angle_to_vector(ang): return [math.cos(ang), math.sin(ang)]def dist(p,q): return math.sqrt((p[0] - q[0]) ** 2+(p[1] - q[1]) ** 2)# Ship classclass Ship: def __init__(self, pos, vel, angle, image, info): self.pos = [pos[0],pos[1]] self.vel = [vel[0],vel[1]] self.thrust = False self.angle = angle self.angle_vel = 0 self.image = image self.image_center = info.get_center() self.image_size = info.get_size() self.radius = info.get_radius() def draw(self,canvas): #canvas.draw_circle(self.pos, self.radius, 1, "White", "White") if self.thrust: canvas.draw_image(self.image, [self.image_center[0]+self.image_size[0],self.image_center[1]], self.image_size, self.pos, self.image_size, self.angle) else: canvas.draw_image(self.image, self.image_center, self.image_size, self.pos, self.image_size, self.angle) def update(self): # angle update self.angle += self.angle_vel # position update self.pos[0] += self.vel[0] self.pos[1] += self.vel[1] self.pos[0] %= WIDTH self.pos[1] %= HEIGHT # friction update self.vel[0] *=(1-friction_para) self.vel[1] *=(1-friction_para) # velocity update if self.thrust: self.vel[0] += angle_to_vector(self.angle)[0] self.vel[1] += angle_to_vector(self.angle)[1] ship__sound.play() ship__sound.set_volume(vol / 10.0) else: ship__sound.rewind() def shoot(self): global a_missile pos_missile = [0,0] vel_missile = [0,0] pos_missile[0] = self.pos[0]+ self.radius * angle_to_vector(self.angle)[0] pos_missile[1] = self.pos[1]+ self.radius * angle_to_vector(self.angle)[1] vel_missile[0] = self.vel[0] + angle_to_vector(self.angle)[0] * 2 vel_missile[1] = self.vel[1] + angle_to_vector(self.angle)[1] * 2 a_missile = Sprite(pos_missile, vel_missile, self.angle, 0, missile_image, missile_info, missile_sound) # Sprite classclass Sprite: def __init__(self, pos, vel, ang, ang_vel, image, info, sound = None): self.pos = [pos[0],pos[1]] self.vel = [vel[0],vel[1]] self.angle = ang self.angle_vel = ang_vel self.image = image self.image_center = info.get_center() self.image_size = info.get_size() self.radius = info.get_radius() self.lifespan = info.get_lifespan() self.animated = info.get_animated() self.age = 0 if sound: sound.rewind() sound.play() def draw(self, canvas): canvas.draw_image(self.image, self.image_center, self.image_size, self.pos, self.image_size, self.angle) def update(self): self.angle += self.angle_vel self.pos[0] += self.vel[0] self.pos[1] += self.vel[1] self.pos[0] %= WIDTH self.pos[1] %= HEIGHT def draw(canvas): global time # animiate background time += 1 wtime = (time / 4) % WIDTH center = debris_info.get_center() size = debris_info.get_size() canvas.draw_image(nebula_image, nebula_info.get_center(), nebula_info.get_size(), [WIDTH / 2, HEIGHT / 2], [WIDTH, HEIGHT]) canvas.draw_image(debris_image, center, size, (wtime - WIDTH / 2, HEIGHT / 2), (WIDTH, HEIGHT)) canvas.draw_image(debris_image, center, size, (wtime + WIDTH / 2, HEIGHT / 2), (WIDTH, HEIGHT)) # draw ship and sprites my_ship.draw(canvas) a_rock.draw(canvas) a_missile.draw(canvas) # update ship and sprites my_ship.update() a_rock.update() a_missile.update() # draw Lives and score canvas.draw_text('Lives', (20, 30), 24, 'White') canvas.draw_text('Score', (700, 30), 24, 'White') canvas.draw_text(str(lives), (20, 50), 24, 'White') canvas.draw_text(str(score), (700, 50), 24, 'White')# timer handler that spawns a rock def rock_spawner(): global a_rock pos = [random.random() * WIDTH, random.random() * HEIGHT] vel = [random.random(), random.random()] ang_vel = random.random() * 0.1 a_rock = Sprite(pos, vel, 0, ang_vel, asteroid_image, asteroid_info)def keydown(key): if key==simplegui.KEY_MAP['space']: my_ship.shoot() elif key==simplegui.KEY_MAP['left']: my_ship.angle_vel -= 0.1 elif key==simplegui.KEY_MAP['right']: my_ship.angle_vel += 0.1 elif key==simplegui.KEY_MAP["up"]: my_ship.thrust = Truedef keyup(key): if key==simplegui.KEY_MAP["space"]: a=1 elif key==simplegui.KEY_MAP["left"]: my_ship.angle_vel = 0 elif key==simplegui.KEY_MAP["right"]: my_ship.angle_vel = 0 elif key==simplegui.KEY_MAP["up"]: my_ship.thrust = False # initialize frameframe = simplegui.create_frame("Asteroids", WIDTH, HEIGHT)# initialize ship and two spritesmy_ship = Ship([WIDTH / 2, HEIGHT / 2], [0, 0], 2, ship_image, ship_info)a_rock = Sprite([WIDTH / 3, HEIGHT / 3], [1, 1], 0, .1, asteroid_image, asteroid_info)a_missile = Sprite([2 * WIDTH / 3, 2 * HEIGHT / 3], [-1,1], 0, 0, missile_image, missile_info, missile_sound)# register handlersframe.set_draw_handler(draw)frame.set_keydown_handler(keydown)frame.set_keyup_handler(keyup)timer = simplegui.create_timer(1000.0, rock_spawner)# get things rollingtimer.start()frame.start()
0 0
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 7 Spaceship
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 3 Stopwatch
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 4 Pong
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 5 Memory
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 6 Blackjack
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 8 RiceRock
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 2 Guess the number
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 1 Rock-paper-scissors
- Coursera_An Introduction to Interactive Programming in Python_Music Player
- An Introduction to Interactive Programming in Python(week 7)
- An Introduction to Interactive Programming in Python
- Mini-project # 4 - "Pong"___An Introduction to Interactive Programming in Python"RICE"
- Mini-project # 1 - Rock-paper-scissors-___An Introduction to Interactive Programming in Python"RICE"
- Interactive Python:Mini-project # 7 - Spaceship
- An Introduction to Interactive Programming in Python - Week zero
- An Introduction to Interactive Programming in Python - Week one
- An Introduction to Interactive Programming in Python - Week two
- An Introduction to Interactive Programming in Python - Week three
- 超牛叉技术支撑的成人网站
- 动态加载TreeView时让TreeView节点前显示加号
- mount命令使用详解(Linux)
- 取余数和商
- 如何将jpeg转换成pdf步骤
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 7 Spaceship
- 好事客导航_新一代安全、实用、便捷的网址导航
- java反射
- 深入理解HTTP Session
- IP查询接口
- C++ class Date
- 《c程序设计语言》读书笔记-5.6-指针重写getline等函数
- [UI]Android启动引导界面ViewPager
- apache jmeter压力测试初体会-Mongodb Elasticsearch Test