ソースファイル一式 source code
03-JumpAct2.zip
MITライセンスです。
JumpAct2.cs
// #3 ジャンプアクション2 Jump Action2 2017/11/22 T.Umezawa
using System;
using System.Collections.Generic;
class Map
{
public static byte[,] sMap = new byte[ 16, 15 ];
public static bool IsArea( int x, int y )
{
return( x >= 0 && x < sMap.GetLength( 1 ) &&
y >= 0 && y < sMap.GetLength( 0 ) );
}
public static bool IsBlock( float x, float y )
{
x -= 0.5f;
y -= 0.5f;
int x0 = (int)Math.Floor( x );
int y0 = (int)Math.Floor( y );
int x1 = (int)Math.Ceiling( x );
int y1 = (int)Math.Ceiling( y );
return( IsBlock( x0, y0 ) ||
IsBlock( x1, y0 ) ||
IsBlock( x0, y1 ) ||
IsBlock( x1, y1 ) );
}
public static bool IsBlock( int x, int y )
{
return( !IsArea( x, y ) || sMap[ y, x ] != 0 );
}
}
class Unit
{
protected float mX, mY;
protected float mDX, mDY;
protected bool mGround = true;
public Unit( float x, float y, float dx, float dy )
{
mX = x;
mY = y;
mDX = dx;
mDY = dy;
}
public bool isBlock()
{
return( Map.IsBlock( mX, mY ) );
}
public virtual void step()
{
mX += mDX;
if( isBlock() ){
mDX = -mDX;
mX += mDX;
}
mY += mDY;
if( !mGround && mDY < 6.0f / 8 ){
mDY += 1.0f / 32; // 重力
}
if( mDY < 0 && isBlock() ){ // 上昇中
mDY = 1.0f / 16;
mY = (int)( mY - 0.5f ) + 1.5f;
}
if( mDY > 0 && isBlock() ){ // 下降中
mDY = 0;
mY = (int)( mY - 0.5f ) + 0.5f;
mGround = true;
}
if( mGround && !Map.IsBlock( mX, mY + 1.0f / 8 ) ){ // 地面から落ちる
mGround = false;
mY += 1.0f / 8;
mDY = 1.0f / 16;
}
}
public bool isCollision( Unit u )
{
return( Math.Abs( mX - u.mX ) < 7.0f / 8 && Math.Abs( mY - u.mY ) < 7.0f / 8 );
}
}
class Player : Unit
{
static System.Drawing.Bitmap sBM = new System.Drawing.Bitmap( "player.png" );
static System.Drawing.Rectangle sRect = new System.Drawing.Rectangle( 0, 0, 8, 8 );
public Player() : base( 0.5f, 14.5f, 1.0f / 16, 0 )
{
}
public void draw( System.Drawing.Graphics g )
{
sRect.X = ( (int)( mX * 8 ) & 1 ) * 8;
sRect.Y = Math.Sign( mDX ) * 4 + 4;
g.DrawImage( sBM, mX * 8 - 4, mY * 8 - 4, sRect, System.Drawing.GraphicsUnit.Pixel );
}
public void jump()
{
if( mGround ){
mGround = false;
mDY = -7.0f / 16;
}
}
public override void step()
{
base.step();
if( mX <= 0.5f || mX >= 14.5f ){
mDX = -mDX;
}
if( mY < 1 ){
JumpAct2.sGameClear = true;
}
}
}
class Enemy : Unit
{
static System.Drawing.Bitmap sBM = new System.Drawing.Bitmap( "monster.png" );
public Enemy() : base( JumpAct2.sRnd.Next( 13 ) + 1, 0.5f, JumpAct2.sRnd.Next( 2 ) / 16.0f - 1.0f / 32, 0 )
{
}
public void draw( System.Drawing.Graphics g )
{
g.DrawImage( sBM, mX * 8 - 4, mY * 8 - 4 );
}
public void step( List le )
{
step();
if( mX <= 0.5f || mX >= 14.5f ){
if( mY < 14 ){
mDX = -mDX;
}else{
mX = JumpAct2.sRnd.Next( 12 ) + 2;
mY = 0.5f;
}
}
foreach( Enemy en in le ){
if( en == this || !isCollision( en ) ){
continue;
}
mDX = Math.Sign( mX - en.mX ) / 32.0f;
if( mDX == 0 ){
mDX = 1.0f / 32;
}
en.mDX = -mDX;
}
}
}
class JumpAct2 : MyForm
{
public static Random sRnd = new Random();
static readonly System.Drawing.Bitmap sBM = new System.Drawing.Bitmap( "block.png" );
System.Drawing.Font mFont = new System.Drawing.Font( "MS Gothic", 4 );
int mCount;
Player mPlayer;
List mLEnemy;
public static bool sGameClear, sGameOver;
int mStage = 1;
protected override void OnLoad( EventArgs e )
{
base.OnLoad( e );
start();
mTimer.Interval = 25;
mTimer.Start();
}
protected override void OnMouseDown( System.Windows.Forms.MouseEventArgs e )
{
if( sGameClear ){
mStage++;
start();
}else if( e.Button == System.Windows.Forms.MouseButtons.Right ){
mStage = 1;
start();
}else{
mPlayer.jump();
}
base.OnMouseDown( e );
}
protected override void onMyPaint( System.Drawing.Graphics g )
{
g.TranslateTransform( 0, -50 + mCount / 16.0f );
for( int y = 0; y < Map.sMap.GetLength( 0 ); y++ ){
for( int x = 0; x < Map.sMap.GetLength( 1 ); x++ ){
if( Map.sMap[ y, x ] != 0 ){
g.DrawImage( sBM, x * 8, y * 8 );
}
}
}
mPlayer.draw( g );
foreach( Enemy en in mLEnemy ){
en.draw( g );
}
g.TranslateTransform( 0, 50 - mCount / 16.0f );
g.DrawString( "TIME " + mCount, mFont, mSBWhite, 0, 0 );
g.DrawString( "STAGE " + mStage, mFont, mSBWhite, 40, 0 );
if( sGameClear ){
g.DrawString( "GAME CLEAR!", mFont, mSBWhite, 40, 40 );
}
if( sGameOver ){
g.DrawString( "GAME OVER", mFont, mSBWhite, 40, 40 );
}
}
protected override void onMyTimer( object sender, System.Timers.ElapsedEventArgs e )
{
if( sGameClear || sGameOver ){
return;
}
mCount++;
mPlayer.step();
foreach( Enemy en in mLEnemy ){
en.step( mLEnemy );
if( mPlayer.isCollision( en ) ){
sGameOver = true;
}
}
Invalidate();
}
void start()
{
sGameClear = false;
sGameOver = false;
mCount = 0;
mPlayer = new Player();
mLEnemy = new List();
for( int i = 0; i < mStage; i++ ){
mLEnemy.Add( new Enemy() );
}
for( int x = 0; x < Map.sMap.GetLength( 1 ); x++ ){
Map.sMap[ Map.sMap.GetLength( 0 ) - 1, x ] = 1;
}
byte v = 1;
int n = 1;
for( int y = 3; y <= 12; y += 3 ){
for( int x = 0; x < Map.sMap.GetLength( 1 ); x++, n-- ){
if( n == 0 ){
v = (byte)( 1 - v );
n = Math.Max( sRnd.Next( 2 ), sRnd.Next( 2 ) ) + v + 1;
}
Map.sMap[ y, x ] = v;
}
}
}
[STAThread]
static void Main()
{
System.Windows.Forms.Application.Run( new JumpAct2() );
}
}
MyForm.cs
// Form継承 2017/11/21 T.Umezawa
using System;
using System.Collections.Generic;
class MyForm : System.Windows.Forms.Form
{
protected System.Timers.Timer mTimer = new System.Timers.Timer();
protected System.Drawing.SolidBrush mSBWhite = new System.Drawing.SolidBrush( System.Drawing.Color.White );
protected override void OnLoad( EventArgs e )
{
ClientSize = new System.Drawing.Size( 960, 720 );
Left = 62; // キャプチャ都合上
Top = 20; // キャプチャ都合上
DoubleBuffered = true;
BackColor = System.Drawing.Color.Black;
mTimer.Elapsed += new System.Timers.ElapsedEventHandler( onMyTimer );
}
protected override void OnPaint( System.Windows.Forms.PaintEventArgs e )
{
base.OnPaint( e );
System.Drawing.Graphics g = e.Graphics;
g.ScaleTransform( 8, 8 );
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
onMyPaint( g );
}
protected virtual void onMyPaint( System.Drawing.Graphics g )
{
}
protected virtual void onMyTimer( object sender, System.Timers.ElapsedEventArgs e )
{
}
}