1027Colors in Mars (20)

本文介绍了一个将十进制颜色值转换为火星RGB颜色值的方法。火星RGB使用13进制表示颜色,通过一个C++程序实现转换过程,并展示了一个具体的输入输出示例。

题目描述

People in Mars represent the colors in their computers in a similar way as the Earth people.  That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the last 2 digits  for Blue.  The only difference is that they use radix 13 (0-9 and A-C) instead of 16.  Now given a color in three decimal numbers (each between 0 and 168), you are supposed to output their Mars RGB values.

 

输入描述:

Each input file contains one test case which occupies a line containing the three decimal color values.


 

输出描述:

For each test case you should output the Mars RGB value in the following format: first output "#", then followed by a 6-digit number where all the English characters must be upper-cased.  If a single color is only 1-digit long, you must print a "0" to the left.

 

输入例子:

15 43 71

 

输出例子:

#123456

 

 

#include <iostream>
using namespace std;

string convert(int x) {
    string y;
    string zero = "0";
    if (x < 10) {
        y = x + 48;
        y = zero + y;
        return y;
    }
    else if (x > 9 && x < 13) {
        y = x + 55;
        y = zero + y;
        return y;
    }
    else if (x / 13 < 10) {
        int a, b;
        a = x / 13 + 48;
        if (x % 13 < 10)
            b = x % 13 + 48;
        else
            b = x % 13 + 55;
        string t1, t2;
        t1 = a;
        t2 = b;
        y = t1 + t2;
        return y;
    }
    else {
        int a, b;
        a = x / 13 + 55;
        if (x % 13 < 10)
            b = x % 13 + 48;
        else
            b = x % 13 + 55;
        string t1, t2;
        t1 = a;
        t2 = b;
        y = t1 + t2;
        return y;
    }
}
int main() {
    int a, b, c;
    cin >> a >> b >> c;
    cout << "#";
    cout << convert(a) << convert(b) << convert(c);
    return 0;
}

