小学生python游戏编程arcade----基本知识1

前言

前面章节分享试用了pyzero,pygame但随着想增加更丰富的游戏内容,好多还要进行自己编写类,从今天开始解绍一个新的python游戏库arcade模块。

基本知识

1、简单窗体

在这里插入图片描述中文自动支持,看标题,其它什么都没改

import arcade

# 设置
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 650
SCREEN_TITLE = "我的游戏"


class MyGame(arcade.Window):
    def __init__(self):
        # Call the parent class and set up the window
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

        arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)

    def setup(self):
        """Set up the game here. Call this function to restart the game."""
        pass

    def on_draw(self):
        """Render the screen."""

        self.clear()
        # Code to draw the screen goes here


def main():
    """Main function"""
    window = MyGame()
    window.setup()
    arcade.run()


if __name__ == "__main__":
    main()

从代码中可以看到:
您可以通过四种方式指定颜色:
标准的CSS颜色名称 Arcade.css颜色包 : arcade.csscolor.RED
非标准颜色名称(此程序包): arcade.color.RED
三个字节的数字: (255, 0, 0)
四个字节的数字(第四个字节是透明度。0透明,255不透明): (255, 0, 0, 255)
代码可试:
arcade.set_background_color(arcade.csscolor.RED),背景换成了红色

2、试着添加角色及背景

在这里插入图片描述

# _*_ coding: UTF-8 _*_
# 开发团队: 信息化未来
# 开发人员: Administrator
# 开发时间:2022/11/4 16:59
# 文件名称: game1.py
# 开发工具: PyCharm
import arcade

# 设置
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 650
SCREEN_TITLE = "我的游戏"

# 常量用于从原始大小缩放精灵
CHARACTER_SCALING = 1
TILE_SCALING = 0.5

class MyGame(arcade.Window):
    def __init__(self):
        # Call the parent class and set up the window
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
        self.wall_list = None
        self.player_list = None
        # Separate variable that holds the player sprite
        self.player_sprite = None

        arcade.set_background_color(arcade.csscolor.RED)


    def setup(self):
        """Set up the game here. Call this function to restart the game."""
        self.player_list = arcade.SpriteList()
        self.wall_list = arcade.SpriteList(use_spatial_hash=True)

        # 添加角色.
        image_source = "images/bird.png"
        self.player_sprite = arcade.Sprite(image_source, CHARACTER_SCALING)
        self.player_sprite.center_x = 64
        self.player_sprite.center_y = 128
        self.player_list.append(self.player_sprite)

        # 利用循环创建背景
        for x in range(0, 1440, 480):
            wall = arcade.Sprite("images/bg2.png", TILE_SCALING)
            wall.center_x = x
            wall.center_y = 650/2
            self.wall_list.append(wall)

        # 在地上放些板条箱, 这显示了使用坐标列表放置精灵
        coordinate_list = [[512, 96], [256, 96], [768, 96]]

        for coordinate in coordinate_list:
            # Add a crate on the ground
            wall = arcade.Sprite(
                ":resources:images/tiles/boxCrate_double.png", TILE_SCALING
            )
            wall.position = coordinate
            self.wall_list.append(wall)

    def on_draw(self):
        self.clear()
        self.wall_list.draw()
        self.player_list.draw()


def main():
    """Main function"""
    window = MyGame()
    window.setup()
    arcade.run()


if __name__ == "__main__":
    main()

3、场景

在这里插入图片描述

import arcade

# 设置
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 650
SCREEN_TITLE = "我的游戏"

# 常量用于从原始大小缩放精灵
CHARACTER_SCALING = 1
TILE_SCALING = 0.5

