An Introduction to Interactive Programming in Python(week 7)

本文为coursera中An Introduction to Interactive Programming in Python第七周的project代码,由于本周只实现了最终project的一部分功能,剩下部分第八周再进行补充。

# -*- coding: cp936 -*-
# Mini-project #7 - Blackjack

import SimpleGUICS2Pygame.simpleguics2pygame as simplegui
import random
import math

# globals for user interface
FRAME_WIDTH = 800
FRAME_HEIGHT = 600
score = 0
lives = 3
time = 0

# the background image
back_image = simplegui.load_image("C:\\Users\\junyang\\Documents\\python\\Asteroids\\SpaceBackground.jpg")
BACK_WIDTH = back_image.get_width()
BACK_HEIGHT = back_image.get_height()
##print BACK_WIDTH,BACK_HEIGHT
# the spaceship
spaceship_image = simplegui.load_image("C:\\Users\\junyang\\Documents\\python\\Asteroids\\cohete_off.png")
SHIP_WIDTH = spaceship_image.get_width()
SHIP_HEIGHT = spaceship_image.get_height()

spaceship_on_image = simplegui.load_image("C:\\Users\\junyang\\Documents\\python\\Asteroids\\cohete_on_wf.png")
SHIP_ON_WIDTH = spaceship_on_image.get_width()
SHIP_ON_HEIGHT = spaceship_on_image.get_height()
#print SHIP_WIDTH,SHIP_HEIGHT
# the planet
planet_image = simplegui.load_image("C:\\Users\\junyang\\Documents\\python\\Asteroids\\Planet.png")
PLANET_WIDTH = planet_image.get_width()
PLANET_HEIGHT = planet_image.get_height()
# the bullet
bullet_image = simplegui.load_image("C:\\Users\\junyang\\Documents\\python\\Asteroids\\bullet.png")
BULLET_WIDTH = bullet_image.get_width()
BULLET_HEIGHT = bullet_image.get_height()
#print PLANET_WIDTH,PLANET_HEIGHT
# the sound of ship_on
spaceship_on_sound = simplegui.load_sound("C:\\Users\\junyang\\Documents\\python\\Asteroids\\ship.wav")
spaceship_on_sound.set_volume(0.1)
# the sound of bullet
##bullet_sound = simplegui.load_sound("C:\\Users\\junyang\\Documents\\python\\Asteroids\\bullet.wav")
# the back music
back_music = simplegui.load_sound("C:\\Users\\junyang\\Documents\\python\\Asteroids\\back_music.ogg")
back_music.set_volume(0.1)

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

# ship class
class Ship:
    def __init__(self,pos,vel,angle,image,info,dissize = [SHIP_WIDTH * 0.5,\
                                                        SHIP_HEIGHT * 0.5]):
        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.radius = info.get_radius()
        self.image_center = info.get_center()
        self.image_size = info.get_size()
        self.dissize = [dissize[0],dissize[1]]
        
    def set_thrust(self,thrust):
        self.thrust = thrust

    def draw(self,canvas):
        canvas.draw_image(self.image,self.image_center,self.image_size,\
                          self.pos,self.dissize,self.angle)

    def update(self):     
        if self.thrust == False:
            self.vel[0] = self.vel[0] * 0.95
            self.vel[1] = self.vel[1] * 0.95
        if self.pos[0] <= -0.5 * self.radius:
            self.pos[0] = FRAME_WIDTH + self.radius * 0.2
        elif self.pos[0] >= 0.5 * self.radius + FRAME_WIDTH:
            self.pos[0] = -0.2 * self.radius
        if self.pos[1] <= -0.5 * self.radius:
            self.pos[1] = FRAME_HEIGHT + self.radius * 0.2
        elif self.pos[1] >= 0.5 * self.radius + FRAME_HEIGHT:
            self.pos[1] = -0.2 * self.radius
        self.pos[0] += self.vel[0]
        self.pos[1] += self.vel[1]
        self.angle += self.angle_vel
            

