Ideas to start with MVC refactoring of Windows Forms simple game? C#

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












As a part of studying C#, I was given a naive Windows Forms game code. The task is to try and make it more MVC-like and optimize where possible.



I've already read what MVC is, but having difficulties what to start with. Here's the code itself, TODOs are just my thoughts.



I'm not asking to do my job, but I would love to get some kind of a set of instructions, any ideas are welcomed and appreciated thou. Thank you



To have an idea what a game looks like, check this image
a simple top-down car game



using System;
using System.Drawing;
using System.Windows.Forms;

namespace OldSchoolRacingGame

public partial class CarGame : Form

private int carSpeed = 5;
private int roadSpeed = 5;
private bool carLeft;
private bool carRight;
private int trafficSpeed = 5;
private int score = 0;
Random rnd = new Random();

public CarGame()

InitializeComponent();

Reset();


private void Reset()


trophy.Visible = false;
button1.Enabled = false;
explosion.Visible = false;
trafficSpeed = 5;
roadSpeed = 5;
score = 0;

Player.Left = 161; // placing player
Player.Top = 286;

carLeft = false;
carRight = false;

Car1.Left = 66; // placing obstacles
Car1.Top = -120;

Car2.Left = 294;
Car2.Top = -185;

roadTrack2.Left = -3; // placing background images
roadTrack2.Top = -222;
roadTrack1.Left = -2;
roadTrack1.Top = -638;

timer1.Start(); // timer starts


private void timer1_Tick(object sender, EventArgs e)


private void CarGame_Load(object sender, EventArgs e)

//TODO: ?


private void MoveCar(object sender, KeyEventArgs e)

if (e.KeyCode == Keys.Left && Player.Left > 0)
carLeft = true;

if (e.KeyCode == Keys.Right &&
Player.Left + Player.Width < panel1.Width)
carRight = true;


private void KeepCarInside(object sender, KeyEventArgs e)

if (e.KeyCode == Keys.Left)
carLeft = false;

if (e.KeyCode == Keys.Right)
carRight = false;


private void SwitchCar1Image()

//TODO: try to make a separate method based on RandomNumberGenerator
//TODO: move to GameModel.cs
int number = rnd.Next(1, 8);

switch (number)

case 1:
Car1.Image = Properties.Resources.carGreen;
break;
case 2:
Car1.Image = Properties.Resources.carOrange;
break;
case 3:
Car1.Image = Properties.Resources.carGrey;
break;
case 4:
Car1.Image = Properties.Resources.CarRed;
break;
case 5:
Car1.Image = Properties.Resources.carPink;
break;
case 6:
Car1.Image = Properties.Resources.TruckBlue;
break;
case 7:
Car1.Image = Properties.Resources.TruckWhite;
break;
case 8:
Car1.Image = Properties.Resources.ambulance;
break;




private void SwitchCar2Image()

// TODO: same as SwitchCar1Image()
int number = rnd.Next(1, 8);

switch (number)

case 1:
Car2.Image = Properties.Resources.carGreen;
break;
case 2:
Car2.Image = Properties.Resources.carOrange;
break;
case 3:
Car2.Image = Properties.Resources.carGrey;
break;
case 4:
Car2.Image = Properties.Resources.CarRed;
break;
case 5:
Car2.Image = Properties.Resources.carPink;
break;
case 6:
Car2.Image = Properties.Resources.TruckBlue;
break;
case 7:
Car2.Image = Properties.Resources.TruckWhite;
break;
case 8:
Car2.Image = Properties.Resources.ambulance;
break;



private void GameOver()

// TODO: move to GameModel.cs
trophy.Visible = true;

timer1.Stop();

button1.Enabled = true;

explosion.Visible = true;
Player.Controls.Add(explosion);
explosion.Location = new Point(-8, 5);
explosion.BackColor = Color.Transparent;
explosion.BringToFront();

if (score < 1000)
trophy.Image = Properties.Resources.bronze;

if (score > 2000)
trophy.Image = Properties.Resources.silver;

if (score > 3500)
trophy.Image = Properties.Resources.gold;


private void button1_Click(object sender, EventArgs e)

Reset();


private void roadTrack2_Click(object sender, EventArgs e)