class MyGame(arcade.Window):
    def __init__(self):
        # Call the parent class and set up the window
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
        self.scene = None
        self.player_sprite = None

        arcade.set_background_color(arcade.csscolor.BLUE)


    def setup(self):

        # 初始化场景
        self.scene = arcade.Scene()

        # 添加精灵列表
        self.scene.add_sprite_list("Player")
        self.scene.add_sprite_list("Walls", use_spatial_hash=True)

        # 添加角色.
        image_source = "images/bird.png"
        self.player_sprite = arcade.Sprite(image_source, CHARACTER_SCALING)
        self.player_sprite.center_x = 264
        self.player_sprite.center_y = 128
        self.scene.add_sprite("Player", self.player_sprite)

        wall = arcade.Sprite("images/1-1.png", TILE_SCALING)
        wall.center_x = SCREEN_WIDTH /2
        wall.center_y = SCREEN_HEIGHT /2
        self.scene.add_sprite("Walls", wall)



    def on_draw(self):
        self.clear()
        self.scene.draw()
        # self.wall_list.draw()
        # self.player_list.draw()


def main():
    """Main function"""
    window = MyGame()
    window.setup()
    arcade.run()


if __name__ == "__main__":
    main()

4、角色控制及物理引擎

需要创建一个物理引擎,以移动我们的角色,并防止她跑过墙壁。这个 PhysicsEngineSimple 类有两个参数:移动的精灵和移动的精灵不能移动的精灵列表。有关物理引擎的更多信息,请参阅 arcade.PhysicsEngineSimple 。
在这里插入图片描述

# _*_ coding: UTF-8 _*_
# 开发团队: 信息化未来
# 开发人员: Administrator
# 开发时间:2022/11/4 16:59
# 文件名称: game1.py
# 开发工具: PyCharm
import arcade

# 设置
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 650
SCREEN_TITLE = "我的游戏"

# 常量用于从原始大小缩放精灵
CHARACTER_SCALING = 1
TILE_SCALING = 0.5
PLAYER_MOVEMENT_SPEED = 2.5


class MyGame(arcade.Window):
    def __init__(self):
        # Call the parent class and set up the window
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
        self.scene = None
        self.player_sprite = None

        arcade.set_background_color(arcade.csscolor.BLUE)

        self.physics_engine = None

    def setup(self):

        # 初始化场景
        self.scene = arcade.Scene()

        # 添加精灵列表
        self.scene.add_sprite_list("Player")
        self.scene.add_sprite_list("Walls", use_spatial_hash=True)

        # 添加角色.
        image_source = "images/1-1.png"
        self.player_sprite = arcade.Sprite(image_source, CHARACTER_SCALING)
        self.player_sprite.center_x = 264
        self.player_sprite.center_y = 128
        self.scene.add_sprite("Player", self.player_sprite)

        wall = arcade.Sprite("images/房子.png", TILE_SCALING)
        wall.center_x = SCREEN_WIDTH /2
        wall.center_y = SCREEN_HEIGHT /2
        self.scene.add_sprite("Walls", wall)

        # 物理引
        self.physics_engine = arcade.PhysicsEngineSimple(
            self.player_sprite, self.scene.get_sprite_list("Walls"))

    def on_draw(self):
        self.clear()
        self.scene.draw()
        # self.wall_list.draw()
        # self.player_list.draw()

    def on_key_press(self, key, modifiers):
        """Called whenever a key is pressed."""

        if key == arcade.key.UP or key == arcade.key.W:
            self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
        elif key == arcade.key.DOWN or key == arcade.key.S:
            self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
        elif key == arcade.key.LEFT or key == arcade.key.A:
            self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
        elif key == arcade.key.RIGHT or key == arcade.key.D:
            self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED

    def on_key_release(self, key, modifiers):
        """Called when the user releases a key."""

        if key == arcade.key.UP or key == arcade.key.W:
            self.player_sprite.change_y = 0
        elif key == arcade.key.DOWN or key == arcade.key.S:
            self.player_sprite.change_y = 0
        elif key == arcade.key.LEFT or key == arcade.key.A:
            self.player_sprite.change_x = 0
        elif key == arcade.key.RIGHT or key == arcade.key.D:
            self.player_sprite.change_x = 0

    def on_update(self, delta_time):
        """Movement and game logic"""

        # Move the player with the physics engine
        self.physics_engine.update()