# Sprite class
class Sprite:
    def __init__(self,pos,vel,ang,ang_vel,image,info,dissize = [PLANET_WIDTH\
                                * 0.1,PLANET_HEIGHT * 0.1],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
        self.dissize = [dissize[0],dissize[1]]
        if sound:
            sound.rewind()
            sound.play()

    def draw(self,canvas):
        canvas.draw_image(self.image,self.image_center,\
                          self.image_size,self.pos,self.dissize,self.angle)

    def update(self):
        if self.pos[0] <= -0.5 * self.radius:
            self.pos[0] = FRAME_WIDTH + self.radius * 0.2
        elif self.pos[0] >= 0.5 * self.radius + FRAME_WIDTH:
            self.pos[0] = -0.2 * self.radius
        if self.pos[1] <= -0.5 * self.radius:
            self.pos[1] = FRAME_HEIGHT + self.radius * 0.2
        elif self.pos[1] >= 0.5 * self.radius + FRAME_HEIGHT:
            self.pos[1] = -0.2 * self.radius
        self.angle += self.angle_vel
        self.pos[0] += self.vel[0]
        self.pos[1] += self.vel[1]

def draw(canvas):
    # draw ship and sprites
    canvas.draw_image(back_image,[BACK_WIDTH / 2,BACK_HEIGHT / 2],[BACK_WIDTH,\
                        BACK_HEIGHT],[FRAME_WIDTH / 2,FRAME_HEIGHT / 2],\
                      [FRAME_WIDTH,FRAME_HEIGHT])
    
    if a_ship.thrust == True:
        a_ship.image = spaceship_on_image
        a_ship.info = ship_on_info
        spaceship_on_sound.play()
    else:
        a_ship.image = spaceship_image
        a_ship.info = ship_info
        spaceship_on_sound.rewind()
    a_ship.draw(canvas)
    a_planet.draw(canvas)
    a_planet.update()
    a_ship.update()
    
def keydown(key):
    if key == simplegui.KEY_MAP['left']:
        a_ship.angle_vel = -0.1
        a_ship.thrust = False
    elif key == simplegui.KEY_MAP['right']:
        a_ship.angle_vel = 0.1
        a_ship.thrust = False
    elif key == simplegui.KEY_MAP['up']:
        a_ship.vel[0] = 2 * math.sin(a_ship.angle)
        a_ship.vel[1] = -2 * math.cos(a_ship.angle)
        a_ship.thrust = True

def keyup(key):
    if key == simplegui.KEY_MAP['left'] or key == simplegui.KEY_MAP['right']:
        a_ship.angle_vel = 0
    elif key == simplegui.KEY_MAP['up']:
        a_ship.thrust = False

def timer_handler():
    a_planet.pos[0] = random.random() * FRAME_WIDTH * 0.9 + FRAME_WIDTH * 0.05
    a_planet.pos[1] = random.random() * FRAME_WIDTH * 0.9 + FRAME_WIDTH * 0.05
    a_planet.vel[0] = random.random() * 0.5 - 0.25
    a_planet.vel[1] = random.random() * 0.5 - 0.25
    a_planet.angle_vel = random.random() * 0.01
    
# initialize frame
frame = simplegui.create_frame("Sprite demo",FRAME_WIDTH,FRAME_HEIGHT)
timer = simplegui.create_timer(5000,timer_handler)

# initialize ship and two sprites
ship_info = ImageInfo([SHIP_WIDTH / 2,SHIP_HEIGHT / 2],[SHIP_WIDTH,SHIP_HEIGHT],SHIP_HEIGHT / 2)
a_ship = Ship([FRAME_WIDTH / 2, FRAME_HEIGHT / 2],[0,0],0,spaceship_image,ship_info)
ship_on_info = ImageInfo([SHIP_ON_WIDTH / 2,SHIP_ON_HEIGHT / 2],[SHIP_ON_WIDTH,\
                       SHIP_ON_HEIGHT],SHIP_ON_HEIGHT / 2)
planet_info = ImageInfo([PLANET_WIDTH / 2,PLANET_HEIGHT / 2],[PLANET_WIDTH,PLANET_HEIGHT],PLANET_HEIGHT * 0.05)
a_planet = Sprite([400,300],[0.3,0.4],0,0.1,planet_image,planet_info)
back_music.play()

# register handlers
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)

# get things rolling
timer.start()
frame.start()
运行后如下图(实在用不了google的storage,中国学生你懂的,所以自己找了素材图,真是没有美感。。。。。)

An Introduction to Interactive Programming in Python(week 7)_第1张图片

你可能感兴趣的:(Python,python,windows,编程)