自己做的一个小游戏(1)--吃金币(基础版)



效果图

1240963-20180912181433987-54139558.png

代码

#include<stdio.h>
#include<conio.h>
#include<Windows.h>
#include<time.h>//计时器
#include<math.h>
#include<stdlib.h>

//跳转函数
void turnto(int x,int y)
{
    COORD loc;
    loc.X = x;
    loc.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), loc);
}

//打印+清除
void clean(int x,int y)
{
    printf("I");
    turnto(x, y);
    printf(" ");
}

//随机数
int unknow(int x, int y)
{
    int i;
    i = (rand() * (y - x) / RAND_MAX + x);
    return i;
}

//o-(-.-)-I开始
int main(void)
{
    clock_t start, end;
    int t;
    int x, y, i, a, b;
    int score=0;
    char name[10];
    long coin[10];


    //输入名字
    printf("本游戏需要英文输入法");
    Sleep(1500);
    turnto(0, 0);
    printf("请输入你的英文名字:_______\b\b\b\b\b\b\b");
    scanf("%s", &name);
    printf("w向上,s向下,a向左,d向右,q to quit");
    
    //初始化
    
    x = 20;
    y = 13;
    turnto(x, y);

    printf("I");

    //coin创建
    srand((unsigned int)time(0));//不要把srand与rand放一个循环
    for (i = 0; i <= 9; i++)
    {
        a = unknow(2, 117);
        b = unknow(3, 27);
        turnto(a, b);
        printf("$");
        coin[i] = a * 100 + b;
    }
    
    //计时开始
    start = clock();

    //移动
    while((i=_getch())!=0)//getch也行?
    {
        if (i == (int)'w')
        {
            turnto(x, y - 1);
            clean(x, y);
            turnto(x, y - 1);
            y = y - 1;
        }
        else if(i==(int)'s')
        {
            turnto(x, y + 1);
            clean(x, y);
            turnto(x, y + 1);
            y = y +1;
        }
        else if (i == (int)'a')
        {
            turnto(x-1, y );
            clean(x, y);
            turnto(x-1, y );
            x = x - 1;
        }
        else if (i == (int)'d')
        {
            turnto(x + 1, y);
            clean(x, y);
            turnto(x + 1, y);
            x = x + 1;
        }
        else if (i == (int)'q')
        {
            printf("你选择退出");
            Sleep(1000);
            return 0;
        }
        //遍历
        for (i = 0; i <= sizeof(coin) / sizeof(long); i++)
        {
            if (x * 100 + y == coin[i])
            {
                coin[i] = 100000;
                score++;
                turnto(0, 1);
                printf("得分:%d                    ",score);
                turnto(x, y);
            }
        }
        if (score == 10)
        {
            break;
        }
    }

    //计时结束
    end = clock();
    t = (end - start) / CLOCKS_PER_SEC;
    turnto(26, 1);
    printf("  用时: %d 秒     \n ", t);
    Sleep(1000);
    printf("%s,你的综合得分为%d",name,110-t);
    Sleep(1000);
    return 0;
}

ps:这是我上大学前几天看了printf()与scanf()之后做出来的游戏;其中随机数与位置跳转函数都是上网查的。所以说编程很有意思,它让你创造而不是让你接受。

转载于:https://www.cnblogs.com/juicebox/p/9636442.html

好的,下面是一个简单的“金币小游戏的代码: ```python import pygame import random # 初始化游戏 pygame.init() # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # 设置屏幕大小 SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) # 设置游戏标题 pygame.display.set_caption("金币小游戏") # 定义金币类 class Coin(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.Surface([10, 10]) self.image.fill(GOLD) self.rect = self.image.get_rect() def update(self): self.rect.y += 1 if self.rect.y > SCREEN_HEIGHT: self.rect.y = 0 self.rect.x = random.randrange(SCREEN_WIDTH) # 定义玩家类 class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.Surface([20, 20]) self.image.fill(BLUE) self.rect = self.image.get_rect() def update(self): # 获取键盘输入 keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.rect.x -= 5 elif keys[pygame.K_RIGHT]: self.rect.x += 5 elif keys[pygame.K_UP]: self.rect.y -= 5 elif keys[pygame.K_DOWN]: self.rect.y += 5 # 碰撞检测 coins_hit = pygame.sprite.spritecollide(self, coins_list, True) for coin in coins_hit: self.score += 1 # 创建精灵组 all_sprites_list = pygame.sprite.Group() coins_list = pygame.sprite.Group() # 创建玩家 player = Player() player.rect.x = SCREEN_WIDTH // 2 player.rect.y = SCREEN_HEIGHT - 50 player.score = 0 # 添加玩家到精灵组 all_sprites_list.add(player) # 添加金币到精灵组 for i in range(50): coin = Coin() coin.rect.x = random.randrange(SCREEN_WIDTH) coin.rect.y = random.randrange(SCREEN_HEIGHT) coins_list.add(coin) all_sprites_list.add(coin) # 设置游戏时钟 clock = pygame.time.Clock() # 游戏循环 done = False while not done: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # 更新精灵组 all_sprites_list.update() # 绘制背景 screen.fill(WHITE) # 绘制精灵组 all_sprites_list.draw(screen) # 绘制分数 font = pygame.font.Font(None, 36) text = font.render("Score: " + str(player.score), True, BLACK) screen.blit(text, [10, 10]) # 更新屏幕 pygame.display.flip() # 设置游戏帧率 clock.tick(60) # 退出游戏 pygame.quit() ``` 通过运行以上代码,即可启动“金币小游戏。在游戏中,玩家需要通过键盘控制角色移动,到尽可能多的金币游戏结束后,会显示玩家的得分。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值