Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

用python写一个命令行版的flappy bird

  1. User

    用python写一个命令行版的flappy bird

  2. Assistant

    编写一个命令行版本的Flappy Bird游戏确实是一个有趣的项目。虽然命令行的功能有限,不能实现图形界面,但你可以用字符和简单逻辑来模拟游戏。下面是一个简化版的代码示例,利用 Python 中的 curses 库来进行绘制和处理键盘输入。

    首先,确保你已经安装了 curses 库,它在大多数 Unix 系统(例如 Linux 和 macOS)上是内置的。如果你在 Windows 上运行 Python,需要先安装 windows-curses 包:

    pip install windows-curses
    

    接下来是 flappy_bird.py 的完整代码示例:

    import curses
    from curses import wrapper
    import random
    
    def main(stdscr):
        # Initialize screen
        curses.curs_set(0)
        stdscr.nodelay(1)
        stdscr.timeout(100)
        
        sh, sw = stdscr.getmaxyx()
        w = curses.newwin(sh, sw, 0, 0)
        
        # Window boundaries
        w.border(0)
        
        # Initial position of the bird
        bird_x = sw // 5
        bird_y = sh // 2
        bird = [[bird_y, bird_x]]
        
        # Create obstacles (pipes)
        pipes = []
        pipe_width = 3
        pipe_gap = sh // 3
        update_pipe_time = 300
        
        # Bird velocity and acceleration
        bird_velocity = 0
        bird_acceleration = 1
        
        # Game loop
        score = 0
        while True:
            w.clear()
            w.border(0)
            
            # Draw bird
            for y, x in bird:
                w.addch(y, x, '0')
            
            # Add new pipe
            if score % update_pipe_time == 0:
                pipe_height = random.randint(pipe_gap // 5, sh // 2)
                pipes.extend([
                    [0, sw - 1],
                    [pipe_height, sw - 1],
                    [pipe_height + pipe_gap, sw - 1],
                    [sh - 1, sw - 1]
                ])
            
            # Move pipes and check for collision
            new_pipes = []
            for top, bottom, px in pipes:
                new_px = px - 1
                if new_px > 0:
                    new_pipes.append([top, bottom, new_px])
                    for y in range(top, bottom):
                        w.addch(y, new_px, '|')
                if bird_y == top or bird_y == bottom:  # Check collision with top or bottom
                    if bird_x == new_px:
                        break
            else:
                pipes = new_pipes
            
            # Handle input
            event = w.getch()
            if event == ord(' '):
                bird_velocity = -5
                
            # Apply gravity
            bird_velocity += bird_acceleration
            bird_y += bird_velocity
            
            if bird_y < 0 or bird_y >= sh:
                break
            
            # Update score
            score += 1
            w.addstr(0, 2, f'Score: {score}')
            
            # Refresh window
            w.refresh()
        
        w.addstr(sh // 2, sw // 2 - 5, 'Game Over')
        w.addstr(sh // 2 + 1, sw // 2 - 7, 'Press any key to exit')
        w.nodelay(0)
        w.getch()
    
    # Run the game
    wrapper(main)
    

    说明:

    1. 使用 curses 库来处理屏幕绘制和键盘输入。
    2. bird 使用一个坐标在屏幕上显示,并用 ' ' 键来控制鸟的上升。
    3. pipes 使用竖线 | 显示,并不断从右向左移动。
    4. 碰到顶部或底部边界则游戏结束。

    运行这段代码,使用空格键让鸟上升。你可以根据自己的需求修改参数,例如鸟的速度、管道的间隙等。

    记住,这只是一个简化的命令行版本的 Flappy Bird,没有实际的图形界面效果,但可以展示如何使用控制台来模拟这样一个游戏。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.