C++写笨鸟先飞(较难)

描述

输入描述

输出描述

用例输入 1 

2
1
2

用例输出 1 

1
4

具体代码:

#include <iostream>
using namespace std;
int main(){
	int n;
    cin>>n;
    int x;
    for(int i=1;i<=n;i++){
        cin>>x;
        int v=1;
        int sum=0;
        for(int j=1;j<=x;j++){
            sum+=v;
            v+=2;
        }
        cout<<sum<<endl;
    }
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是用Python实现一个简单的笨鸟先飞游戏的代码: ```python import pygame import random # 初始化 pygame.init() # 设置屏幕大小 screen_width = 288 screen_height = 512 screen = pygame.display.set_mode((screen_width, screen_height)) # 设置标题和图标 pygame.display.set_caption("Flappy Bird") icon = pygame.image.load("icon.png") pygame.display.set_icon(icon) # 加载背景图片和音效 background = pygame.image.load("background.png") bird_upflap = pygame.image.load("bird_upflap.png") bird_midflap = pygame.image.load("bird_midflap.png") bird_downflap = pygame.image.load("bird_downflap.png") pipe_up = pygame.image.load("pipe_up.png") pipe_down = pygame.image.load("pipe_down.png") hit_sound = pygame.mixer.Sound("hit.wav") wing_sound = pygame.mixer.Sound("wing.wav") score_sound = pygame.mixer.Sound("point.wav") # 设置字体 font = pygame.font.Font("04B_19.ttf", 40) # 定义函数:绘制分数 def draw_score(score): score_text = font.render(str(score), True, (255, 255, 255)) screen.blit(score_text, (screen_width//2 - score_text.get_width()//2, 50)) # 定义类:鸟 class Bird: def __init__(self): self.x = 50 self.y = screen_height//2 self.image = bird_midflap self.velocity = 0 self.gravity = 1 self.lift = -10 self.score = 0 self.is_dead = False def flap(self): if not self.is_dead: wing_sound.play() self.velocity += self.lift def update(self): if not self.is_dead: self.velocity += self.gravity self.y += self.velocity if self.velocity > 10: self.velocity = 10 if self.y < 0: self.y = 0 self.velocity = 0 if self.y > screen_height - 50: self.y = screen_height - 50 self.velocity = 0 if self.velocity < 0: self.image = bird_upflap elif self.velocity > 0: self.image = bird_downflap else: self.image = bird_midflap def draw(self): screen.blit(self.image, (self.x, self.y)) # 定义类:管道 class Pipe: def __init__(self): self.x = screen_width + 50 self.y = random.randint(100, screen_height - 250) self.width = 52 self.height = 320 self.speed = 4 self.is_passed = False def update(self): self.x -= self.speed def draw(self): screen.blit(pipe_up, (self.x, self.y - self.height)) screen.blit(pipe_down, (self.x, self.y + 100)) def is_collided(self, bird): if bird.x + 34 > self.x and bird.x < self.x + self.width: if bird.y < self.y - self.height + 25 or bird.y + 24 > self.y + 75: return True return False # 创建对象 bird = Bird() pipes = [Pipe()] # 游戏主循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: bird.flap() # 绘制背景 screen.blit(background, (0, 0)) # 更新鸟的位置和状态 bird.update() # 绘制鸟 bird.draw() # 更新管道的位置 for pipe in pipes: pipe.update() # 检测碰撞 if pipe.is_collided(bird): hit_sound.play() bird.is_dead = True # 绘制管道 pipe.draw() # 计分 if not pipe.is_passed and pipe.x < bird.x: pipe.is_passed = True bird.score += 1 score_sound.play() # 添加新管道 if pipe.x < -pipe.width: pipes.append(Pipe()) # 删除离开屏幕的管道 if pipes[0].x < -pipes[0].width: pipes.pop(0) # 绘制分数 draw_score(bird.score) # 刷新屏幕 pygame.display.update() # 退出游戏 pygame.quit() ``` 需要注意的几点: - 游戏中使用了一些图片和音效,需要提前准备好。 - 游戏中的鸟和管道都是由类来表示的,方便管理和操作。 - 游戏中的管道是动态生成的,随着时间的推移不断向左移动,同时也不断生成新的管道,从而实现无限循环。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值