share|improve this question



















  • MVC stands for Model-View-Controller. As long as these patterns aren't present in your code I would argue that it doesn't fit the MVC pattern :)
    – IEatBagels
    May 10 at 12:44










  • @TopinFrassi I see your point. Thanks
    – Burn_jacks
    May 10 at 12:56










  • I'd say analyze the code and write down every functionality that it has, then start from scratch applying the MVC pattern.
    – Denis
    May 10 at 14:19










  • @Denis That's exactly what i am trying to do ) starting from scratch ) thanx
    – Burn_jacks
    May 10 at 14:21






  • 1




    @Burn_jacks: There is a line of acceptability between refactoring coding style (which answers on CR often do) and refactoring the code and its design pattern. You're asking for the latter, to a degree where we'd be doing something that you haven't shown to grasp. That's out of scope for a code review. Your question is essentially asking us to do the MVC design for you. Read up on the individual responsibilities of a model, a view, and a controller. Then design classes with those responsibilities in mind. Then start carrying over the game logic.
    – Flater
    May 11 at 8:34

















up vote
0
down vote

favorite












As a part of studying C#, I was given a naive Windows Forms game code. The task is to try and make it more MVC-like and optimize where possible.



I've already read what MVC is, but having difficulties what to start with. Here's the code itself, TODOs are just my thoughts.



I'm not asking to do my job, but I would love to get some kind of a set of instructions, any ideas are welcomed and appreciated thou. Thank you



To have an idea what a game looks like, check this image
a simple top-down car game



using System;
using System.Drawing;
using System.Windows.Forms;

namespace OldSchoolRacingGame

public partial class CarGame : Form

private int carSpeed = 5;
private int roadSpeed = 5;
private bool carLeft;
private bool carRight;
private int trafficSpeed = 5;
private int score = 0;
Random rnd = new Random();

public CarGame()

InitializeComponent();

Reset();


private void Reset()


trophy.Visible = false;
button1.Enabled = false;
explosion.Visible = false;
trafficSpeed = 5;
roadSpeed = 5;
score = 0;

Player.Left = 161; // placing player
Player.Top = 286;

carLeft = false;
carRight = false;

Car1.Left = 66; // placing obstacles
Car1.Top = -120;

Car2.Left = 294;
Car2.Top = -185;

roadTrack2.Left = -3; // placing background images
roadTrack2.Top = -222;
roadTrack1.Left = -2;
roadTrack1.Top = -638;

timer1.Start(); // timer starts


private void timer1_Tick(object sender, EventArgs e)


private void CarGame_Load(object sender, EventArgs e)

//TODO: ?


private void MoveCar(object sender, KeyEventArgs e)

if (e.KeyCode == Keys.Left && Player.Left > 0)
carLeft = true;

if (e.KeyCode == Keys.Right &&
Player.Left + Player.Width < panel1.Width)
carRight = true;


private void KeepCarInside(object sender, KeyEventArgs e)

if (e.KeyCode == Keys.Left)
carLeft = false;

if (e.KeyCode == Keys.Right)
carRight = false;


private void SwitchCar1Image()

//TODO: try to make a separate method based on RandomNumberGenerator
//TODO: move to GameModel.cs
int number = rnd.Next(1, 8);

switch (number)

case 1:
Car1.Image = Properties.Resources.carGreen;
break;
case 2:
Car1.Image = Properties.Resources.carOrange;
break;
case 3:
Car1.Image = Properties.Resources.carGrey;
break;
case 4:
Car1.Image = Properties.Resources.CarRed;
break;
case 5:
Car1.Image = Properties.Resources.carPink;
break;
case 6:
Car1.Image = Properties.Resources.TruckBlue;
break;
case 7:
Car1.Image = Properties.Resources.TruckWhite;
break;
case 8:
Car1.Image = Properties.Resources.ambulance;
break;




private void SwitchCar2Image()

// TODO: same as SwitchCar1Image()
int number = rnd.Next(1, 8);

switch (number)

case 1:
Car2.Image = Properties.Resources.carGreen;
break;
case 2:
Car2.Image = Properties.Resources.carOrange;
break;
case 3:
Car2.Image = Properties.Resources.carGrey;
break;
case 4:
Car2.Image = Properties.Resources.CarRed;
break;
case 5:
Car2.Image = Properties.Resources.carPink;
break;
case 6:
Car2.Image = Properties.Resources.TruckBlue;
break;
case 7:
Car2.Image = Properties.Resources.TruckWhite;
break;
case 8:
Car2.Image = Properties.Resources.ambulance;
break;



private void GameOver()

// TODO: move to GameModel.cs
trophy.Visible = true;

timer1.Stop();

button1.Enabled = true;

explosion.Visible = true;
Player.Controls.Add(explosion);
explosion.Location = new Point(-8, 5);
explosion.BackColor = Color.Transparent;
explosion.BringToFront();

if (score < 1000)
trophy.Image = Properties.Resources.bronze;

if (score > 2000)
trophy.Image = Properties.Resources.silver;

if (score > 3500)
trophy.Image = Properties.Resources.gold;


private void button1_Click(object sender, EventArgs e)

Reset();


private void roadTrack2_Click(object sender, EventArgs e)











share|improve this question



















  • MVC stands for Model-View-Controller. As long as these patterns aren't present in your code I would argue that it doesn't fit the MVC pattern :)
    – IEatBagels
    May 10 at 12:44










  • @TopinFrassi I see your point. Thanks
    – Burn_jacks
    May 10 at 12:56










  • I'd say analyze the code and write down every functionality that it has, then start from scratch applying the MVC pattern.
    – Denis
    May 10 at 14:19










  • @Denis That's exactly what i am trying to do ) starting from scratch ) thanx
    – Burn_jacks
    May 10 at 14:21






  • 1




    @Burn_jacks: There is a line of acceptability between refactoring coding style (which answers on CR often do) and refactoring the code and its design pattern. You're asking for the latter, to a degree where we'd be doing something that you haven't shown to grasp. That's out of scope for a code review. Your question is essentially asking us to do the MVC design for you. Read up on the individual responsibilities of a model, a view, and a controller. Then design classes with those responsibilities in mind. Then start carrying over the game logic.
    – Flater
    May 11 at 8:34













up vote
0
down vote

favorite









up vote
0
down vote

favorite











As a part of studying C#, I was given a naive Windows Forms game code. The task is to try and make it more MVC-like and optimize where possible.



I've already read what MVC is, but having difficulties what to start with. Here's the code itself, TODOs are just my thoughts.



I'm not asking to do my job, but I would love to get some kind of a set of instructions, any ideas are welcomed and appreciated thou. Thank you



To have an idea what a game looks like, check this image
a simple top-down car game



using System;
using System.Drawing;
using System.Windows.Forms;

namespace OldSchoolRacingGame

public partial class CarGame : Form

private int carSpeed = 5;
private int roadSpeed = 5;
private bool carLeft;
private bool carRight;
private int trafficSpeed = 5;
private int score = 0;
Random rnd = new Random();

public CarGame()

InitializeComponent();

Reset();


private void Reset()


trophy.Visible = false;
button1.Enabled = false;
explosion.Visible = false;
trafficSpeed = 5;
roadSpeed = 5;
score = 0;

Player.Left = 161; // placing player
Player.Top = 286;

carLeft = false;
carRight = false;

Car1.Left = 66; // placing obstacles
Car1.Top = -120;

Car2.Left = 294;
Car2.Top = -185;

roadTrack2.Left = -3; // placing background images
roadTrack2.Top = -222;
roadTrack1.Left = -2;
roadTrack1.Top = -638;

timer1.Start(); // timer starts


private void timer1_Tick(object sender, EventArgs e)


private void CarGame_Load(object sender, EventArgs e)

//TODO: ?


private void MoveCar(object sender, KeyEventArgs e)

if (e.KeyCode == Keys.Left && Player.Left > 0)
carLeft = true;

if (e.KeyCode == Keys.Right &&
Player.Left + Player.Width < panel1.Width)
carRight = true;


private void KeepCarInside(object sender, KeyEventArgs e)

if (e.KeyCode == Keys.Left)
carLeft = false;

if (e.KeyCode == Keys.Right)
carRight = false;


private void SwitchCar1Image()

//TODO: try to make a separate method based on RandomNumberGenerator
//TODO: move to GameModel.cs
int number = rnd.Next(1, 8);

switch (number)

case 1:
Car1.Image = Properties.Resources.carGreen;
break;
case 2:
Car1.Image = Properties.Resources.carOrange;
break;
case 3:
Car1.Image = Properties.Resources.carGrey;
break;
case 4:
Car1.Image = Properties.Resources.CarRed;
break;
case 5:
Car1.Image = Properties.Resources.carPink;
break;
case 6:
Car1.Image = Properties.Resources.TruckBlue;
break;
case 7:
Car1.Image = Properties.Resources.TruckWhite;
break;
case 8:
Car1.Image = Properties.Resources.ambulance;
break;




private void SwitchCar2Image()

// TODO: same as SwitchCar1Image()
int number = rnd.Next(1, 8);

switch (number)

case 1:
Car2.Image = Properties.Resources.carGreen;
break;
case 2:
Car2.Image = Properties.Resources.carOrange;
break;
case 3:
Car2.Image = Properties.Resources.carGrey;
break;
case 4:
Car2.Image = Properties.Resources.CarRed;
break;
case 5:
Car2.Image = Properties.Resources.carPink;
break;
case 6:
Car2.Image = Properties.Resources.TruckBlue;
break;
case 7:
Car2.Image = Properties.Resources.TruckWhite;
break;
case 8:
Car2.Image = Properties.Resources.ambulance;
break;



private void GameOver()

// TODO: move to GameModel.cs
trophy.Visible = true;

timer1.Stop();

button1.Enabled = true;

explosion.Visible = true;
Player.Controls.Add(explosion);
explosion.Location = new Point(-8, 5);
explosion.BackColor = Color.Transparent;
explosion.BringToFront();

if (score < 1000)
trophy.Image = Properties.Resources.bronze;

if (score > 2000)
trophy.Image = Properties.Resources.silver;

if (score > 3500)
trophy.Image = Properties.Resources.gold;


private void button1_Click(object sender, EventArgs e)

Reset();


private void roadTrack2_Click(object sender, EventArgs e)











share|improve this question











As a part of studying C#, I was given a naive Windows Forms game code. The task is to try and make it more MVC-like and optimize where possible.



I've already read what MVC is, but having difficulties what to start with. Here's the code itself, TODOs are just my thoughts.



I'm not asking to do my job, but I would love to get some kind of a set of instructions, any ideas are welcomed and appreciated thou. Thank you



To have an idea what a game looks like, check this image
a simple top-down car game



using System;
using System.Drawing;
using System.Windows.Forms;

namespace OldSchoolRacingGame

public partial class CarGame : Form

private int carSpeed = 5;
private int roadSpeed = 5;
private bool carLeft;
private bool carRight;
private int trafficSpeed = 5;
private int score = 0;
Random rnd = new Random();

public CarGame()

InitializeComponent();

Reset();


private void Reset()


trophy.Visible = false;
button1.Enabled = false;
explosion.Visible = false;
trafficSpeed = 5;
roadSpeed = 5;
score = 0;

Player.Left = 161; // placing player
Player.Top = 286;

carLeft = false;
carRight = false;

Car1.Left = 66; // placing obstacles
Car1.Top = -120;

Car2.Left = 294;
Car2.Top = -185;

roadTrack2.Left = -3; // placing background images
roadTrack2.Top = -222;
roadTrack1.Left = -2;
roadTrack1.Top = -638;

timer1.Start(); // timer starts


private void timer1_Tick(object sender, EventArgs e)


private void CarGame_Load(object sender, EventArgs e)

//TODO: ?


private void MoveCar(object sender, KeyEventArgs e)

if (e.KeyCode == Keys.Left && Player.Left > 0)
carLeft = true;

if (e.KeyCode == Keys.Right &&
Player.Left + Player.Width < panel1.Width)
carRight = true;


private void KeepCarInside(object sender, KeyEventArgs e)

if (e.KeyCode == Keys.Left)
carLeft = false;

if (e.KeyCode == Keys.Right)
carRight = false;


private void SwitchCar1Image()

//TODO: try to make a separate method based on RandomNumberGenerator
//TODO: move to GameModel.cs
int number = rnd.Next(1, 8);

switch (number)

case 1:
Car1.Image = Properties.Resources.carGreen;
break;
case 2:
Car1.Image = Properties.Resources.carOrange;
break;
case 3:
Car1.Image = Properties.Resources.carGrey;
break;
case 4:
Car1.Image = Properties.Resources.CarRed;
break;
case 5:
Car1.Image = Properties.Resources.carPink;
break;
case 6:
Car1.Image = Properties.Resources.TruckBlue;
break;
case 7:
Car1.Image = Properties.Resources.TruckWhite;
break;
case 8:
Car1.Image = Properties.Resources.ambulance;
break;




private void SwitchCar2Image()

// TODO: same as SwitchCar1Image()
int number = rnd.Next(1, 8);

switch (number)

case 1:
Car2.Image = Properties.Resources.carGreen;
break;
case 2:
Car2.Image = Properties.Resources.carOrange;
break;
case 3:
Car2.Image = Properties.Resources.carGrey;
break;
case 4:
Car2.Image = Properties.Resources.CarRed;
break;
case 5:
Car2.Image = Properties.Resources.carPink;
break;
case 6:
Car2.Image = Properties.Resources.TruckBlue;
break;
case 7:
Car2.Image = Properties.Resources.TruckWhite;
break;
case 8:
Car2.Image = Properties.Resources.ambulance;
break;



private void GameOver()

// TODO: move to GameModel.cs
trophy.Visible = true;

timer1.Stop();

button1.Enabled = true;

explosion.Visible = true;
Player.Controls.Add(explosion);
explosion.Location = new Point(-8, 5);
explosion.BackColor = Color.Transparent;
explosion.BringToFront();

if (score < 1000)
trophy.Image = Properties.Resources.bronze;