import pygame import random import sys # 初始化Pygame pygame.init() # 屏幕设置 WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Aerospace Resource Collection and Obstacle Avoidance Game") # 颜色定义 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) # 字体设置 font = pygame.font.Font(None, 36) # 简单问答功能 - 修改为选择题格式 def ask_question(): question_templates = [ ("Which planet is known as the 'Red Planet'?", ["A. Earth", "B. Mars", "C. Venus"], "B"), ("What is the primary power source for the International Space Station?", ["A. Nuclear Energy", "B. Solar Energy", "C. Wind Energy"], "B") ] return random.choice(question_templates) def check_answer(user_input, correct_answer): return user_input.lower() == correct_answer.lower() # 玩家类 class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.Surface((50, 50)) self.image.fill(GREEN) self.rect = self.image.get_rect() self.rect.center = (WIDTH // 2, HEIGHT - 60) self.speed_x = 0 def update(self): keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.speed_x = -5 elif keys[pygame.K_RIGHT]: self.speed_x = 5 else: self.speed_x = 0 self.rect.x += self.speed_x if self.rect.left < 0: self.rect.left = 0 if self.rect.right > WIDTH: self.rect.right = WIDTH # 障碍物类 class Obstacle(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface((50, 50)) self.image.fill(RED) self.rect = self.image.get_rect() self.rect.topleft = (x, y) self.speed_y = random.randint(2, 5) def update(self): self.rect.y += self.speed_y if self.rect.top > HEIGHT: self.rect.bottom = 0 self.rect.x = random.randint(0, WIDTH - self.rect.width) # 资源类 class Resource(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface((30, 30)) self.image.fill(WHITE) self.rect = self.image.get_rect() self.rect.topleft = (x, y) self.speed_y = random.randint(2, 4) def update(self): self.rect.y += self.speed_y if self.rect.top > HEIGHT: self.rect.bottom = 0 self.rect.x = random.randint(0, WIDTH - self.rect.width) # 游戏主函数 def main(): clock = pygame.time.Clock() all_sprites = pygame.sprite.Group() obstacles = pygame.sprite.Group() resources = pygame.sprite.Group() player = Player() all_sprites.add(player) for _ in range(5): obstacle = Obstacle(random.randint(0, WIDTH - 50), random.randint(-200, -50)) all_sprites.add(obstacle) obstacles.add(obstacle) for _ in range(3): resource = Resource(random.randint(0, WIDTH - 30), random.randint(-200, -50)) all_sprites.add(resource) resources.add(resource) score = 0 game_state = "playing" collision_occurred = False question = "" options = [] correct_answer = "" user_answer = "" while True: screen.fill(BLACK) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if game_state == "playing": if collision_occurred: if event.type == pygame.KEYDOWN: if event.key == pygame.K_a or event.key == pygame.K_b or event.key == pygame.K_c: user_answer = event.unicode.upper() # 获取用户输入的选项字母 if check_answer(user_answer, correct_answer): collision_occurred = False user_answer = "" else: game_state = "game_over" if game_state == "playing": if not collision_occurred: all_sprites.update() hits = pygame.sprite.spritecollide(player, resources, True) for hit in hits: score += 10 new_resource = Resource(random.randint(0, WIDTH - 30), random.randint(-200, -50)) all_sprites.add(new_resource) resources.add(new_resource) if pygame.sprite.spritecollideany(player, obstacles): collision_occurred = True question, options, correct_answer = ask_question() all_sprites.draw(screen) score_text = font.render(f"Score: {score}", True, WHITE) screen.blit(score_text, (10, 10)) if collision_occurred: question_text = font.render(question, True, WHITE) screen.blit(question_text, (10, 50)) for idx, option in enumerate(options, start=1): option_text = font.render(option, True, WHITE) screen.blit(option_text, (10, 90 + idx * 40)) elif game_state == "game_over": game_over_text = font.render("Game Over - Press SPACE to Restart", True, WHITE) screen.blit(game_over_text, (WIDTH // 4, HEIGHT // 2)) keys = pygame.key.get_pressed() if keys[pygame.K_SPACE]: game_state = "playing" score = 0 all_sprites.empty() obstacles.empty() resources.empty() player = Player() all_sprites.add(player) for _ in range(5): obstacle = Obstacle(random.randint(0, WIDTH - 50), random.randint(-200, -50)) all_sprites.add(obstacle) obstacles.add(obstacle) for _ in range(3): resource = Resource(random.randint(0, WIDTH - 30), random.randint(-200, -50)) all_sprites.add(resource) resources.add(resource) pygame.display.flip() clock.tick(60) if __name__ == "__main__": main()修改代码,使得问题回答正确后之前触碰到的障碍物消失
最新发布
06-04
### 实现用户回答问题正确后移除障碍物的逻辑 在 Pygame 中实现这一功能,需要将障碍物存储为一个列表或字典,并在用户回答问题正确时从该列表中移除对应的障碍物。以下是完整的实现思路和代码示例: #### 1. 障碍物管理 首先,定义一个列表 `obstacles` 来存储所有障碍物的位置。每个障碍物可以是一个元组 `(x, y)` 表示其坐标。 ```python obstacles = [(100, 150), (200, 250), (300, 350)] # 示例障碍物位置 ``` #### 2. 检测碰撞 在主循环中,检测玩家是否与某个障碍物发生碰撞。如果发生碰撞,则记录该障碍物的位置,并提示用户回答问题。 ```python def check_collision(player_pos, obstacles): for obstacle in obstacles: if abs(player_pos[0] - obstacle[0]) < SNAKE_WIDTH and abs(player_pos[1] - obstacle[1]) < SNAKE_HEIGHT: return obstacle # 返回碰撞的障碍物 return None ``` #### 3. 提问与验证答案 当检测到碰撞时,暂停游戏并提示用户回答问题。如果答案正确,则从 `obstacles` 列表中移除该障碍物。 ```python def ask_question(obstacle): question = "请输入正确的答案以移除障碍物:" # 示例问题 answer = input(question) if answer == "正确答案": # 假设正确答案为"正确答案" return True return False ``` #### 4. 更新障碍物列表 如果用户回答正确,则从 `obstacles` 列表中移除对应的障碍物。 ```python def remove_obstacle(obstacle, obstacles): if obstacle in obstacles: obstacles.remove(obstacle) # 移除障碍物 ``` #### 5. 主循环整合 将上述逻辑整合到主循环中,确保在每次更新帧时检测碰撞并处理障碍物移除。 ```python while True: player_pos = snake_list[0] # 假设蛇头为玩家位置 collided_obstacle = check_collision(player_pos, obstacles) if collided_obstacle: if ask_question(collided_obstacle): # 如果回答正确 remove_obstacle(collided_obstacle, obstacles) # 移除障碍物 # 绘制障碍物 for obstacle in obstacles: pygame.draw.rect(screen, COLORS['darkblue'], (obstacle[0], obstacle[1], OBSTACLE_WIDTH, OBSTACLE_HEIGHT)) pygame.display.flip() clock.tick(FPS) ``` #### 注意事项 - 确保在 `ask_question` 函数中暂停游戏,直到用户输入答案。 - 使用 `pygame.event.get()` 监听键盘事件[^1],以便用户可以通过键盘输入答案。 - 在绘制障碍物时,确保使用最新的 `obstacles` 列表[^3]。 ### 示例代码 以下是一个完整的代码示例: ```python import pygame import sys # 初始化 pygame.init() screen = pygame.display.set_mode((400, 300)) pygame.display.set_caption('Zombie VS Plants') clock = pygame.FPS = 30 COLORS = {'darkred': (139, 0, 0), 'darkblue': (0, 0, 139)} SNAKE_WIDTH = 20 SNAKE_HEIGHT = 20 OBSTACLE_WIDTH = 20 OBSTACLE_HEIGHT = 20 # 障碍物列表 obstacles = [(100, 150), (200, 250), (300, 350)] # 碰撞检测 def check_collision(player_pos, obstacles): for obstacle in obstacles: if abs(player_pos[0] - obstacle[0]) < SNAKE_WIDTH and abs(player_pos[1] - obstacle[1]) < SNAKE_HEIGHT: return obstacle return None # 提问 def ask_question(obstacle): question = "请输入正确的答案以移除障碍物:" answer = input(question) if answer == "正确答案": return True return False # 移除障碍物 def remove_obstacle(obstacle, obstacles): if obstacle in obstacles: obstacles.remove(obstacle) # 主循环 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # 假设蛇头为玩家位置 player_pos = (200, 200) # 示例玩家位置 collided_obstacle = check_collision(player_pos, obstacles) if collided_obstacle: if ask_question(collided_obstacle): remove_obstacle(collided_obstacle, obstacles) # 绘制障碍物 screen.fill((255, 255, 255)) for obstacle in obstacles: pygame.draw.rect(screen, COLORS['darkblue'], (obstacle[0], obstacle[1], OBSTACLE_WIDTH, OBSTACLE_HEIGHT)) pygame.display.flip() clock.tick(30) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值