猫史档案馆


【Python作品分享】谷歌小恐龙【作品秀】

用户:活波的木叶龙4Lii活波的木叶龙4Lii查看:4 回复:5 评论:4 创建时间:2022-03-25T11:36:30


【作品展示】

center_image

 

【作品介绍】

使用arcade库

还原谷歌浏览器的小恐龙(没声音。。。)

耗时3天

 

【作品源代码】

from arcade import *
import json

# 设置窗体宽和高
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 200
# 设置窗体标题
SCREEN_TITLE = '谷歌小恐龙'


class Road(Sprite):
    def __init__(self, image):
        super().__init__(image)
        self.center_x = SCREEN_WIDTH
        self.center_y = SCREEN_HEIGHT // 2
        self.change_x = -3
        self.wait = 0

    def update(self):
        super().update()
        self.wait += 1
        if self.wait % 6 == 0:
            self.change_x -= 0.005

        if self.center_x <= 0 - SCREEN_WIDTH // 2:
            self.center_x = SCREEN_WIDTH + SCREEN_WIDTH // 2


class Cloud(Sprite):
    def __init__(self, image, road):
        super().__init__(image)
        self.center_y = random.randint(90, SCREEN_HEIGHT - 10)
        self.center_x = SCREEN_WIDTH + 30
        self.change_x = road.change_x + 1.7

    def update(self):
        super().update()
        if self.right < -20:
            self.喵()

class Dinosaur(Sprite):
    def __init__(self, image):
        super().__init__(image)
        self.center_x = 100
        self.center_y = 53

        self.append_texture(load_texture('images/小恐龙-下蹲1.png', mirrored=False, scale=1))
        self.append_texture(load_texture('images/小恐龙-下蹲2.png', mirrored=False, scale=1))
        self.append_texture(load_texture('images/小恐龙-跑动1.png', mirrored=False, scale=1))
        self.append_texture(load_texture('images/小恐龙-跑动2.png', mirrored=False, scale=1))
        self.append_texture(load_texture('images/小恐龙-瞪眼.png', mirrored=False, scale=1))
        self.JUMP = 0
        self.SQUAT1 = 1
        self.SQUAT2 = 2
        self.RUN1 = 3
        self.RUN2 = 4
        self.GAME_OVER = 5

        self.wait = 0
        self.run()

    def jump(self):
        if self.now == 'run' and self.center_y == 53:
            self.now = 'jump'
            self.change_y = 12.5
            self.set_texture(self.JUMP)
        else:
            return

    def squat(self):
        self.now = 'squat'
        self.now_cheak_texture1 = self.SQUAT1
        self.now_cheak_texture2 = self.SQUAT2

    def run(self):
        self.now = 'run'
        self.now_cheak_texture1 = self.RUN1
        self.now_cheak_texture2 = self.RUN2

    def update(self):
        super().update()
        self.wait += 1
        if self.now == 'run' or self.now == 'squat':
            if self.wait % 6 == 0:
                if self.textures.index(self.texture) == self.now_cheak_texture2:
                    self.set_texture(self.now_cheak_texture1)
                else:
                    self.set_texture(self.now_cheak_texture2)
        elif self.now == 'jump':
            self.change_y -= 0.7

            if self.top >= SCREEN_HEIGHT:
                self.change_y = -5

        if self.center_y <= 53:
            self.center_y = 53
            self.change_y = 0
            if self.now == 'squat':
                self.squat()
            else:
                self.run()
                
class Enemy(Sprite):
    def __init__(self, image, road):
        super().__init__(image)
        if image == 'images/小翼龙-飞行1.png':
            self.append_texture(load_texture('images/小翼龙-飞行2.png'))
            self.like = 'fly'
            self.FLY1 = 0
            self.FLY2 = 1
            self.center_y = random.randint(90, SCREEN_HEIGHT - 10)
        else:
            self.like = 'stay'
            self.center_y = 53

        self.center_x = SCREEN_WIDTH + 30
        self.change_x = road.change_x
        self.wait = 0
        
    def update(self):
        super().update()
        self.wait += 1
        if self.wait % 6 == 0:
            self.change_x -= 0.005
            if self.like == 'fly':
                if self.textures.index(self.texture) == self.FLY2:
                    self.set_texture(self.FLY1)
                else:
                    self.set_texture(self.FLY2)

        if self.right < -20:
            self.喵()


