Author Topic: Simon: The first pSDL game  (Read 3522 times)

0 Members and 1 Guest are viewing this topic.

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Simon: The first pSDL game
« on: June 30, 2012, 12:03:16 pm »
I created Simon the memory game for prizm using pSDL to show the basics of what it can currently do(I'm working on image loading and I may be able to finish before I leave tomorrow).
At the first menu heres what you can do
Press 1 to start it with text at the bottom for which arrow it maps to
press AC/ON to start without the help
press esc to exit

while it is running it will who each color/help if you enabled it for 3/4 of a second and then once all are shown it will go to black and you can start inputing arrows and the screen will change to the appropriate color
if you manage to get to 25 correct you win(the game)

My code is not exactly the best example of SDL code but here it is...
Spoiler For code:
Code: [Select]
#include <SDL.h>
#include <fxcg_syscalls.h>
#include <fxcg/display.h>
#include <stdlib.h>
#include <string.h>

static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
int sys_rand(void) {
    next = next * 1103515245 + 12345;
    return((unsigned)(next/65536) % 32768);
}

void sys_srand(unsigned seed) {
    next = seed;
}


int main()
{
SDL_Surface *screen;
int i, j, k, width, height, bpp, done=0, printCase=0, win=1;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);
width = 384;
height = 216;
bpp = 8;
SDL_Color palette[5];
palette[0].r=255; palette[1].g=0; palette[1].r=0;
palette[1].r=0; palette[2].g=255; palette[2].b=0;
palette[2].r=0; palette[3].g=0; palette[3].b=255;
palette[3].r=255; palette[0].g=255; palette[0].b=255;
palette[4].r=0; palette[4].g=0; palette[4].b=0;
screen = SDL_SetVideoMode(width, height, bpp, SDL_SWSURFACE);
SDL_SetColors(screen, palette, 0, 4);
if (screen == NULL) return -1;
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_UpdateRect(screen, 0, 0, 0, 0);
PrintXY(1, 1, "XXPress AC/ON to start", 0x20, TEXT_COLOR_WHITE);
PrintXY(1, 2, "XXPress Esc to Exit", 0x20, TEXT_COLOR_WHITE);
PrintXY(1, 3, "XXPress 1 for help", 0x20, TEXT_COLOR_WHITE);
Bdisp_PutDisp_DD();
while (!done)
{
while(SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
return 0;

case SDLK_POWER:
done = 1;
break;

case SDLK_1:
printCase = 1;
done = 1;
break;

default:
break;
}
}
}
if (SDL_GetTicks() > 10000) return 0;
    }
    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
    SDL_UpdateRect(screen, 0, 0, 0, 0);
    unsigned char set[25];
    unsigned char choice[25];
    sys_srand(SDL_GetTicks());
    for ( i=0; i<25; i++)
    {
set[i] = sys_rand() % 4;
}
for (i=0; i<25; i++)
{
for (j=0; j<i; j++)
{
memset(screen->pixels, set[j], width*height);
SDL_UpdateRect(screen, 0, 0, 0, 0);
if (set[j] > 3)
return -1;
if (printCase)
{
switch (set[j])
{
case 0:
PrintXY(18, 7, "XXUP", 0, TEXT_COLOR_BLACK);
break;

case 1:
PrintXY(16, 7, "XXRIGHT", 0, TEXT_COLOR_BLACK);
break;

case 2:
PrintXY(16, 7, "XXDOWN", 0, TEXT_COLOR_BLACK);
break;

case 3:
PrintXY(16, 7, "XXLEFT", 0, TEXT_COLOR_BLACK);
break;
}
Bdisp_PutDisp_DD();
}
SDL_Delay(750);
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0,0,0));
SDL_UpdateRect(screen, 0,0,0,0);
k=0;
while (k<i)
{
while(SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_UP:
choice[k]=0;
memset(screen->pixels, 0, width*height);
SDL_UpdateRect(screen, 0, 0, 0, 0);

k+=1;
break;

case SDLK_RIGHT:
choice[k]=1;
memset(screen->pixels, 1, width*height);
SDL_UpdateRect(screen, 0, 0, 0, 0);
k+=1;
break;

case SDLK_DOWN:
choice[k]=2;
memset(screen->pixels, 2, width*height);
SDL_UpdateRect(screen, 0, 0, 0, 0);
k+=1;
break;

case SDLK_LEFT:
choice[k]=3;
memset(screen->pixels, 3, width*height);
SDL_UpdateRect(screen, 0, 0, 0, 0);
k+=1;
break;

case SDLK_ESCAPE:
win=0;
i=100;
k=105;
break;


default:
break;
}
}
}
}
for (j=0; j<i; j++)
{
if (set[j] != choice[j])
{
win=0;
i=100;
}
}
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_UpdateRect(screen, 0, 0, 0, 0);
if (win == 0) PrintXY(1, 1, "XXSorry You have lost", 0x20, TEXT_COLOR_WHITE);
else PrintXY(1, 1, "XXYou have won the game!", 0x20, TEXT_COLOR_WHITE);
Bdisp_PutDisp_DD();
SDL_Delay(2000);
return 0;
}

EDIT: fixed error where esc could lock up calc


« Last Edit: June 30, 2012, 01:20:51 pm by ruler501 »
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Simon: The first pSDL game
« Reply #1 on: July 01, 2012, 01:30:44 am »
Oh Nice, I need to try this soon :). Glad to see Omnimaga members venture into PRIZM game programming. I was surprised practically no one here even bothered in the first 1.5 years the calc got released O.O

If you finish before leaving, do you think you could upload it at http://www.omnimaga.org/index.php?action=downloads;sa=add;cat=70 so it gets more visibility?
« Last Edit: July 01, 2012, 01:32:29 am by DJ_O »