Skip to content

Commit

Permalink
finale
Browse files Browse the repository at this point in the history
  • Loading branch information
frlundst committed Mar 8, 2021
1 parent 9b03e50 commit a3a5b4c
Show file tree
Hide file tree
Showing 19 changed files with 637 additions and 382 deletions.
2 changes: 0 additions & 2 deletions .deps/delay.S.P

This file was deleted.

6 changes: 0 additions & 6 deletions .deps/game.c.P

This file was deleted.

2 changes: 0 additions & 2 deletions .deps/labwork.S.P

This file was deleted.

2 changes: 0 additions & 2 deletions .deps/snake.c.P

This file was deleted.

5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.c.o
*.S.o
outfile*
/.deps
.vscode
Binary file modified data.c.o
Binary file not shown.
3 changes: 2 additions & 1 deletion delay.S
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ hexasc:
addi $v0,$a0,0x30
jr $ra
nop


delay:
PUSH $s3
li $s3, 47000
li $s3, 470000
li $t6, 0
li $t7, 0
j loop
Expand Down
Binary file removed delay.S.o
Binary file not shown.
31 changes: 12 additions & 19 deletions display.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ void display_init(void){
spi_send_recv(0xAF);
}

void display_string(int line, char *s) { //LAB
/*(TAKEN FROM LAB) Display text*/
void display_string(int line, char *s) {
int i;
if(line < 0 || line >= 4)
return;
Expand All @@ -81,6 +82,7 @@ void display_string(int line, char *s) { //LAB
textbuffer[line][i] = ' ';
}

/*(TAKEN FROM LAB) Display text*/
void display_update(void) { //LAB
int i, j, k;
int c;
Expand Down Expand Up @@ -125,6 +127,8 @@ void display_image(int x, const uint8_t *data) { //LAB
}
}

/*This function translates the display array to oled_display array.
It converts an x and y coordinate system into a readable array for the computer.*/
void translateToImage() {
int page, column, row, c, k;
uint8_t powerOfTwo = 1;
Expand All @@ -147,19 +151,8 @@ void translateToImage() {
}
}

void create_object(int xPos, int yPos, int width, int height) {
int row, column;

for (row = 0; row < 32; row++) {
for (column = 0; column < 128; column++) {
if (row >= yPos && row <= (yPos + height) && column >= xPos && column <= (xPos + width)) {
display[row][column] = 1;
}
}
}
}

void clear_display() {
/*This function sets all the values in the display array and oled display array into 0s*/
void clear_display() {
int row, column, i;

for(row = 0; row < 32; row++) {
Expand All @@ -173,12 +166,12 @@ void clear_display() {
}
}

/*This function calls all the necessary functions for the game to start*/
void display_start(){
clear_display();
snake();
//create_object(64,16,1,1);
translateToImage();
display_image(0, oled_display);
clear_display(); //This clear the screen everytime it loops. It will clear the last frame
game(); //Call game
translateToImage(); //Translate
display_image(0, oled_display); //And then display the frame
}


Expand Down
Binary file modified display.c.o
Binary file not shown.
174 changes: 142 additions & 32 deletions game.c
Original file line number Diff line number Diff line change
@@ -1,54 +1,164 @@
/*Written by Fredrik Lundström and Michell Dib 2021 (c)*/

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <pic32mx.h>
#include "snake.h" //Project header file

int score = 0;
int speed = 20;
int btns;
int speed = 1000; //Higher is slower

//We made premade apples since the time.h library would not work on the Uno32
int apple_x[] = {
56,
39,
28,
95,
46,
107,
13,
69,
65,
11,
28,
82
};

int snake_length;
int snake_width = 1;
int snake_x = 64;
int snake_y = 16;
int apple_y[] = {
10,
29,
20,
9,
15,
3,
21,
27,
11,
17,
8,
12
};

int apple_x;
int apple_y;

void snake(){
snake_create();
apple_create();
void game(){
snake_move(); //First we will call for the snake movements
if(snake.y[0] == apple_y[score] && snake.x[0] == apple_x[score]){ //If we collide with apple
eat_apple(); //Call eat_apple function
}
if((snake.y[0] == 32 || snake.y[0] == 0) || (snake.x[0] == 128 || snake.x[0] == 0)){ //If we collide with wall
collision(); //Call collision function
}
create_snake(); //After movements are made we create the snake

if(snake_x == apple_x && snake_y == apple_y){
apple_eat();
int k;
for(k = snake.length - 1; k > 0; k--){ // This for loop check if the head will collide with any coordinate of the body
if(snake.x[0] == snake.x[k] && snake.y[0] == snake.y[k])
collision();
}

create_apple(); //Every frame we create an apple based on the score that changes the apples coordinates to any of the premade values.
delay(speed);
snake_move();
}

void snake_create(){
snake_length = 5;
create_object(snake_x, snake_y, snake_length, snake_width);
void collision(){
char score2[] = "X";
score2[0] = score + '0'; //Here we make an integer to a string

while(btns != 0b1000){ //While BTN4 is not pressed we will display the score and a game over string.
clear_display();
display_string(1, "GAME OVER SCORE: ");
display_string(2, score2);
display_update();
translateToImage();
display_image(0, oled_display);
btns = getbtns();
}
main(); //If BTN4 is pressed we jump back to main and the game will then restart.
}

void snake_move(){
if(snake_x > 128){
snake_x = 0;
}else{
snake_x++;

void create_snake(){ //Here we will print out the snake
int i,j,k,x,y;
for(k = 0; k < snake.length; k++){
for(i = 0; i < 1; i++){ //These for-loops can change the size of the snake but we decided to go with one pixel
for(j = 0; j < 1; j++){
x = snake.x[k] + i;
y = snake.y[k] + j;

display[y][x] = 1;
}
}
}
}

void snake_move(){ //MOVEMENT FOR SNAKE

btns = getbtns(); //Get buttons

int i;
for(i = snake.length - 1; i > 0; i--){ //This for loop will change the coordinates of every part of the snake so that the body will follow the head
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
}

if(snake.dir == 'R'){ //RIGHT
snake.x[0] += 1;
}
if(snake.dir == 'D'){ //DOWN
snake.y[0] += 1;
}
if(snake.dir == 'U'){ //UP
snake.y[0] -= 1;
}
if(snake.dir == 'L'){ //LEFT
snake.x[0] -= 1;
}

/*for(k = snake.length - 1; k > 0; k--){ // This for loop check if the head will collide with any coordinate of the body
if(snake.x[0] == snake.x[k] && snake.y[0] == snake.y[k])
collision();
}*/

// SO THE UNO32 REMEMBER THE DIRECTION EVEN AFTER THE BUTTON IS RELEASED
if(snake.dir != 'L'){
if(btns == 0b0001){ //RIGHT
snake.dir = 'R';
}
}
if(snake.dir != 'U'){
if(btns == 0b0010){ //DOWN
snake.dir = 'D';
}
}
if(snake.dir != 'D'){
if(btns == 0b0100){ //UP
snake.dir = 'U';
}
}
if(snake.dir != 'R'){
if(btns == 0b1000){ //LEFT
snake.dir = 'L';
}
}
}

void create_apple(){
//RANDOMIZE POSITION does not work :(
display[apple_y[score]][apple_x[score]] = 1; //Display apple
}

void apple_create(){
//Randomize
create_object(apple_x, apple_y, 1, 1);
void eat_apple(){
snake.length++; //Increase length
score++; //Increase score which will also give the apple new coordinates
}

void apple_eat(){
display[apple_x][apple_y] = 0; //Clear apple pixel
apple_x = 5;
apple_y = 5;
apple_create();
score++;
int getbtns(void){ //Get btns
int btn = (PORTD >> 5) & 0x0007;
btn = (btn << 1);
//Since the buttons was set to bits 11 through 5 we will now
//shift the bits 5 bits the right and check them with and operation
//and then see if buttons 4,3 or 2 are active.
// & them with seven since it is 0b0111 and button 1 will then always be 0
btn = ((PORTF >> 1) & 0x1) | btn; //For some reason the first button is in the portf register
return btn; //Return the active buttons as integer (active bits will be 1)
}
Binary file removed game.c.o
Binary file not shown.
37 changes: 34 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,44 @@ int main(void){
/* SPI2CON bit ON = 1; */
SPI2CONSET = 0x8000;

display_init(); //Initialize OLED display
display_init(); //Initialize OLED display

score = 0; //Set score to one

snake.y = snakeY; //Set y to array
snake.x = snakeX; //Set x to array

snake.y[0] = 16; //Initialize snake
snake.x[0] = 64; //We make the snake big at first for demonstration purposes only
snake.y[1] = 16;
snake.x[1] = 65;
snake.y[2] = 16;
snake.x[2] = 66;
snake.y[3] = 16;
snake.x[3] = 67;
snake.y[4] = 16;
snake.x[4] = 68;
snake.y[5] = 16;
snake.x[5] = 69;
snake.y[6] = 16;
snake.x[6] = 70;
snake.y[7] = 16;
snake.x[7] = 71;
snake.y[8] = 16;
snake.x[8] = 72;
snake.y[9] = 16;
snake.x[9] = 73;


snake.length = 10;
snake.dir = 'L';

while(1){
display_start();
display_start(); //Start display
}

return;
}



Binary file modified main.c.o
Binary file not shown.
Binary file modified outfile.elf
Binary file not shown.
Loading

0 comments on commit a3a5b4c

Please sign in to comment.