def main():
    """Main function"""
    window = MyGame()
    window.setup()
    arcade.run()


if __name__ == "__main__":
    main()```

总结

通过此次的《连连看》游戏实现,让我对swing的相关知识有了进一步的了解,对java这门语言也有了比以前更深刻的认识。

java的一些基本语法,比如数据类型、运算符、程序流程控制和数组等,理解更加透彻。java最核心的核心就是面向对象思想,对于这一个概念,终于悟到了一些。

源码获取

关注博主后,私聊博主免费获取
需要技术指导,育娃新思考,企业软件合作等更多服务请联系博主

今天是以此模板持续更新此育儿专栏的第 12 /50次。
可以关注我,点赞我、评论我、收藏我啦。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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
### 回答1: 我可以帮你使用Pythonarcade库来创建一个平台类游戏arcade库提供了一个简单而强大的API,可以帮助你创建2D游戏。它还有一个文档,可以帮助你开始创建游戏,并且拥有许多示例代码,可以让你学习如何使用这个库。 ### 回答2: 使用Pythonarcade库可以很方便地制作平台类游戏arcade库提供了许多内置函数和类,用于处理游戏循环、精灵、碰撞检测等常见的游戏开发任务。 首先,我们需要创建一个游戏窗口。通过arcade库提供的Window类,我们可以创建一个指定大小的窗口,并设置背景颜色。 接下来,我们可以创建角色或者其他游戏中的物体,这些物体被称为精灵。arcade库提供了Sprite类用于创建精灵。我们可以设置精灵的外观、位置、大小等属性。 在游戏循环中,我们可以监听用户输入,例如键盘按键、鼠标点击等事件。通过事件处理函数,我们可以根据用户的操作来更新精灵的状态。 在平台类游戏中,通常会涉及到角色的跳跃、移动、碰撞检测等功能。arcade库提供了一些内置函数和方法来处理这些功能。例如,我们可以使用移动函数来控制角色的水平和垂直移动。我们还可以使用碰撞检测函数来判断角色是否与其他物体发生了碰撞。 此外,arcade库还可以处理音效、动画等高级功能,以及绘制背景、地图等元素。它还提供了一些内置的工具函数,用于计算距离、旋转等常见操作。 总的来说,使用Pythonarcade库可以实现一个简单而又有趣的平台类游戏。通过利用arcade库提供的丰富功能,可以很方便地创建精灵、处理用户输入、实现碰撞检测等游戏开发任务。无论是新手还是有经验的开发者,都可以轻松上手使用arcade库制作自己的平台类游戏。 ### 回答3: 使用Pythonarcade库可以很方便地制作平台类游戏。首先,我们需要导入arcade库。然后创建一个继承自arcade.Window的类,作为游戏窗口。 在窗口类中,我们可以定义一些属性,比如玩家的位置和角色模型等。然后,我们可以使用arcade库提供的绘图函数来渲染游戏界面。 接着,我们可以定义一些事件处理函数,比如按键事件或鼠标事件。根据不同的事件,我们可以更新游戏状态,比如移动玩家或触发一些特殊的效果。 在游戏循环中,我们可以使用arcade库提供的定时器函数来设定游戏帧率。每一帧,我们可以调用draw函数来绘制游戏界面,并在需要时更新游戏状态。 此外,我们可以使用arcade库提供的物理引擎功能,比如重力或碰撞检测,来增加游戏的真实性和挑战性。 最后,当玩家达到游戏的胜利条件或失败条件时,我们可以显示游戏结束画面,并提供一些重新开始或退出游戏的选项。 总的来说,使用Pythonarcade库可以很方便地制作一个平台类游戏。通过定义游戏窗口、绘制游戏界面、处理事件和更新游戏状态等步骤,我们可以创建一个有趣且具有挑战性的游戏体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信息化未来

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值