class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        # 窗体初始化
        self.setup()

    def setup(self):
        # 设置背景图片
        set_background_color(color.WHITE)
        self.road_1 = Road('images/沙漠地面.png')
        self.road_2 = Road('images/沙漠地面.png')
        self.road_2.center_x = SCREEN_WIDTH * 2

        self.dinosaur = Dinosaur('images/小恐龙-站立.png')
        self.enemys = SpriteList()
        self.clouds = SpriteList()

        self.game_now = 'wait'
        self.score = 0
        self.wait = 0
        self.last_creat_enemy = 0
        self.wait_time = random.choice([120, 150, 200, 225, 240])
        try:
            self.hight_score = self.load_score()
        except:
            self.dump_score()
        self.game_over = Sprite('images/重来.png')
        self.game_over.center_x = SCREEN_WIDTH // 2
        self.game_over.center_y = SCREEN_HEIGHT // 2 - 15

    def on_draw(self):
        start_render()
        self.road_1.draw()
        self.road_2.draw()
        self.clouds.draw()

        self.dinosaur.draw()
        self.enemys.draw()

        if self.game_now == 'gaming' or self.game_now == 'game_over':
            num = 5 - len(str(self.score))
            zero = num * '0'
            num = 5 - len(str(self.hight_score))
            hight_zero = num * '0'
            draw_text(f'HI {hight_zero}{self.hight_score}', SCREEN_WIDTH - 180, SCREEN_HEIGHT - 30, (100, 100, 100, 255), font_size=20)
            draw_text(f'{zero}{self.score}', SCREEN_WIDTH - 70, SCREEN_HEIGHT - 30, (55, 55, 55, 255), font_size=20)
        
        if self.game_now == 'wait':
            draw_rectangle_outline(self.dinosaur.center_x, self.dinosaur.center_y, self.dinosaur.width * 3.8, self.dinosaur.height * 4, color.WHITE, 116)
            
        if self.game_now == 'game_over':
            draw_text('GAME     OVER', self.game_over.center_x - 76, self.game_over.center_y + 30, (55, 55, 55, 255), font_size=20)
            self.game_over.draw()

    def on_update(self, delta_time):
        if self.game_now == 'wait':
            self.set_size(200, SCREEN_HEIGHT)

        if self.game_now == 'setup':
            self.dinosaur.update()
            self.wait += 15
            now_width = 200 + self.wait
            self.set_size(now_width, SCREEN_HEIGHT)
            if now_width >= SCREEN_WIDTH:
                self.set_size(SCREEN_WIDTH, SCREEN_HEIGHT)
                self.wait = 0
                self.game_now = 'gaming'

        elif self.game_now == 'gaming':
            self.wait += 1
            self.road_1.update()
            self.road_2.update()
            self.clouds.update()

            self.dinosaur.update()
            self.enemys.update()
            self.game_now = self.judje_hit(self.dinosaur, self.enemys)

            if self.wait % 6 == 0:
                self.score += 1

            if self.wait % 150 == 0:
                self.creat_cloud()

            if self.wait - self.last_creat_enemy >= self.wait_time:
                self.last_creat_enemy = self.wait
                self.wait_time = random.choice([120, 150, 200, 225, 240])
                self.creat_enemy()

        elif self.game_now == 'game_over':
            self.dinosaur.set_texture(self.dinosaur.GAME_OVER)

    def on_key_press(self, symbol, modifiers):
        if symbol == arcade.key.ESCAPE:
            sys.exit()
        if self.game_now == 'wait':
            if symbol == arcade.key.SPACE or symbol == arcade.key.UP or symbol == arcade.key.W:
                self.dinosaur.jump()
                self.game_now = 'setup'

        elif self.game_now == 'gaming':
            if symbol == arcade.key.SPACE or symbol == arcade.key.UP or symbol == arcade.key.W:
                self.dinosaur.jump()
            elif symbol == arcade.key.DOWN or symbol == arcade.key.S:
                if self.dinosaur.now == 'jump':
                    self.dinosaur.change_y = -10
                    self.dinosaur.now = 'squat'
                else:
                    if self.dinosaur.now == 'run' and self.dinosaur.center_y == 53:
                        self.dinosaur.squat()
                
        elif self.game_now == 'game_over':
            if symbol == arcade.key.SPACE or symbol == arcade.key.UP or symbol == arcade.key.W or symbol == arcade.key.P:
                self.setup()
                self.game_now = 'gaming'

    def on_key_release(self, symbol, modifiers):
        if symbol == arcade.key.DOWN or symbol == arcade.key.S:
            if self.dinosaur.center_y <= 53 and self.dinosaur.now == 'squat':
                self.dinosaur.run()

    def on_mouse_press(self, x, y, button, modifiers):
        if self.game_now == 'game_over':
            if x >= self.game_over.center_x - self.game_over.width // 2 and x <= self.game_over.center_x + self.game_over.width // 2:
                if y >= self.game_over.center_y - self.game_over.height // 2 and y <= self.game_over.center_y + self.game_over.height // 2:
                    self.setup()
                    self.game_now = 'gaming'

    def creat_enemy(self):
        texture_list = ['images/仙人掌1.png', 'images/仙人掌2.png', 'images/仙人掌3.png','images/仙人掌4.png', 'images/仙人掌5.png', 'images/仙人掌6.png']
        if self.score >= 400:
            texture_list.append('images/小翼龙-飞行1.png')
        texture = random.choice(texture_list)
        
        enemy = Enemy(texture, self.road_1)
        self.enemys.append(enemy)

    def creat_cloud(self):
        cloud = Cloud('images/云朵.png', self.road_1)
        self.clouds.append(cloud)

    def judje_hit(self, hit1, hit2):
        hit_list = check_for_collision_with_list(hit1, hit2)
        if hit_list:
            if self.score > self.hight_score:
                self.dump_score()
            return 'game_over'
        else:
            return 'gaming'

    def load_score(self):
        with open('hight_score', 'r')as f:
            down = json.load(f)
            return down['hight_score']

    def dump_score(self):
        score = {}
        score['hight_score'] = self.score
        with open('hight_score', 'w')as f:
            json.dump(score, f)

if __name__ == '__main__':
    game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    run()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
用户_869149用户_869149

NB啊NB

点赞0


评论


用户_869149用户_869149

沙发!

点赞0


评论


CHN_流星CHN_流星

pygame没必要三天

点赞0


评论


吉祥如意喵吉祥如意喵

为什么运行不了

 

点赞0


评论


_HHHHHHHHHH__HHHHHHHHHH_

我发现,这些发py作品的,都不发要导入的图片。

唉————

点赞0


评论