使用pygame zero 开发游戏2(带例子)

一。上一篇pygame zero (pgzero)的游戏例子中,有几个函数是可以重点介绍的。

1.动画函数 animate()

 函数功能:在窗口中移动一个角色。

函数参数:第一个参数是你想要移动的角色

tween=这个可选参数用来改变动画效果

duration=这个参数设置动画持续的时间是多少秒

on_finished=这个参数能让你指定一个函数,当动画结束时,这个函数就会被自动调用。

最后一个参数用来指定你想要移动的角色的属性。属性可以设置多个。举例来说,如果角色目前位于(0,0),你想把它移动到(100,0)的位置,最后一个参数就写成x=100,就可以了。

animate()函数的移动过程很平滑,移动过程耗费的时间和duration参数指定的是一样的。

2.定时:clock.schedule_interval(handle_func,time)

用途:每隔time时间之后,执行一次handle_func函数。

3.生成游戏角色可以用下面的语句

<变量> =Actor("str", (x, y))    #生成一个游戏角色,导入一个名为str图片

之后,如果要更换这个角色的图像,直接用   <变量>.image="newstr",  就可以完成变换。这个是用来实现角色动作变化的方法。

二。编写一个游戏来进一步熟悉这些函数

1.游戏介绍:屏幕上出现多个 不同颜色的五角星,从上往下掉落,掉落速度不一样。如果有一个五角星接触到底部边缘,就失败。掉落过程中,五角星会平移交换位置。游戏者只要及时用鼠标左键点中红色的五角星,就会进入下一关(更多的五角星,更快的下降速度,一共五关)。如果点到了其它颜色的五角星,也会失败。失败之后,按空格键可以重新开始。

2.游戏截图:

源代码和图片见资源文件

import pygame
import pgzrun
import random

FONT_COLOUR=(255,255,255)

WIDTH=800
HEIGHT=600

CENTER_X=WIDTH/2
CENTER_Y=HEIGHT/2
CENTER=(CENTER_X,CENTER_Y)

FINAL_LEVEL =10
START_SPEED=10
COLOURS=["green","blue"]

game_over=False
game_complete=False
current_level=6
stars=[]
animations=[]

def draw():
    global stars,current_level,game_over,game_complete
    screen.clear()
    screen.blit("space",(0,0))
    if game_over:
        display_message("GAME OVER!","TRY AGAIN.")
    elif game_complete:
        display_message("You Win!","Well Done.")
    else:
        for star in stars:
            star.draw()
def update():
    global  stars,game_over,game_complete,current_level
    if len(stars) ==0:
        stars= make_stars(current_level)
    if(game_complete or game_over) and keyboard.space:
        stars=[]
        current_level=6
        game_complete=False
        game_over=False
def make_stars(number_of_extra_stars):
    colours_to_create=get_colours_to_create(number_of_extra_stars)
    new_stars=create_stars(colours_to_create)
    layout_stars(new_stars)
    animate_stars(new_stars)
    return new_stars
def get_colours_to_create(number_of_extra_stars):
    colours_to_create=["red"]
    for i in range(0,number_of_extra_stars):
        random_colur= random.choice(COLOURS)
        colours_to_create.append(random_colur)

    return colours_to_create

def create_stars(colours_to_create):
    new_stars=[]
    for colour in colours_to_create:
        star =Actor(colour+"-star")
        new_stars.append(star)

    return new_stars
def layout_stars(stars_to_layout):
    number_of_gaps=len(stars_to_layout)+1
    gap_size=WIDTH/number_of_gaps
    random.shuffle(stars_to_layout)
    for index,star in enumerate(stars_to_layout):
        new_x_pos=(index+1)*gap_size
        star.x=new_x_pos

def animate_stars(stars_to_animate):
    for star in stars_to_animate:
        duration =START_SPEED-current_level+random.randint(0,3)
        star.anchor=("center","bottom")
        animation=animate(star,duration=duration,on_finished=handle_game_over,y=HEIGHT)
        animations.append(animation)
def handle_game_over():
    global game_over
    game_over=True
def on_mouse_down(pos):
    global stars,current_level
    for star in stars:
        if star.collidepoint(pos):
            if "red" in star.image:
                red_star_click()
            else:
                handle_game_over()
def red_star_click():
    global  current_level,stars,animations,game_complete
    stop_animations(animations)
    if current_level==FINAL_LEVEL:
        game_complete=True
    else:
        current_level+=1
        stars=[]
        animations=[]
def stop_animations(animations_to_stop):
    for animation in animations_to_stop:
        if animation.running:
            animation.stop()
def display_message(heading_text,sub_heading_text):
    screen.draw.text(heading_text,fontsize=60,center=CENTER,color=FONT_COLOUR)
    screen.draw.text(sub_heading_text,fontsize=30,center=(CENTER_X,CENTER_Y+30),color=FONT_COLOUR)

def shuffle():
    global stars
    if stars:
        x_values=[star.x for star in stars]
        random.shuffle(x_values)
        for index,star in enumerate(stars):
            new_x=x_values[index]
            animation=animate(star,duration=0.3,x=new_x)
            animations.append(animation)
clock.schedule_interval(shuffle,0.9)
pgzrun.go()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值