python爬取网页下载按钮

大家好,小编来为大家解答以下问题,python 爬取网页内容并保存到数据库,利用python爬取简单网页数据步骤,现在让我们一起来看看吧!

python制作的赛车小游戏,逆行飙车,通过键盘方向键控制 程序运行截图: 源代码

import pygame, sys, time, random

# pygame 初始化
()
pygame.display.set_caption('逆行飙车')


# 常量类
class Constant:
    # 自定义事件
    OUT_OF_SIDE = pygame.USEREVENT + 1  # 碰到边界
    SPEED_UP = pygame.USEREVENT + 2  # 速度加快

    # 初始分数
    SCORE = 0

    # 帧数
    FPS = 30


# 屏幕
size = width, height = (351, 600)
screen = pygame.display.set_mode(size)  # 屏幕surface
screen_rect = screen.get_rect()  # 屏幕rect

# 背景图片
street_img = ('')  # 背景surface
street_rect = street_img.get_rect()  # 背景rect

# 字体和音乐
font_big = ('', 60)
font_small = ('文道甜甜圈.ttf', 20)
font_game_over = font_big.render('Game Over !', True, (255, 0, 0))
font_game_over_rect = font_game_over.get_rect()
font_game_over_rect.center = screen_rect.center
font_score = font_small.render(f'SCORE: {Constant.SCORE}', True, (255, 255, 255))
font_score_rect = font_score.get_rect()
font_score_rect.x = 10
font_score_rect.y = 10

# 定时添加自定义事件
.set_timer(Constant.SPEED_UP, 5000)  #

clock = .Clock()


# 敌人类
class Enemy(pygame.sprite.Sprite):

    def __init__(self):
        super().__init__()  # 调用父类方法初始化对象
         = ('')
         = pygame.Surface((40, 48))
         = .get_rect(left=screen.get_width() / 2 - 22, top=0)
        self.speed = 5

    def move(self):
        global font_score
        .move_ip(0, self.speed)
        if  > screen_rect.height:
            Constant.SCORE += 1
            font_score = font_small.render(f'SCORE: {Constant.SCORE}', True, (255, 255, 255))
             = 0
             = random.randint(60, 250)


# 玩家类
class Player(pygame.sprite.Sprite):

    def __init__(self):
        super().__init__()  # 调用父类方法初始化对象
         = ('')
         = .get_rect()
        .midbottom = screen_rect.midbottom
        self.speed = 5

    def move(self):
        pressed_keys = .get_pressed()
        if pressed_keys[pygame.K_UP] and  >= 0:
            .move_ip(0, -self.speed)
        if pressed_keys[pygame.K_DOWN] and .bottom <= screen.get_height():
            .move_ip(0, self.speed)
        if pressed_keys[pygame.K_LEFT] and  >= 33:
            .move_ip(-self.speed, 0)
        if pressed_keys[pygame.K_RIGHT] and .right <= screen.get_width() - 34:
            .move_ip(self.speed, 0)
        # 超出边界
        if  <= 50 or .right >= screen.get_width() - 52:
            (pygame.event.Event(Constant.OUT_OF_SIDE))


# 游戏类
class Game:
    # 游戏初始化
    def __init__(self):
        self.player = Player()
        self.enemy = Enemy()
        # 定义敌人精灵组
        self.enemies = pygame.sprite.Group()
        (self.enemy)

        # 定义所有精灵组
        self.all_sprites = pygame.sprite.Group()
        (self.player)
        (self.enemy)

    # 游戏运行/循环
    def run(self):

        # 播放背景音乐
        pygame.mixer.Sound("background.mp3").play(-1)

        while 1:

            (street_img, street_rect)
            (font_score, font_score_rect)

            for event in ():
                if  == :
                    ()
                    ()
                # 自定义事件触发
                if  == Constant.OUT_OF_SIDE:
                    print('超出边界了')
                if  == Constant.SPEED_UP:  # 敌人 加速 事件
                    # print('speed up')
                    self.enemy.speed += 1

            for sprite in self.all_sprites:
                (, )
                ()

            # 发生碰撞4种情况:

            # 1、敌人和玩家都存在
            # if pygame.sprite.spritecollide(player, enemies, False):
            #     print('撞车了')

            # 2、敌人消失
            # if pygame.sprite.spritecollide(player, enemies, True):
            #     print('撞车了')

            # 3、玩家和敌人都消失
            # if pygame.sprite.spritecollide(player, enemies, True):
            #     ()   # 单独控制某个精灵消失
            #     print('撞车了')

            # 4、玩家消失
            if pygame.sprite.spritecollideany(self.player, self.enemies):
                # 延迟2秒 刷新退出

                # 播放碰撞声音
                pygame.mixer.Sound('crash.mp3').play()
                # 玩家从所有精灵组移除
                ()

                # 显示游戏结束任务
                (font_game_over, font_game_over_rect)
                # 更新显示字体
                pygame.display.update()

                time.sleep(3)
                ()
                ()

            ()

            ()

if __name__ == '__main__':
    Game().run()
# 这个代码较为完整

完整赛车小游戏下载:赛车小游戏源代码Python代码大全,海量代码任你下载


原文地址:https://blog.csdn.net/weixin_42756970/article/details/127387062

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值