arcade库做反弹小球(python)

        大家好!今天来用 python中的arcade库做小游戏。arcade库相对于pygame库知名度相对低一些,但我认为这个库做游戏也很不错。

1.整体代码

import arcade
import time
print("欢迎游玩反弹小球!  v1.0.2")
print("下面是一些操作提示,请认真阅读。")
print("1.左侧是玩家,用上键和下键进行移动;右侧是AI。")
print("2.打到球随机获得1-3个金币,没有打到球则自动退出游戏。")
print("5秒后进入游戏。")

time.sleep(5)

class MyWindow(arcade.Window):
    def __init__(self):
        super().__init__(600,700,"反弹小球")

    def setup(self):
        self.ball=arcade.Sprite("res/smallball.png",scale=1,center_x=400,center_y=200)
        self.block_1=arcade.Sprite("res/block_1.png",scale=1,center_x=15,center_y=350)
        self.block_2=arcade.Sprite("res/block_2.png",scale=1,center_x=585,center_y=350)
        self.line=arcade.Sprite("res/line.png",scale=1,center_x=300,center_y=350)

        self.speed=2
        self.ball.change_x=self.speed
        self.ball.change_y=self.speed
        self.ball.change_angle=1
        self.music=arcade.load_sound("res/get.mp3")
        self.music2=arcade.load_sound("res/end.mp3")
        self.coin=0
    
    def on_draw(self):
        arcade.start_render()
        self.ball.draw()
        self.block_1.draw()
        self.block_2.draw()
        self.line.draw()
        arcade.draw_text("玩家",140,50,(150,150,150),font_size=40,anchor_x="center",anchor_y="center",font_name="res/黑体.ttf")
        arcade.draw_text("AI",465,50,(150,150,150),font_size=40,anchor_x="center",anchor_y="center",font_name="res/黑体.ttf")

    def on_update(self, delta_time: float):
        
        if arcade.check_for_collision(self.block_1,self.ball)==True:
            self.ball.change_x=self.speed
            arcade.play_sound(self.music)
            self.coin+=1

        if arcade.check_for_collision(self.block_2,self.ball)==True:
            self.ball.change_x=-self.speed
            self.speed+=0.2
        if self.ball.top>=700:
            self.ball.change_y=-self.speed

        if self.ball.right>=600:
            self.ball.change_x=-self.speed

        if self.ball.bottom<=0:
            self.ball.change_y=self.speed            

        if self.ball.left<=0:
            arcade.play_sound(self.music2)
            time.sleep(2)
            print("恭喜您获得了",self.coin,"枚金币!")
            self.close()

        if self.block_1.change_y>0 and self.block_1.top>=700:
            self.block_1.top=700
            self.block_1.change_y=0

        if self.block_1.change_y<0 and self.block_1.bottom<=0:
            self.block_1.bottom=0
            self.block_1.change_y=0

        if self.block_2.change_y<0 and self.block_2.top<=0:
            self.block_2.top=700
            self.block_2.change_y=0

        if self.block_2.change_y<0 and self.block_2.bottom<=0:
            self.block_2.bottom=0
            self.block_2.change_y=0

        self.block_2.center_y=self.ball.center_y

        self.ball.update()
        self.block_1.update()
        self.block_2.update()

    def on_key_press(self, symbol: int, modifiers: int):
        if symbol==arcade.key.UP:
            self.block_1.change_y=10

            if self.speed==5:
                self.speed=5

        if symbol==arcade.key.DOWN:
            self.block_1.change_y=-10
            self.speed+=0.2
            if self.speed==5:
                self.speed=3

    def on_key_release(self, symbol: int, modifiers: int):
        if symbol==arcade.key.UP or symbol==arcade.key.DOWN:
            self.block_1.change_y=0   

win=MyWindow()
win.setup()
arcade.run()

2.角色注意

       以上是全部代码。其中的smallball、block_1、block_2、line是四个图片,可以自己用画图软件绘制绘制。

block_1:长100像素,宽30像素的白色填充长方形。

block_2:长100像素,宽30像素的白色填充长方形 。

line:长700像素,宽20像素的白色填充长方形。

ball:直径24像素的白色填充圆形。

绘制完后需要保存到res文件夹。

3.代码解析

     1.使用__init__函数修改窗口数据

     2.使用setup函数设置基础数据

           1.设置角色(smallball、block_1、block_2、line)

           2.设置小球初始数值(速度、旋转)

           3.设置音乐数值

           4.设置金币数值

     3.使用on_draw函数绘制角色和文字

     4.使用on_update函数更新数据

           1.检查小球和两个挡板的碰撞

           2.检查两个挡板的上下限制

           3.更新数据

     5.使用on_key_press函数和on_key_release函数检测键盘事件

4.注意事项

 注意,如果没有安装arcade库,需要用如下代码在终端安装。

pip install arcade

5.玩法

使用上、下键控制左侧挡板移动,打到得分,没打到游戏自动退出。

Learn and use Python and PyGame to design and build cool arcade games. In Program Arcade Games: With Python and PyGame, Second Edition, Dr. Paul Vincent Craven teaches you how to create fun and simple quiz games; integrate and start using graphics; animate graphics; integrate and use game controllers; add sound and bit-mapped graphics; and build grid-based games. After reading and using this book, you'll be able to learn to program and build simple arcade game applications using one of today's most popular programming languages, Python. You can even deploy onto Steam and other Linux-based game systems as well as Android, one of today's most popular mobile and tablet platforms. You'll learn: How to create quiz games How to integrate and start using graphics How to animate graphics How to integrate and use game controllers How to add sound and bit-mapped graphics How to build grid-based games Audience This book assumes no prior programming knowledge. Table of Contents Chapter 1: Before Getting Started… Chapter 2: Create a Custom Calculator Chapter 3: What Is a Computer Language? Chapter 4: Quiz Games and If Statements Chapter 5: Guessing Games with Random Numbers and Loops Chapter 6: Introduction to Graphics Chapter 7: Back to Looping Chapter 8: Introduction to Lists Chapter 9: Introduction to Animation Chapter 10: Functions Chapter 11: Controllers and Graphics Chapter 12: Bitmapped Graphics and Sound Chapter 13: Introduction to Classes Chapter 14: Introduction to Sprites Chapter 15: Libraries and Modules Chapter 16: Searching Chapter 17: Array-Backed Grids Chapter 18: Sorting Chapter 19: Exceptions Chapter 20: Recursion Chapter 21: Formatting Chapter 22: Exercises
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值