Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
aostap
/
ussimang
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Merge Requests
0
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e9fba8a4
authored
Mar 20, 2023
by
caraun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parent
05d21e0c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
291 additions
and
0 deletions
Üks variant
Üks variant
0 → 100644
View file @
e9fba8a4
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <gtk/gtk.h>
#define ROWS 30
#define COLUMNS 30
#define CELL_SIZE 20//ruudustiku jaoks/liikumise jaoks/positsiooni jaoks
typedef struct {
int x;
int y;
} Position;//algpositsioon
typedef enum {//typedef enum on selleks, et konstandid defineerida
UP,
DOWN,
LEFT,
RIGHT
} Direction;//noolte jaoks
typedef struct {
int score;
char name[20];
} Score;//skoor
typedef struct {
int length;
Position positions[ROWS*COLUMNS];//positsiooni määramiseks ruudustikul
Direction direction;
Position food;
Score scores[3];
int num_scores;
} Game;//terve mängujaoks andmetüübid, mis eksisteerivad koos
Game game;//struct, mille andmeid me kasutame
void initialize_game() {//algväärtustamine mängu seadistuseks
game.length = 1;//ussi pikkus, algab 1 lülist, jupist, tükist
game.positions[0].x = ROWS/2;//alustab keskelt
game.positions[0].y = COLUMNS/2;//alustab keskelt
game.direction = RIGHT;//alustab liikumist paremale
srand(time(NULL));
game.food.x = rand() % ROWS;//toidu genereerimine random kohta x
game.food.y = rand() % COLUMNS;//toidu genereerimine random kohta y
game.score = 0;
game.num_scores = 0;//mitu skoori salvetab ja näitab meile failis(3)
}
gboolean draw_callback(GtkWidget *widget, cairo_t *cr, gpointer data) {
int i;
cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);//selleks, et joonistada cairo ja gtk abil taust(väljak, siin on tumehall taust praegu, saab muuta)
cairo_rectangle(cr, 0, 0, ROWS*CELL_SIZE, COLUMNS*CELL_SIZE);
cairo_fill(cr);
cairo_set_source_rgb(cr, 1, 1, 1);//ussi joonistamine(praegu siin valge värv automaatselt)
for (i = 0; i < game.length; i++) {
cairo_rectangle(cr, game.positions[i].x*CELL_SIZE, game.positions[i].y*CELL_SIZE, CELL_SIZE, CELL_SIZE);
cairo_fill(cr);
}
//Toidu joonistamine
cairo_set_source_rgb(cr, 1, 0, 0.1);//punane õun
cairo_rectangle(cr, game.food.x*CELL_SIZE, game.food.y*CELL_SIZE, CELL_SIZE, CELL_SIZE);
cairo_fill(cr);
//Skoori kuvamine
cairo_select_font_face(cr, "Arial", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, 20);
char score_text[20];
sprintf(score_text, "Score: %d", game.score);
cairo_set_source_rgb(cr, 1, 1, 1);//(valge)
cairo_move_to(cr, 10, 20);
cairo_show_text(cr, score_text);
return TRUE;
}
gboolean tick_callback(gpointer data) {
int i;
//ussi liikumine
for (i = game.length - 1; i > 0; i--) {
game.positions[i] = game.positions[i-1];
}
switch (game.direction) {
case UP:
game.positions[0].y--;//mööda y telge üles
break;
case DOWN:
game.positions[0].y++;//mööda y telge alla
break;
case LEFT:
game.positions[0].x--;//mööda x telge vasakule
break;
case RIGHT:
game.positions[0].x++;//mööda x telge paremale
break;
}
//Selleks, et vaadata, kas uss sõitis seina või enda otsa
if (game.positions[0].x < 0 || game.positions[0].x >= ROWS || game.positions[0].y < 0 || game.positions[0].y >= COLUMNS) {
printf("Game over!\n");
return FALSE;
}
for (i = 1; i < game.length; i++) {
if (game.positions[0].x == game.positions[i].x && game.positions[0].y == game.positions[i].y) {
printf("Game over!\n");
return FALSE;
}
}
//Selleks, et vaadata, kas uss sõi õuna/toidu ära
if (game.positions[0].x == game.food.x && game.positions[0].y == game.food.y) {
game.score++;//skoor kasvab ühe võrra
game.length++;//uss läheb pikemaks ühe võrra
game.positions[game.length-1] = game.positions[game.length-2];
switch (game.direction) {//nooled typedef
case UP:
game.positions[game.length-1].y++;
break;
case DOWN:
game.positions[game.length-1].y--;
break;
case LEFT:
game.positions[game.length-1].x++;
break;
case RIGHT:
game.positions[game.length-1].x--;
break;
}
game.food.x = rand() % ROWS;//toit muudab koguaeg asukohta(x)
game.food.y = rand() % COLUMNS;
}
gtk_widget_queue_draw(GTK_WIDGET(data));//ma ei oska seda eesti keeles kirjeldada lühidalt
return TRUE;
}
void save_score() {
if (game.num_scores < 3) {//3 parimat? tulemust
game.scores[game.num_scores].score = game.score;
printf("Enter your name: ");
scanf("%s", game.scores[game.num_scores].name);
game.num_scores++;
}
else {
int i;
int min_score = game.scores[0].score;
int min_index = 0;
for (i = 1; i < 3; i++) {
if (game.scores[i].score < min_score) {
min_score = game.scores[i].score;//uus väikseim skoor
min_index = i;
}
}
if (game.score > min_score){
game.scores[min_index].score = game.score;
printf("Enter your name: ");
scanf("%s", game.scores[min_index].name);
}
}
FILE *fp = fopen("scores.txt", "w");
int i;
for (i = 0; i < game.num_scores; i++) {
fprintf(fp, "%s: %d\n", game.scores[i].name, game.scores[i].score);//kirjuta skoorid faili
}
fclose(fp);
}
void on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data){
switch (event->keyval) {//sündmus vastavalt noole vajutusele
case GDK_KEY_Up:
if (game.direction != DOWN)
game.direction = UP;
break;
case GDK_KEY_Down:
if (game.direction != UP)
game.direction = DOWN;
break;
case GDK_KEY_Left:
if (game.direction != RIGHT)
game.direction = LEFT;
break;
case GDK_KEY_Right:
if (game.direction != LEFT)
game.direction = RIGHT;
break;
}
}
void on_new_game_activate(GtkMenuItem *menuitem, gpointer data){//mängu alustamine
initialize_game();
GtkWidget game_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);//uus aken avaneb kasutaja jaoks
gtk_window_set_title(GTK_WINDOW(game_window), "Snake Game");
gtk_window_set_default_size(GTK_WINDOW(game_window), ROWSCELL_SIZE, COLUMNS*CELL_SIZE);
gtk_window_set_position(GTK_WINDOW(game_window), GTK_WIN_POS_CENTER);
g_signal_connect(game_window, "destroy", G_CALLBACK(gtk_main_quit), NULL);//aken hävitatakse ja avaneb mänguväljak
g_signal_connect(game_window, "key_press_event", G_CALLBACK(on_key_press), NULL);
GtkWidget *drawing_area = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(game_window), drawing_area);//joonistamise ala gridi jaoks
gtk_widget_add_events(game_window, GDK_KEY_PRESS_MASK);
g_signal_connect(G_OBJECT(drawing_area), "draw", G_CALLBACK(on_draw_event), NULL);
gtk_widget_show_all(game_window);
//funktsioonide valja kutsumise aeg(100ms), ussi liikumise funktsiooni kutsumine mitu korda ja mänguakna kuvamine
g_timeout_add(100, (GSourceFunc)update_snake_position, (gpointer)drawing_area);
}
void on_high_scores_activate(GtkMenuItem *menuitem, gpointer data) {//skoori fail
FILE *fp = fopen("scores.txt", "r");
GtkWidget *dialog;
GtkWidget *content_area;
GtkWidget *label;
gchar *score_text = g_malloc(500);//mälu andmine skoori stringi jaoks
gchar *line = NULL;//to avoid uninitialized pointer errors
size_t len = 0;
ssize_t read;
if (fp == NULL) {
printf("Unable to open scores file.\n");
return;
}
dialog = gtk_dialog_new_with_buttons("High Scores",
NULL,GTK_DIALOG_MODAL,"OK",GTK_RESPONSE_ACCEPT,NULL);
content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));//skoori kuvamiseks lõpus
while ((read = getline(&line, &len, fp)) != -1) {
g_snprintf(score_text, 500, "%s%s\n", score_text, line);//skoori printimine dialoogi)
}
label = gtk_label_new(score_text);
gtk_container_add(GTK_CONTAINER(content_area), label);//kuvab skoori (dialoogi) akna
gtk_widget_show_all(dialog);
g_signal_connect(dialog,"response",G_CALLBACK(gtk_widget_destroy),NULL);//hävita signaal/lõpeta skoori kuvamine
fclose(fp);
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *menubar;
GtkWidget *filemenu;
GtkWidget *file;
GtkWidget *new_game;
GtkWidget *high_scores;
GError *error = NULL;
gtk_init(&argc, &argv);
initialize_game();
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);//põhiakna tegemine
gtk_window_set_title(GTK_WINDOW(window), "Snake");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);//põhiakna suurus
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);//menüü tegemine
gtk_container_add(GTK_CONTAINER(window), vbox);
menubar = gtk_menu_bar_new();
filemenu = gtk_menu_new();
file = gtk_menu_item_new_with_label("File");//menüü valikud fail tulemustega
new_game = gtk_menu_item_new_with_label("New Game");
high_scores = gtk_menu_item_new_with_label("High Scores");
gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);
gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), new_game);
gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), high_scores);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);//menubar
g_signal_connect(new_game, "activate", G_CALLBACK(on_new_game_activate), NULL);//signaalide aktiveerimine
g_signal_connect(high_scores, "activate", G_CALLBACK(on_high_scores_activate), NULL);//aktiveerimine skoori jaoks mänguga samal ajal
gtk_widget_show_all(window);//näita kõiki aknaid
gtk_main();//alustab gtk maini/sündmuseid/käske
save_score();//salvesta skoor
return 0;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment