[Python] Spaceship游戏初步实现 Mini-project #7

9 篇文章 0 订阅
6 篇文章 0 订阅

Spaceship顾名思义是一款太空游戏,受经典的街机游戏Asteroid(1979)启发编写而成。在Mini-project #7中,我们先实现飞船的控制、导弹的发射、星球的随机出现和运动,简单设置UI,从而展示Lives和Score。在Mini-project #8中会增加更多功能。

具体思路如下(注:这次描述不是按照代码显示顺序,而是按照完成的思路写的):

游戏细节描述在Coursera的interactive-python教程中,见此:https://www.coursera.org/learn/interactive-python-2/supplement/2ZVsF/mini-project-description

首先导入所需库,设置全局变量

import simplegui
import math
import random

# globals for user interface
WIDTH = 800
HEIGHT = 600
score = 0
lives = 3
time = 0

1. 创建飞船类

# 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):
        #canvas.draw_circle(self.pos, self.radius, 1, "White", "White")
        if self.thrust: #change image status
            canvas.draw_image(self.image, [130, 45], 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): #这里是重头戏,飞船的加速度改变影响速度,速度也受阻力影响
        #update angle
        self.angle += self.angle_vel
        c = 0.2
        #update position
        self.pos[0] = (self.pos[0] + self.vel[0]) % WIDTH
        self.pos[1] = (self.pos[1] + self.vel[1]) % HEIGHT
        #update velocity
        self.vel[0] *= (1-c) #摩擦力
        self.vel[1] *= (1-c)

        if self.thrust:
            forward = angle_to_vector(self.angle)
            self.vel[0] += forward[0]
            self.vel[1] += forward[1]
     #change angle
    def set_thrust(self, on):
        self.thrust = on
        if on: #add sound to thrust
            ship_thrust_sound.rewind()
            ship_thrust_sound.play()
        else:
            ship_thrust_sound.pause()

    def increase_angvel(self):
        self.angle_vel += .05

    def decrease_angvel(self):
        self.angle_vel -= .05

    #add missile and shooting
    def shoot(self):
        global a_missile
        forward = angle_to_vector(self.angle)
        pos = [self.pos[0], self.pos[1]]
        pos[0] += vel[0] + self.radius
        pos[1] += vel[1] + self.radius
        vel[0] = self.vel[0] + (vel[0] * 6)
        vel[1] = self.vel[1] + (vel[1] * 6)
        a_missile = Sprite(pos, vel, self.angle, 0, missile_image, missile_info, missile_sound)


def increase_angvel(self): self.angle_vel += .05 #试合适的值 def decrease_angvel(self): self.angle_vel -= .05 #add missile and shooting 发射导弹 def shoot(self): global a_missile forward = angle_to_vector(self.angle) missile_pos = [self.pos[0]+self.radius*forward[0],self.pos[1]+self.radius*forward[1]] missile_vel = [self.vel[0]+5*forward[0], self.vel[1]+5*forward[1]] a_missile = Sprite(missile_pos, missile_vel, self.angle, 0, missile_image, missile_info, missile_sound) #使用Sprite类,见下面说明

 键盘控制: 

def keydown(key):
    if key == simplegui.KEY_MAP['left']: #逆时针旋转 所以decrease angle_velocity
        my_ship.decrease_angvel()
    elif key == simplegui.KEY_MAP['right']:
        my_ship.increase_angvel()
    elif key == simplegui.KEY_MAP['up']:
        my_ship.set_thrust(True)
    elif key == simplegui.KEY_MAP['space']:
        my_ship.shoot()
        
def keyup(key):
    if key == simplegui.KEY_MAP["left"]:
        my_ship.increase_angvel()
    elif key == simplegui.KEY_MAP['right']:
        my_ship.decrease_angvel()
    elif key == simplegui.KEY_MAP['up']:
        my_ship.set_thrust(False)
要想键盘能控制,在注册处理器(register handlers)时要添加set_keydown_handler和set_keyup_handler(最后代码中有写)

frame.set_keydown_handler(keydown)#添加键盘控制
frame.set_keyup_handler(keyup)
2. 创建Sprite类。星石(rock)、导弹(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):
        # 第二个self.image_size是指按图像大小显示
        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 # make the sprite move and rotate

        self.pos[0] = (self.pos[0]+self.vel[0])%WIDTH
        self.pos[1] = (self.pos[1]+self.vel[1])%HEIGHT
创建函数使星石定时随机产生
# timer handler that spawns a rock    
def rock_spawner(): #set a_rock to be a new rock on every tick
    global a_rock
    a_rock = Sprite([random.randrange(WIDTH),random.randrange(HEIGHT)], [random.randrange(-1,1),random.randrange(-1,1)], 0.05, 0.05, asteroid_image, asteroid_info)
3. 在画面中显示背景、星云等

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()
添加UI,显示所剩生命(Lives)和所得分数(Score):

    # draw score and lives
    canvas.draw_text('Lives:'+ str(lives), (50,50), 28, "Red")
    canvas.draw_text('Score:'+ str(score), (625,50), 28, "Red")
4. 导入飞船、导弹、星石、星云等图片(这个步骤中的代码写在“创建飞船类”之前,我是按照功能的实现描述的)

在一开始先创建好图片信息类:

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_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")

# alternative upbeat soundtrack by composer and former IIPP student Emiel Stopler
# please do not redistribute without permission from Emiel at http://www.filmcomposer.nl
#soundtrack = simplegui.load_sound("https://storage.googleapis.com/codeskulptor-assets/ricerocks_theme.mp3")
设置基本函数
#角度转为向量
# helper functions to handle transformations
def angle_to_vector(ang):
    return [math.cos(ang), math.sin(ang)]
#p,q距离,(a^2+b^2)^0.5
def dist(p,q):
    return math.sqrt((p[0] - q[0]) ** 2+(p[1] - q[1]) ** 2)
5. 初始化画面设置与开始

# 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.05, asteroid_image, asteroid_info) #0.05秒旋转一下
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()
6. 游戏界面展示:

谢谢阅读!欢迎互相学习~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值