Я создал программу-змейку на C++, используя библиотеку raylib. Моя игра полностью зависает через определенное время (несколько минут).
Вот полный код моей программы:
#include
#include "raylib.h"
#include "raymath.h"
#include
using namespace std;
// Constant Values
const int cell_size = 40; // Cell size has to be equal to assets dimension in pixels
const int cell_count_x = 20, cell_count_y = 15;
const int game_width = cell_size * cell_count_x;
const int game_height = cell_size * cell_count_y;
const int FPS = 60;
const int offset = cell_size * 2;
const int font_size = 40;
// Classes
class Food {
public:
Vector2 position;
Texture texture;
Food(deque snake_body) {
Image image = LoadImage("Graphics/apple.png");
texture = LoadTextureFromImage(image);
UnloadImage(image);
position = GetRandomPos(snake_body);
}
~Food() {
UnloadTexture(texture);
}
void Draw() {
DrawTexture(texture, offset + position.x * cell_size, offset + position.y * cell_size, WHITE);
}
Vector2 GetRandomPos(deque snake_body) {
float x = GetRandomValue(0, cell_count_x - 1);
float y = GetRandomValue(0, cell_count_y - 1);
Vector2 position = { x, y };
while (CheckPos(snake_body, position) == false) {
float x = GetRandomValue(0, cell_count_x - 1);
float y = GetRandomValue(0, cell_count_y - 1);
Vector2 position = { x, y };
}
return position;
}
bool CheckPos(deque snake_body, Vector2 position) {
for (int i = 0; i < snake_body.size(); i++)
if (Vector2Equals(snake_body, position))
return false;
return true;
}
};
class Snake {
public:
deque body = { Vector2{cell_count_x / 3, cell_count_y / 3}, Vector2{cell_count_x / 3 - 1, cell_count_y / 3} , Vector2{cell_count_x / 3 - 1, cell_count_y / 3} };
Vector2 direction = { 1, 0 };
bool fruit_collision = false;
void Draw() {
for (int i = 0; i < body.size(); i++) {
float x = body.x;
float y = body.y;
Rectangle body_part = Rectangle{ offset + x * cell_size, offset + y * cell_size, (float)cell_size, (float)cell_size };
DrawRectangleRounded(body_part, 0.5, 10, DARKGREEN);
}
}
void UpdateMovement() {
if (fruit_collision)
fruit_collision = false;
else
body.pop_back();
body.push_front(Vector2Add(body[0], direction));
}
void UpdateDirection() {
if (IsKeyPressed(KEY_UP) && direction.y != 1)
direction = { 0, -1 };
if (IsKeyPressed(KEY_DOWN) && direction.y != -1)
direction = { 0, 1 };
if (IsKeyPressed(KEY_LEFT) && direction.x != 1)
direction = { -1, 0 };
if (IsKeyPressed(KEY_RIGHT) && direction.x != -1)
direction = { 1, 0 };
}
void Reset() {
body = { Vector2{cell_count_x / 3, cell_count_y / 3}, Vector2{cell_count_x / 3 - 1, cell_count_y / 3} , Vector2{cell_count_x / 3 - 1, cell_count_y / 3} };
direction = { 1, 0 };
}
};
class Game {
public:
Snake snake = Snake();
Food food = Food(snake.body);
double last_update_time = 0;
int score = 0, max_score = 0;
Sound food_sound, death_sound;
Game() {
InitAudioDevice();
food_sound = LoadSound("Sounds/food_sound.mp3");
death_sound = LoadSound("Sounds/death_sound.mp3");
}
~Game() {
UnloadSound(food_sound);
UnloadSound(death_sound);
CloseAudioDevice();
}
void Draw() {
food.Draw();
snake.Draw();
}
void DisplayText() {
DrawText("Snake Game", offset, offset / 2, font_size, DARKGREEN);
DrawText("Score: ", offset, offset + game_height + 5, font_size, DARKGREEN);
DrawText(TextFormat("%i", score), offset + 150, offset + game_height + 5, font_size, DARKGREEN);
DrawText("Max Score: ", offset + game_width - 260, offset + game_height + 5, font_size, DARKGREEN);
DrawText(TextFormat("%i", max_score), offset + game_width - 15, offset + game_height + 5, font_size, DARKGREEN);
}
void Update() {
snake.UpdateDirection();
if (EventTriggered(0.2))
snake.UpdateMovement();
CheckCollisionWithFood();
CheckCollisionWithEdge();
CheckCollisionWithBody();
}
void CheckCollisionWithFood() {
if (Vector2Equals(snake.body[0], food.position)) {
food.position = food.GetRandomPos(snake.body);
snake.fruit_collision = true;
score++;
PlaySound(food_sound);
}
}
void CheckCollisionWithEdge() {
if (snake.body[0].x >= cell_count_x || snake.body[0].x == -1 || snake.body[0].y >= cell_count_y || snake.body[0].y == -1)
GameOver();
}
void CheckCollisionWithBody() {
for (int i = 2; i < snake.body.size(); i++)
if (Vector2Equals(snake.body[0], snake.body))
GameOver();
}
void GameOver() {
snake.Reset();
food.position = food.GetRandomPos(snake.body);
if (score > max_score)
max_score = score;
score = 0;
PlaySound(death_sound);
}
bool EventTriggered(double interval) {
double current_time = GetTime();
if (current_time - last_update_time >= interval) {
last_update_time = current_time;
return true;
}
return false;
}
};
int main()
{
InitWindow(game_width + offset * 2, game_height + offset * 2, "Snake");
SetTargetFPS(FPS);
// Game Class
Game game = Game();
// Main Loop
while (WindowShouldClose() == false) {
BeginDrawing();
// Update
game.Update();
// Draw
ClearBackground(GREEN);
DrawRectangleLinesEx(Rectangle{ offset - 5, offset - 5, cell_size * cell_count_x + 10, cell_size * cell_count_y + 10 }, 5, DARKGREEN);
game.Draw();
game.DisplayText();
EndDrawing();
}
CloseWindow();
return 0;
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... nt-of-time
Raylib C++ перестает работать через определенное время ⇐ C++
Программы на C++. Форум разработчиков
-
Anonymous
1730792958
Anonymous
Я создал программу-змейку на C++, используя библиотеку raylib. Моя игра полностью зависает через определенное время (несколько минут).
Вот полный код моей программы:
#include
#include "raylib.h"
#include "raymath.h"
#include
using namespace std;
// Constant Values
const int cell_size = 40; // Cell size has to be equal to assets dimension in pixels
const int cell_count_x = 20, cell_count_y = 15;
const int game_width = cell_size * cell_count_x;
const int game_height = cell_size * cell_count_y;
const int FPS = 60;
const int offset = cell_size * 2;
const int font_size = 40;
// Classes
class Food {
public:
Vector2 position;
Texture texture;
Food(deque snake_body) {
Image image = LoadImage("Graphics/apple.png");
texture = LoadTextureFromImage(image);
UnloadImage(image);
position = GetRandomPos(snake_body);
}
~Food() {
UnloadTexture(texture);
}
void Draw() {
DrawTexture(texture, offset + position.x * cell_size, offset + position.y * cell_size, WHITE);
}
Vector2 GetRandomPos(deque snake_body) {
float x = GetRandomValue(0, cell_count_x - 1);
float y = GetRandomValue(0, cell_count_y - 1);
Vector2 position = { x, y };
while (CheckPos(snake_body, position) == false) {
float x = GetRandomValue(0, cell_count_x - 1);
float y = GetRandomValue(0, cell_count_y - 1);
Vector2 position = { x, y };
}
return position;
}
bool CheckPos(deque snake_body, Vector2 position) {
for (int i = 0; i < snake_body.size(); i++)
if (Vector2Equals(snake_body[i], position))
return false;
return true;
}
};
class Snake {
public:
deque body = { Vector2{cell_count_x / 3, cell_count_y / 3}, Vector2{cell_count_x / 3 - 1, cell_count_y / 3} , Vector2{cell_count_x / 3 - 1, cell_count_y / 3} };
Vector2 direction = { 1, 0 };
bool fruit_collision = false;
void Draw() {
for (int i = 0; i < body.size(); i++) {
float x = body[i].x;
float y = body[i].y;
Rectangle body_part = Rectangle{ offset + x * cell_size, offset + y * cell_size, (float)cell_size, (float)cell_size };
DrawRectangleRounded(body_part, 0.5, 10, DARKGREEN);
}
}
void UpdateMovement() {
if (fruit_collision)
fruit_collision = false;
else
body.pop_back();
body.push_front(Vector2Add(body[0], direction));
}
void UpdateDirection() {
if (IsKeyPressed(KEY_UP) && direction.y != 1)
direction = { 0, -1 };
if (IsKeyPressed(KEY_DOWN) && direction.y != -1)
direction = { 0, 1 };
if (IsKeyPressed(KEY_LEFT) && direction.x != 1)
direction = { -1, 0 };
if (IsKeyPressed(KEY_RIGHT) && direction.x != -1)
direction = { 1, 0 };
}
void Reset() {
body = { Vector2{cell_count_x / 3, cell_count_y / 3}, Vector2{cell_count_x / 3 - 1, cell_count_y / 3} , Vector2{cell_count_x / 3 - 1, cell_count_y / 3} };
direction = { 1, 0 };
}
};
class Game {
public:
Snake snake = Snake();
Food food = Food(snake.body);
double last_update_time = 0;
int score = 0, max_score = 0;
Sound food_sound, death_sound;
Game() {
InitAudioDevice();
food_sound = LoadSound("Sounds/food_sound.mp3");
death_sound = LoadSound("Sounds/death_sound.mp3");
}
~Game() {
UnloadSound(food_sound);
UnloadSound(death_sound);
CloseAudioDevice();
}
void Draw() {
food.Draw();
snake.Draw();
}
void DisplayText() {
DrawText("Snake Game", offset, offset / 2, font_size, DARKGREEN);
DrawText("Score: ", offset, offset + game_height + 5, font_size, DARKGREEN);
DrawText(TextFormat("%i", score), offset + 150, offset + game_height + 5, font_size, DARKGREEN);
DrawText("Max Score: ", offset + game_width - 260, offset + game_height + 5, font_size, DARKGREEN);
DrawText(TextFormat("%i", max_score), offset + game_width - 15, offset + game_height + 5, font_size, DARKGREEN);
}
void Update() {
snake.UpdateDirection();
if (EventTriggered(0.2))
snake.UpdateMovement();
CheckCollisionWithFood();
CheckCollisionWithEdge();
CheckCollisionWithBody();
}
void CheckCollisionWithFood() {
if (Vector2Equals(snake.body[0], food.position)) {
food.position = food.GetRandomPos(snake.body);
snake.fruit_collision = true;
score++;
PlaySound(food_sound);
}
}
void CheckCollisionWithEdge() {
if (snake.body[0].x >= cell_count_x || snake.body[0].x == -1 || snake.body[0].y >= cell_count_y || snake.body[0].y == -1)
GameOver();
}
void CheckCollisionWithBody() {
for (int i = 2; i < snake.body.size(); i++)
if (Vector2Equals(snake.body[0], snake.body[i]))
GameOver();
}
void GameOver() {
snake.Reset();
food.position = food.GetRandomPos(snake.body);
if (score > max_score)
max_score = score;
score = 0;
PlaySound(death_sound);
}
bool EventTriggered(double interval) {
double current_time = GetTime();
if (current_time - last_update_time >= interval) {
last_update_time = current_time;
return true;
}
return false;
}
};
int main()
{
InitWindow(game_width + offset * 2, game_height + offset * 2, "Snake");
SetTargetFPS(FPS);
// Game Class
Game game = Game();
// Main Loop
while (WindowShouldClose() == false) {
BeginDrawing();
// Update
game.Update();
// Draw
ClearBackground(GREEN);
DrawRectangleLinesEx(Rectangle{ offset - 5, offset - 5, cell_size * cell_count_x + 10, cell_size * cell_count_y + 10 }, 5, DARKGREEN);
game.Draw();
game.DisplayText();
EndDrawing();
}
CloseWindow();
return 0;
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79157152/raylib-c-stops-working-after-a-certain-amount-of-time[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия