コード例 #1
0
        private void drawGame()
        {
            backGraphics.FillRectangle(Brushes.Black, 0, 0, VIEW_W, VIEW_H);
            backGraphics.DrawEllipse(Pens.Blue,
                                     player.x, player.y, BALL_SIZE * 2, BALL_SIZE * 2);

            int total = 0;

            for (int i = 0; i < MAX_BULLET; i++)
            {
                if (vBullet[i] != null)
                {
                    total++;
                    backGraphics.DrawEllipse(Pens.Yellow,
                                             vBullet[i].x, vBullet[i].y, BALL_SIZE * 2, BALL_SIZE * 2);
                }
            }

            //子彈數量
            String str = "子彈數量" + total;

            backGraphics.DrawString(str, SystemFonts.CaptionFont,
                                    Brushes.Black, 0, 0);


            //畫遊戲畫面
            total = 0;
            for (int i = 0; i < MAX_ENEMY; i++)
            {
                if (vMonster[i] != null)
                {
                    total++;
                    backGraphics.DrawEllipse(Pens.Red,
                                             vMonster[i].x, vMonster[i].y, BALL_SIZE * 2, BALL_SIZE * 2);
                }
            }


            //怪物數量
            str = "怪物數量" + total;
            backGraphics.DrawString(str, SystemFonts.CaptionFont,
                                    Brushes.Black, 0, 20);

            if (keySpace.isDown())
            {
                backGraphics.DrawString("space down", SystemFonts.CaptionFont,
                                        Brushes.Black, 0, 40);
            }
            else
            {
                backGraphics.DrawString("space up", SystemFonts.CaptionFont,
                                        Brushes.Black, 0, 40);
            }

            //把背景頁畫到視窗上面
            wndGraphics.DrawImageUnscaled(backBmp, 0, 0);
        }
コード例 #2
0
        //this.inva
        // Invalidate();//通知重繪畫面}

        private void onTimer(object sender, EventArgs e)
        {
            //就是主迴圈main loop
            keySpace.onTimer();//bPress
            keyUp.onTimer();
            keyDown.onTimer();
            keyLeft.onTimer();
            keyRight.onTimer();

            if (keySpace.isPress())//壓下去
            {
                //也要做發射
                addBullet();
            }
            if (keySpace.isDown())
            {
                //壓著空白鍵
                makeBulletTime -= 1.0f / REQUIRE_FPS;
                if (makeBulletTime <= 0)
                {
                    //子彈發射時間
                    addBullet();
                }
            }
            if (keyUp.isDown())//下
            {
                player.y -= 5;
            }
            if (keyDown.isDown())
            {
                player.y += 5;
            }
            if (keyLeft.isDown())
            {
                player.x -= 5;
            }
            if (keyRight.isDown())
            {
                player.x += 5;
            }


            makeMonsterTime -= 1.0f / REQUIRE_FPS; // 1/30秒
            if (makeMonsterTime <= 2.5)
            {
                //產生怪時間到了
                for (int i = 0; i < MAX_ENEMY; i++)
                {
                    if (vMonster[i] == null)
                    {
                        vMonster[i] = new Point();

                        vMonster[i].x = random.Next(0, VIEW_W);
                        vMonster[i].y = random.Next(0, VIEW_H);

                        makeMonsterTime = MAKE_MONSTER_TIME;

                        break;
                    }
                }
            }

            moveMonster();

            moveBullet_Killmonster();

            drawGame();
        }