私は新しいOOPの学生です。私はC#でXamarinスタジオで簡単な時計プログラムを書いています。時計のフォーマットは00:00:00です。カウントアップして時計として動作する必要があります。 しかし、私の時計は画面上に描画することができますが、「チェック」しないので、いくつかの問題がなければなりません。時計プログラムはカウントされません
using SwinGameSDK;
namespace MyGame
{
public class Clock
{
private Counter counter = new Counter();
private int _seconds;
private int _minutes;
private int _hours;
public Clock()
{
_seconds = counter.SecondsCounter;
_minutes = counter.MinutesCounter;
_hours = counter.HoursCounter;
}
public int Seconds {
get {
return _seconds;
}
set {
_seconds = value;
}
}
public int Minutes {
get {
return _minutes;
}
set {
_minutes = value;
}
}
public int Hours {
get {
return _hours;
}
set {
_hours = value;
}
}
public void DrawClock()
{
DrawHours();
SwinGame.DrawText (":", Color.Black, "Arial", 80, 360, 200);
DrawMinutes();
SwinGame.DrawText (":", Color.Black, "Arial", 80, 520, 200);
DrawSeconds();
}
public void DrawHours()
{
SwinGame.DrawText (Hours.ToString ("D2"), Color.Black, "Arial", 80, 250, 208);
}
public void DrawMinutes()
{
SwinGame.DrawText (Minutes.ToString ("D2"), Color.Black, "Arial", 80, 410, 208);
}
public void DrawSeconds()
{
SwinGame.DrawText (Seconds.ToString ("D2"), Color.Black, "Arial", 80, 560, 208);
}
public void UpdateClock()
{
counter.UpdateCounter();
}
public void ResetClock()
{
counter.Reset();
}
public void SetClock()
{
counter.SetTimer();
}
}
}
using System.Timers;
namespace MyGame
{
public class Counter
{
private int _hoursCounter;
private int _minutesCounter;
private int _secondsCounter;
public Counter()
{
_hoursCounter = 0;
_minutesCounter = 0;
_secondsCounter = 0;
}
public int HoursCounter {
get {
return _hoursCounter;
}
set {
_hoursCounter = value;
}
}
public int MinutesCounter {
get {
return _minutesCounter;
}
set {
_minutesCounter = value;
}
}
public int SecondsCounter {
get {
return _secondsCounter;
}
set {
_secondsCounter = value;
}
}
Timer timer = new Timer();
public void SetTimer()
{
timer.Interval = 1000;
timer.Elapsed += (sender, e) => UpdateCounter();
}
public void UpdateCounter()
{
timer.Start();
SecondsCounter += 1;
if (SecondsCounter == 60) {
SecondsCounter = 0;
MinutesCounter += 1;
}
if (MinutesCounter == 60) {
MinutesCounter = 0;
HoursCounter += 1;
}
if (HoursCounter == 24) {
HoursCounter = 0;
MinutesCounter = 0;
SecondsCounter = 0;
}
}
public void Reset()
{
HoursCounter = 0;
MinutesCounter = 0;
SecondsCounter = 0;
timer.Close();
}
}
}
using SwinGameSDK;
namespace MyGame
{
public class GameMain
{
public static void Main()
{
var myClock = new Clock();
//Open the game window
SwinGame.OpenGraphicsWindow ("GameMain", 800, 600);
SwinGame.ShowSwinGameSplashScreen();
//Run the game loop
while (false == SwinGame.WindowCloseRequested()) {
//Fetch the next batch of UI interaction
SwinGame.ProcessEvents();
//Clear the screen and draw the framerate
SwinGame.ClearScreen (Color.White);
SwinGame.DrawFramerate (0, 0);
myClock.DrawClock();
myClock.SetClock();
if (SwinGame.MouseClicked (MouseButton.LeftButton)) {
myClock.UpdateClock();
}
if (SwinGame.MouseClicked (MouseButton.RightButton)) {
myClock.ResetClock();
}
//Draw onto the screen
SwinGame.RefreshScreen (60);
}
}
}
}
側注:使用自動実装プロパティ:https://msdn.microsoft.com/en-us/library/bb384054.aspx – user3185569
をあなたはそれをデバッグしようとしましたか?チックが起きたときに画面を更新する方法にも入りますか? – 3615
まず、コードをデバッグして何が起きているのかを確認する必要があります。 'SetTimer'に' timer.Enabled = true'を追加しても問題ありませんか? – Pikoh