if (score > 2000)
trophy.Image = Properties.Resources.silver;

if (score > 3500)
trophy.Image = Properties.Resources.gold;


private void button1_Click(object sender, EventArgs e)

Reset();


private void roadTrack2_Click(object sender, EventArgs e)













share|improve this question










share|improve this question




share|improve this question









asked May 10 at 10:24









Burn_jacks

41




41











  • MVC stands for Model-View-Controller. As long as these patterns aren't present in your code I would argue that it doesn't fit the MVC pattern :)
    – IEatBagels
    May 10 at 12:44










  • @TopinFrassi I see your point. Thanks
    – Burn_jacks
    May 10 at 12:56










  • I'd say analyze the code and write down every functionality that it has, then start from scratch applying the MVC pattern.
    – Denis
    May 10 at 14:19










  • @Denis That's exactly what i am trying to do ) starting from scratch ) thanx
    – Burn_jacks
    May 10 at 14:21






  • 1




    @Burn_jacks: There is a line of acceptability between refactoring coding style (which answers on CR often do) and refactoring the code and its design pattern. You're asking for the latter, to a degree where we'd be doing something that you haven't shown to grasp. That's out of scope for a code review. Your question is essentially asking us to do the MVC design for you. Read up on the individual responsibilities of a model, a view, and a controller. Then design classes with those responsibilities in mind. Then start carrying over the game logic.
    – Flater
    May 11 at 8:34

















  • MVC stands for Model-View-Controller. As long as these patterns aren't present in your code I would argue that it doesn't fit the MVC pattern :)
    – IEatBagels
    May 10 at 12:44










  • @TopinFrassi I see your point. Thanks
    – Burn_jacks
    May 10 at 12:56










  • I'd say analyze the code and write down every functionality that it has, then start from scratch applying the MVC pattern.
    – Denis
    May 10 at 14:19










  • @Denis That's exactly what i am trying to do ) starting from scratch ) thanx
    – Burn_jacks
    May 10 at 14:21






  • 1




    @Burn_jacks: There is a line of acceptability between refactoring coding style (which answers on CR often do) and refactoring the code and its design pattern. You're asking for the latter, to a degree where we'd be doing something that you haven't shown to grasp. That's out of scope for a code review. Your question is essentially asking us to do the MVC design for you. Read up on the individual responsibilities of a model, a view, and a controller. Then design classes with those responsibilities in mind. Then start carrying over the game logic.
    – Flater
    May 11 at 8:34
















MVC stands for Model-View-Controller. As long as these patterns aren't present in your code I would argue that it doesn't fit the MVC pattern :)
– IEatBagels
May 10 at 12:44




MVC stands for Model-View-Controller. As long as these patterns aren't present in your code I would argue that it doesn't fit the MVC pattern :)
– IEatBagels
May 10 at 12:44












@TopinFrassi I see your point. Thanks
– Burn_jacks
May 10 at 12:56




@TopinFrassi I see your point. Thanks
– Burn_jacks
May 10 at 12:56












I'd say analyze the code and write down every functionality that it has, then start from scratch applying the MVC pattern.
– Denis
May 10 at 14:19




I'd say analyze the code and write down every functionality that it has, then start from scratch applying the MVC pattern.
– Denis
May 10 at 14:19












@Denis That's exactly what i am trying to do ) starting from scratch ) thanx
– Burn_jacks
May 10 at 14:21




@Denis That's exactly what i am trying to do ) starting from scratch ) thanx
– Burn_jacks
May 10 at 14:21




1




1




@Burn_jacks: There is a line of acceptability between refactoring coding style (which answers on CR often do) and refactoring the code and its design pattern. You're asking for the latter, to a degree where we'd be doing something that you haven't shown to grasp. That's out of scope for a code review. Your question is essentially asking us to do the MVC design for you. Read up on the individual responsibilities of a model, a view, and a controller. Then design classes with those responsibilities in mind. Then start carrying over the game logic.
– Flater
May 11 at 8:34





@Burn_jacks: There is a line of acceptability between refactoring coding style (which answers on CR often do) and refactoring the code and its design pattern. You're asking for the latter, to a degree where we'd be doing something that you haven't shown to grasp. That's out of scope for a code review. Your question is essentially asking us to do the MVC design for you. Read up on the individual responsibilities of a model, a view, and a controller. Then design classes with those responsibilities in mind. Then start carrying over the game logic.
– Flater
May 11 at 8:34
















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194090%2fideas-to-start-with-mvc-refactoring-of-windows-forms-simple-game-c%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194090%2fideas-to-start-with-mvc-refactoring-of-windows-forms-simple-game-c%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?