前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >捡金币2

捡金币2

作者头像
叶子陪你玩
发布2022-02-08 18:38:17
2850
发布2022-02-08 18:38:17
举报

在上篇捡金币游戏的基础上,加上了开始选择和分数统计,结束功能。

gui.py

代码语言:javascript
复制
import arcade
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

def draw_button(content,center_x,center_y,font_size=20):
    arcade.draw_rectangle_filled(
        center_x,
        center_y,
        100,
        50,
        arcade.color.WHITE,
    )
    arcade.draw_text(content,
                     center_x,
                     center_y,
                     arcade.color.BLACK,
                     font_size,
                     anchor_x="center",
                     anchor_y="center",
                     font_name=('arial',)
                     )

main.py

代码语言:javascript
复制
import arcade
import random
from gui import draw_button
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Starting Template"

# 玩家
class Player(arcade.Sprite):
    def __init__(self,image):
        super().__init__(image)
        self.center_x = SCREEN_WIDTH//2
        self.center_y = self.height//2

    def move(self,direction):
        # 左移
        if direction == "left" and self.center_x >0:
            self.center_x = self.center_x - 20
        # 右移
        elif direction == "right" and self.center_x < SCREEN_WIDTH:
            self.center_x = self.center_x + 20

# 金币
class Coin(arcade.Sprite):
    def __init__(self,image):
        super().__init__(image)
        self.center_x = SCREEN_WIDTH//2
        self.center_y = SCREEN_HEIGHT-self.height//2

# 创建游戏类
class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.setup()

    def setup(self):
        arcade.set_background_color(arcade.color.SKY_BLUE)
        self.player = Player('robot.png')
        self.coins = arcade.SpriteList()

        # 程序运行总时间
        self.total_time = 0
        # 按键控制变量
        self.direction = None
        self.score = 0
        # 是否开始
        self.is_start = False

    def on_draw(self):
        arcade.start_render()
        self.player.draw()
        if self.coins:
            for coin in self.coins:
                coin.draw()
        arcade.draw_text('score:{}'.format(self.score),
                         SCREEN_WIDTH-120 , SCREEN_HEIGHT - 30,
                         arcade.color.RED, 20,
                         font_name=('arial',)
                         )

        if not self.is_start:
            draw_button('开始',SCREEN_WIDTH // 2,SCREEN_HEIGHT-200,20)
            draw_button('音乐', SCREEN_WIDTH // 2, SCREEN_HEIGHT - 300, 20)


    def on_update(self, delta_time):
        if self.is_start:
            # 记录程序经过的时间
            self.total_time = self.total_time + delta_time

            if self.total_time >= 0.5:
                c = Coin('gold.png')
                x = random.randint(50,750)
                c.center_x = x
                self.coins.append(c)
                self.total_time = 0

            for coin in self.coins:
                # 金币落下
                coin.center_y -= 5
                # 判断是否超出底边界
                if coin.center_y < 0:
                    self.coins.remove(coin)

            # 与金币列表碰撞检测,返回所有碰到的金币
            coin_list = self.player.collides_with_list(self.coins)
            # 循环删除所有碰到的金币
            for c in coin_list:
                self.score = self.score + 1
                self.coins.remove(c)

            if self.direction =='left':
                self.player.move("left")
            elif self.direction =='right':
                self.player.move("right")

            if self.score >=10:
                exit()

    def on_key_press(self, key, key_modifiers):
        if key == arcade.key.A:
            self.direction = 'left'

        elif key == arcade.key.D:
            self.direction = 'right'

    def on_key_release(self, key, key_modifiers):
        if key == arcade.key.A or key == arcade.key.D:
            self.direction = None

    def on_mouse_press(self, x, y, button, key_modifiers):
        if x>(SCREEN_HEIGHT - 200 - 50) and x<(SCREEN_HEIGHT - 200 + 50) and y>(SCREEN_HEIGHT - 200-25) and y<(SCREEN_HEIGHT - 200+25):
            self.is_start = True

if __name__ == '__main__':
    # 实例对象
    game = MyGame(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_TITLE)
    arcade.run()
本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-01-16,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 叶子陪你玩编程 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com