An Introduction to Interactive Programming in Python 第七周作业

最后一个作业了 还记着当时大概做了一周才做完。

效果图:
图片一直上传失败,下次补图。

在这里插入图片描述

图来了。

import simplegui
import random
import math
# globals for user interface
WIDTH = 800
HEIGHT = 600
score = 0
lives = 3
time = 0.5

class 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


    # art assets created by Kim Lathrop, may be freely re-used in non-commercial projects, please credit Kim

    # 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.png
debris_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.png
nebula_info = ImageInfo([400, 300], [800, 600])   #星云
nebula_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/nebula_blue.f2014.png")

    # splash image
splash_info = ImageInfo([200, 150], [400, 300])
splash_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/splash.png")

    # ship image
ship_info = ImageInfo([45, 45], [90, 90], 35)
    #ship_info = ImageInfo([135, 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.png
missile_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.png
asteroid_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.png
explosion_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 redistribute
soundtrack = 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_thrust_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 transformations
def 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 class
class 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):
        ship_center = ship_info.get_center()
        ship_size = ship_info.get_size()
        if self.thrust:
            ship_thrust_sound.play()
            canvas.draw_image(ship_image, [ship_center[0] + ship_size[0], ship_center[1]], ship_size, self.pos, self.image_size, self.angle)
        else:
            ship_thrust_sound.rewind()
            canvas.draw_image(ship_image, ship_info.get_center(), ship_info.get_size(), self.pos, self.image_size, self.angle)

    def update(self):
            # Angle update
        self.angle  += self.angle_vel

            # Postition update
        self.pos[0] = (self.pos[0] + self.vel[0]) % WIDTH
        self.pos[1] = (self.pos[1] + self.vel[1]) % HEIGHT

            # Friction update
        friction = 0.01
        self.vel = map(lambda x: x * (1 - friction), self.vel)

            # Thrust update
        if self.thrust:
            thrust = 0.1
            forward = angle_to_vector(self.angle)
            self.vel[0] += forward[0] * thrust
            self.vel[1] += forward[1] * thrust

    def set_angle_vel(self, value):  #设定角速度
        self.angle_vel = value

    def set_thrust(self, value):  #设定thrust的值
        self.thrust = value

    def shoot(self):
        global missiles
            # cannan position
        forward = angle_to_vector(self.angle)
        missile_pos = map(lambda x, y: x + self.radius * y, self.pos, forward)
        missile_vel = map(lambda x, y: x + self.radius * .1 * y ,self.vel,forward)

        a_missile = Sprite(missile_pos, missile_vel, 0, 0, missile_image, missile_info, missile_sound)
        missiles.append(a_missile)


    # Sprite class
class 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.pos[0] = (self.pos[0] + self.vel[0]) % WIDTH
        self.pos[1] = (self.pos[1] + self.vel[1]) % HEIGHT
        self.angle  += self.angle_vel

            # update lifespan
        self.lifespan -= 1
        


def draw(canvas):
    global time, missiles

        # 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)
    
    for a_rock in rocks:
        a_rock.draw(canvas)
    for a_missile in missiles:
        a_missile.draw(canvas)

        # update ship and sprites
    my_ship.update()  
    
    for a_rock in rocks:
        a_rock.update()
    for a_missile in missiles:
        a_missile.update()
        if a_missile.lifespan == 0:
            missiles.remove(a_missile)

        # draw score and life
    ship_life_size = map(lambda x: x / 3, ship_info.get_size())
    for dummy_idx in range(lives):
        ship_life_pos = [WIDTH / 20 + dummy_idx * ship_life_size[0], HEIGHT / 20]
        canvas.draw_image(ship_image, ship_info.get_center(), ship_info.get_size(),
                              ship_life_pos, ship_life_size, math.pi * 1.5)
        #canvas.draw_text("Lives",    [WIDTH / 20, HEIGHT / 20], HEIGHT / 25, "White")
        #canvas.draw_text(str(lives), [WIDTH / 20, HEIGHT / 10], HEIGHT / 25, "White")
    canvas.draw_text("Score",    [WIDTH * 0.85, HEIGHT / 20], HEIGHT / 25, "White")
    canvas.draw_text(str(score), [WIDTH * 0.85, HEIGHT / 10], HEIGHT / 25, "White")


    # timer handler that spawns a rock    
def rock_spawner():
    global a_rock
        # rock position
    a_rock_pos = [WIDTH * random.random(), HEIGHT * random.random()]

        # rock velocity
    magnitude = random.random() * .3 + .2
    angle = random.random() * (math.pi * 2)
    a_rock_vel = [magnitude * math.cos(angle), magnitude * math.sin(angle)]

        # rock angle velocity
    a_rock_angle_vel = random.randrange(50, 100) * random.choice([-1, 1]) * .0001
    a_rock = Sprite(a_rock_pos, a_rock_vel, 0, a_rock_angle_vel, asteroid_image, asteroid_info)

    if len(rocks) < 10:
        rocks.append(a_rock)

    # initialize frame
frame = simplegui.create_frame("Asteroids", WIDTH, HEIGHT)

    # initialize ship and two sprites
my_ship = Ship([WIDTH / 2, HEIGHT / 2], [0, 0], 0, ship_image, ship_info)
    #a_rock = Sprite([WIDTH / 3, HEIGHT / 3], [1, 1], 0, 0, asteroid_image, asteroid_info)
rocks = []
missiles = []

    # handlers definition
downs = {"up":    [my_ship.set_thrust, True],
         "space": [my_ship.shoot],
         "left":  [my_ship.set_angle_vel, -0.02],
         "right": [my_ship.set_angle_vel, 0.02]}

def keydown(key):
    """
    keyboard control functions:   
    """
    for i in downs:
        if key == simplegui.KEY_MAP[i]:
            if len(downs[i]) > 1:
                downs[i][0](downs[i][1])
                #print "kong",downs[i][1]
            else:
                downs[i][0]()
                #print "de",downs[i][0]
                
ups = {"up":    [my_ship.set_thrust, False],
       "left":  [my_ship.set_angle_vel, 0],
       "right": [my_ship.set_angle_vel, 0]}

def keyup(key):
    for i in ups:
        if key == simplegui.KEY_MAP[i]:
            ups[i][0](ups[i][1])


    # register handlers
frame.set_draw_handler(draw)

timer = simplegui.create_timer(1000.0, rock_spawner)

frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)

    # get things rolling
timer.start()
frame.start()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值