Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CH 1 0 ! : # # # #include #WIDTH 6 4 0 # 4 8 0 #BLOCK _ SIZE 1 6 #SNAKE _

CH10
"!:
#
#
#
#include
#WIDTH 640
#480
#BLOCK_SIZE 16
#SNAKE_LENGTH 5
typedef {
int x;
int y;
}2;
typedef {
2
2
}
SDL_Window *
SDL_Renderer *renderer
SDL_Event
bool isRunning = true;
bool isGameOver =
[SNAKE_LENGTH]
2
void Init()
()
()
void ()
()
void SpawnFood()
int main(int argc char *argv[]){
SDL_Init(SDL_INIT_VIDEO)
window = SDL_CreateWindow(""Snake"", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN)
renderer = SDL_CreateRenderer(window,-1, SDL_RENDERER_ACCELERATED)
()
(){
(SDL_PollEvent(&event)){
(event.type == SDL_QUIT){
isRunning =
}
}
()
()
()
SDL_Delay(100);
}
SDL_DestroyRenderer(renderer)
SDL_DestroyWindow()
SDL_Quit();
0
}
void Init(){
[0].position.x =/2
[0].position.y = HEIGHT /2
[0].velocity.x = BLOCK_SIZE
[0].velocity.y =0
(int i =1 i < SNAKE_LENGTH++i){
snake[i].position.x =[i -1].position.x - BLOCK_SIZE
snake[i].position.y =[i -1].position.y;
[i].velocity.x =[i -1].velocity.x;
[i].velocity.y =[i -1].velocity.y
}
()
}
(){
(isGameOver){
}
(int i = SNAKE_LENGTH -1; i >0; --i){
snake[i].position.x =[i -1].position.x;
snake[i].position.y =[i -1].position.y;
}
snake[0].position.x += snake[0].velocity.x;
[0].position.y +=[0].velocity.y
()
}
(){
SDL_SetRenderDrawColor(000255)
SDL_RenderClear(renderer)
SDL_SetRenderDrawColor(255255255255)
(int i =0 i < SNAKE_LENGTH++i){
SDL_Rect rect ={snake[i].position.x, snake[i].position.y, BLOCK_SIZE, BLOCK_SIZE};
SDL_RenderFillRect(renderer, &rect);
}
SDL_Rect foodRect ={food.x, food.y, BLOCK_SIZE, BLOCK_SIZE};
SDL_RenderFillRect(renderer, &foodRect);
SDL_RenderPresent(renderer)
}
void HandleInput(){
(event.type == SDL_KEYDOWN){
(event.key.keysym.sym){
SDLK_UP:
(snake[0].velocity.y ==0){
[0].velocity.x =0
[0].velocity.y =-BLOCK_SIZE
}
SDLK_DOWN:
(snake[0].velocity.y ==0){
[0].velocity.x =0
[0].velocity.y = BLOCK_SIZE
}
SDLK_LEFT:
(snake[0].velocity.x ==0){
[0].velocity.x =-BLOCK_SIZE
[0].velocity.y =0
}
SDLK_RIGHT:
(

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions

Question

What is the specific purpose of an acceptable use policy?

Answered: 1 week ago