Python – Spaceship 讲解

0

1. 引用相关的module

import simplegui
import math
import random

2. 全局参量

WIDTH = 800 # 画布的宽
HEIGHT = 600 # 画布的高
score = 0 # 初始分数0
lives = 3 # 3条命
time = 0.5 # 时间为0.5
FRICTION = 0.95 #摩擦系数


3.元素类

class ImageInfo: # 设置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

4. 飞船类

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):
        if self.thrust:  
            self.image_center[0]=135  
        else:  
            self.image_center[0]=45  
        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.vel[0]
        self.pos[1] += self.vel[1]
        #飞船的现有坐标加上速度,使其坐标移动
        self.angle += self.angle_vel
        forward = angle_to_vector(self.angle)
        if self.thrust:    
            self.vel[0] += forward[0]
            self.vel[1] += forward[1]
            #如果是加速的,那么如上所示
        self.vel[0] *= FRICTION
        self.vel[1] *= FRICTION
        #如果没有加速,那么加上摩擦系数
        self.pos[0] %= WIDTH
        self.pos[1] %= HEIGHT
 
    def set_angle_vel(self, angle_vel):
        self.angle_vel += angle_vel
    
    def set_thrust(self, thrust):
        self.thrust = thrust
        if self.thrust:
            ship_thrust_sound.play()
        else:
            ship_thrust_sound.rewind()
 
    def shoot(self):  
        global a_missile  
        foward=angle_to_vector(self.angle)  
        tmppos=[self.pos[0]+45*foward[0],self.pos[1]+45*foward[1]]  
        tmpvel=[self.vel[0]+foward[0]*3,self.vel[1]+foward[1]*3]  
        a_missile = Sprite(tmppos, tmpvel, 0, 0, missile_image, missile_info, missile_sound)  
        missile_sound.play()

5. Sprite类

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

6. 辅助函数

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)
 
def draw(canvas):
    global time
    # animiate background
    time += 1
    wtime = (time / 4) % WIDTH
    #定义两个星云,以匀速向右移动,一个是从全屏开始,另一个是从最右边开始
    #例如速度是1-10,当到10后,第一张图已经画完了,刚出画布的右边,这时回到1重画;
    #而此时第二张图也画完了,刚好铺满画布
    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))
    canvas.draw_text('lives:'+str(lives), (50, 50), 24, "Red")  
    canvas.draw_text('score:'+str(score), (50, 100), 24, "Red")  
    
    # 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()
               
def rock_spawner():
    global a_rock  
    a_rock = Sprite([random.random()*WIDTH, random.random()*HEIGHT], [random.random(), random.random()], random.random()*2*math.pi, random.random()*0.1, asteroid_image, asteroid_info)  
 
def keydown(key):  
    if 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=True  
    elif key==simplegui.KEY_MAP['space']:  
        my_ship.shoot()  
         
def keyup(key):  
    if 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

7. 主程序

# 创建一个框,名为"Asteroids",设置宽高,全局变量已设
frame = simplegui.create_frame("Asteroids", WIDTH, HEIGHT)
 
# 初始化元素
# 元素1:尘埃的图像和初始数值
debris_info = ImageInfo([320, 240], [640, 480])
debris_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/debris2_blue.png")
 
# 元素2:星云的图像和初始数值
nebula_info = ImageInfo([400, 300], [800, 600])
nebula_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/nebula_blue.f2014.png")
 
# 元素3:开始图像和初始数值
splash_info = ImageInfo([200, 150], [400, 300])
splash_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/splash.png")
 
# 元素4:飞船图像和初始数值
ship_info = ImageInfo([45, 45], [90, 90], 35)
ship_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/double_ship.png")
 
# 元素5:导弹的图像和初始数值
missile_info = ImageInfo([5,5], [10, 10], 3, 50)
missile_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/shot2.png")
 
# 元素6:小行星图像和初始数值
asteroid_info = ImageInfo([45, 45], [90, 90], 40)
asteroid_image = simplegui.load_image("http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/asteroid_blue.png")
 
# 元素7:爆炸动画和初始数值
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")
 
# 元素8:声音和初始数值
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")
 
# 初始化飞船的对象和两个Sprite类的对象,注意后面非数值参数都是ImageInfo的对象
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)
a_missile = Sprite([2*WIDTH/3, 2*HEIGHT/3], [-1,1], 0, 0, missile_image, missile_info, missile_sound)
 
# register handlers
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)
timer = simplegui.create_timer(1000.0, rock_spawner)
 
# get things rolling
timer.start()
frame.start()
本文出自 Frank's Blog

版权声明:


本文链接:Python – Spaceship 讲解
版权声明:本文为原创文章,仅代表个人观点,版权归 Frank Zhao 所有,转载时请注明本文出处及文章链接
你可以留言,或者trackback 从你的网站

留言哦

blonde teen swallows load.xxx videos