mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Pong and snake now use rand(3).
This commit is contained in:
parent
8b7188e6a4
commit
c5605b6693
6 changed files with 79 additions and 20 deletions
|
@ -6,7 +6,7 @@
|
||||||
#include <libmaxsi/sortix-vga.h>
|
#include <libmaxsi/sortix-vga.h>
|
||||||
#include <libmaxsi/sortix-keyboard.h>
|
#include <libmaxsi/sortix-keyboard.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
using namespace Maxsi;
|
using namespace Maxsi;
|
||||||
using namespace Maxsi::Keyboard;
|
using namespace Maxsi::Keyboard;
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
#include <libmaxsi/sortix-vga.h>
|
#include <libmaxsi/sortix-vga.h>
|
||||||
#include <libmaxsi/sortix-keyboard.h>
|
#include <libmaxsi/sortix-keyboard.h>
|
||||||
#include <libmaxsi/sortix-sound.h>
|
#include <libmaxsi/sortix-sound.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
using namespace Maxsi;
|
using namespace Maxsi;
|
||||||
using namespace Maxsi::Keyboard;
|
using namespace Maxsi::Keyboard;
|
||||||
|
@ -107,22 +109,26 @@ void Goal(nat player)
|
||||||
{
|
{
|
||||||
frame->text[ballx + bally*width] = ' ' | (COLOR8_WHITE << 8);
|
frame->text[ballx + bally*width] = ' ' | (COLOR8_WHITE << 8);
|
||||||
|
|
||||||
|
int offset = (rand() % 4) - 2;
|
||||||
ballx = width/2;
|
ballx = width/2;
|
||||||
bally = height/2;
|
bally = height/2 + offset;
|
||||||
|
|
||||||
if ( player == 1 )
|
if ( player == 1 )
|
||||||
{
|
{
|
||||||
ballvelx = 1;
|
ballvelx = 1;
|
||||||
ballvely = 1;
|
ballvely = (rand() % 2) * 2 - 1;
|
||||||
p1score++;
|
p1score++;
|
||||||
}
|
}
|
||||||
else if ( player == 2 )
|
else if ( player == 2 )
|
||||||
{
|
{
|
||||||
ballvelx = -1;
|
ballvelx = -1;
|
||||||
ballvely = -1;
|
ballvely = (rand() % 2) * 2 - 1;
|
||||||
p2score++;
|
p2score++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( ballvely > 0 ) { ballvely /= ballvely; }
|
||||||
|
if ( ballvely < 0 ) { ballvely /= -ballvely; }
|
||||||
|
|
||||||
System::Sound::SetFrequency(goalfreq);
|
System::Sound::SetFrequency(goalfreq);
|
||||||
soundleft = 50;
|
soundleft = 50;
|
||||||
|
|
||||||
|
@ -198,8 +204,30 @@ void ReadInput()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int usage(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
printf("usage: %s [OPTIONS]\n", argv[0]);
|
||||||
|
printf("Options:\n");
|
||||||
|
printf(" --speed <miliseconds> How many miliseconds between updates\n");
|
||||||
|
printf(" --usage Display this screen\n");
|
||||||
|
printf(" --help Display this screen\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
int sleepms = 50;
|
||||||
|
for ( int i = 1; i < argc; i++ )
|
||||||
|
{
|
||||||
|
if ( String::Compare(argv[i], "--help") == 0 ) { return usage(argc, argv); }
|
||||||
|
if ( String::Compare(argv[i], "--usage") == 0 ) { return usage(argc, argv); }
|
||||||
|
if ( String::Compare(argv[i], "--speed") == 0 && 1 < argc-i )
|
||||||
|
{
|
||||||
|
sleepms = String::ToInt(argv[++i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int result = Init();
|
int result = Init();
|
||||||
if ( result != 0 ) { return result; }
|
if ( result != 0 ) { return result; }
|
||||||
|
|
||||||
|
@ -208,7 +236,6 @@ int main(int argc, char* argv[])
|
||||||
ReadInput();
|
ReadInput();
|
||||||
Update();
|
Update();
|
||||||
UpdateUI();
|
UpdateUI();
|
||||||
const unsigned sleepms = 50;
|
|
||||||
Thread::USleep(sleepms * 1000);
|
Thread::USleep(sleepms * 1000);
|
||||||
if ( soundleft < 0 ) { continue; }
|
if ( soundleft < 0 ) { continue; }
|
||||||
if ( soundleft <= 50 )
|
if ( soundleft <= 50 )
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <libmaxsi/keyboard.h>
|
#include <libmaxsi/keyboard.h>
|
||||||
#include <libmaxsi/sortix-vga.h>
|
#include <libmaxsi/sortix-vga.h>
|
||||||
#include <libmaxsi/sortix-keyboard.h>
|
#include <libmaxsi/sortix-keyboard.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
using namespace Maxsi;
|
using namespace Maxsi;
|
||||||
using namespace Maxsi::Keyboard;
|
using namespace Maxsi::Keyboard;
|
||||||
|
@ -36,15 +37,6 @@ const int speedincrease = -5;
|
||||||
const int maxspeed = 40;
|
const int maxspeed = 40;
|
||||||
volatile int speed;
|
volatile int speed;
|
||||||
|
|
||||||
// HACK: Sortix has no random number generator yet!
|
|
||||||
int random_seed=1337;
|
|
||||||
int rand(int max)
|
|
||||||
{
|
|
||||||
int tmpmax=max;
|
|
||||||
random_seed = random_seed + 37 * 1103515245 + 12345;
|
|
||||||
return (unsigned int) (random_seed / 65536) % tmpmax;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Clear()
|
void Clear()
|
||||||
{
|
{
|
||||||
// Reset the game data.
|
// Reset the game data.
|
||||||
|
@ -56,7 +48,7 @@ void Reset()
|
||||||
Clear();
|
Clear();
|
||||||
tailx = posx = width/2;
|
tailx = posx = width/2;
|
||||||
taily = posy = height/2;
|
taily = posy = height/2;
|
||||||
switch ( rand(4) )
|
switch ( rand() % 4 )
|
||||||
{
|
{
|
||||||
case 0: velx = -1; vely = 0; break;
|
case 0: velx = -1; vely = 0; break;
|
||||||
case 1: velx = 1; vely = 0; break;
|
case 1: velx = 1; vely = 0; break;
|
||||||
|
@ -64,8 +56,8 @@ void Reset()
|
||||||
case 3: velx = 0; vely = -1; break;
|
case 3: velx = 0; vely = -1; break;
|
||||||
}
|
}
|
||||||
|
|
||||||
animalx = 2 + rand(width-4);
|
animalx = 2 + (rand() % width-4);
|
||||||
animaly = 2 + rand(height-4);
|
animaly = 2 + (rand() % height-4);
|
||||||
|
|
||||||
taillen = 0;
|
taillen = 0;
|
||||||
tailmax = 3;
|
tailmax = 3;
|
||||||
|
@ -149,8 +141,8 @@ void Update()
|
||||||
if ( newx == animalx && newy == animaly )
|
if ( newx == animalx && newy == animaly )
|
||||||
{
|
{
|
||||||
tailmax++;
|
tailmax++;
|
||||||
animalx = 2 + rand(width-4);
|
animalx = 2 + (rand() % width-4);
|
||||||
animaly = 2 + rand(height-4);
|
animaly = 2 + (rand() % height-4);
|
||||||
if ( maxspeed < speed ) { speed += speedincrease; }
|
if ( maxspeed < speed ) { speed += speedincrease; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,6 +55,7 @@ thread.o \
|
||||||
io.o \
|
io.o \
|
||||||
init.o \
|
init.o \
|
||||||
start.o \
|
start.o \
|
||||||
|
random.o \
|
||||||
|
|
||||||
HEADERS=\
|
HEADERS=\
|
||||||
error.h \
|
error.h \
|
||||||
|
|
|
@ -50,6 +50,7 @@ void exit(int);
|
||||||
void _Exit(int status);
|
void _Exit(int status);
|
||||||
void free(void*);
|
void free(void*);
|
||||||
void* malloc(size_t);
|
void* malloc(size_t);
|
||||||
|
int rand(void);
|
||||||
|
|
||||||
/* TODO: These are not implemented in libmaxsi/sortix yet. */
|
/* TODO: These are not implemented in libmaxsi/sortix yet. */
|
||||||
#ifndef SORTIX_UNIMPLEMENTED
|
#ifndef SORTIX_UNIMPLEMENTED
|
||||||
|
@ -90,7 +91,6 @@ int posix_openpt(int);
|
||||||
char* ptsname(int);
|
char* ptsname(int);
|
||||||
int putenv(char*);
|
int putenv(char*);
|
||||||
void qsort(void*, size_t, size_t, int (*)(const void*, const void*));
|
void qsort(void*, size_t, size_t, int (*)(const void*, const void*));
|
||||||
int rand(void);
|
|
||||||
long random(void);
|
long random(void);
|
||||||
void* realloc(void*, size_t);
|
void* realloc(void*, size_t);
|
||||||
char* realpath(const char* restrict, char* restrict);
|
char* realpath(const char* restrict, char* restrict);
|
||||||
|
|
39
libmaxsi/random.cpp
Normal file
39
libmaxsi/random.cpp
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
/******************************************************************************
|
||||||
|
|
||||||
|
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
|
||||||
|
|
||||||
|
This file is part of LibMaxsi.
|
||||||
|
|
||||||
|
LibMaxsi is free software: you can redistribute it and/or modify it under
|
||||||
|
the terms of the GNU Lesser General Public License as published by the Free
|
||||||
|
Software Foundation, either version 3 of the License, or (at your option)
|
||||||
|
any later version.
|
||||||
|
|
||||||
|
LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
|
||||||
|
more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
random.cpp
|
||||||
|
Provides access to random numbers using various algorithms.
|
||||||
|
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include "platform.h"
|
||||||
|
|
||||||
|
namespace Maxsi
|
||||||
|
{
|
||||||
|
namespace Random
|
||||||
|
{
|
||||||
|
int random_seed=1337;
|
||||||
|
extern "C" int rand()
|
||||||
|
{
|
||||||
|
random_seed = random_seed + 37 * 1103515245 + 12345;
|
||||||
|
return random_seed / 65536;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue