Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 打地鼠{public partial class 打地鼠遊戲 : Form{private Button[] button = new System.Windows.Forms.Button[25];private Button[] ctrlBtn = new System.Windows.Forms.Button[3];private String[] cB_name = { "開始", "暫停", "重置" };private Timer timer1 = new System.Windows.Forms.Timer();private Timer timer2 = new System.Windows.Forms.Timer();private Random ran = new Random();private int score = new int();//分數private int time = 61;//倒數時間private int speed=2000;//碼表速度public 打地鼠遊戲(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){label1.Text = "";label2.Text = "";load();}private void load(){timer1 = new Timer();timer2 = new Timer();timer1.Tick += new System.EventHandler(timer1_Tick); timer2.Tick += new System.EventHandler(timer2_Tick);for (int i = 0; i < 25; i++) //動態產生button{button[i] = new Button();button[i].Size = new Size(60, 60); button[i].Click += new System.EventHandler(button_Click);this.Controls.Add(button[i]);if (i <5)button[i].Location = new System.Drawing.Point(60 * (i + 2), 20);if (i>=5&&i < 10)button[i].Location = new System.Drawing.Point(60 * (i%5 + 2), 80);if (i >= 10 && i < 15)button[i].Location = new System.Drawing.Point(60 * (i%5 + 2), 140);if (i >= 15 && i < 20)button[i].Location = new System.Drawing.Point(60 * (i%5 + 2), 200);if (i >= 20 && i < 25)button[i].Location = new System.Drawing.Point(60 * (i%5 + 2), 260);}for (int j = 0; j < 3; j++) {//動態產生ctrlBtnctrlBtn[j] = new Button();ctrlBtn[j].Text = cB_name[j];ctrlBtn[j].Size = new Size(80, 50);ctrlBtn[j].Location = new System.Drawing.Point(450, 50 * (j + 3));ctrlBtn[j].Click += new System.EventHandler(ctrlBtn_Click);this.Controls.Add(ctrlBtn[j]);}}private void button_Click(object sender, EventArgs e){int ilate=0;for(int i=0;i<25;i++){//確認"地鼠"為哪顆按鈕if (sender == button[i] && button[i].BackColor == Color.PapayaWhip){ilate=i;}}if (sender == button[ilate])//判別地鼠{score += 5;//按對加5分,速度變快50speed -= 50;button[ilate].BackColor = Color.LightGreen;}else if(sender!=button[ilate]){score -= 10;//按錯減10分,速度減慢200speed += 200;}label2.Text = "分數" + score.ToString();//label2.Text = "分數" + score.ToString()+"n速度:"+(timer1.Interval)*0.001+"s";} private void ctrlBtn_Click(object sender, EventArgs e){if (sender == ctrlBtn[0]) { start(); }if (sender == ctrlBtn[1]) { stop(); }if (sender == ctrlBtn[2]) { reset(); }} private void start()//遊戲開始{timer1.Start();timer1.Interval = 999;//因為timer不會馬上啟動,所以先設個緩衝時間timer2.Start();timer2.Interval = 1000;//此數字為1000/1000s(1s)}private void stop() //遊戲暫停{timer1.Stop();timer2.Stop();}private void reset() //重置遊戲{time = 61;score = 0;speed = 2000;label1.Text = "";label2.Text = "";for (int i = 0; i < 25; i++){button[i].BackColor = SystemColors.Control;}} private void timer1_Tick(object sender, EventArgs e)//產生"地鼠"{for (int i = 0; i < 25; i++){button[i].BackColor =Color.LightGreen; }button[ran.Next(0, 25)].BackColor = Color.PapayaWhip;//隨機挑選一個按鈕為"地鼠"if (speed >= 700)//以700為速度最快分界點{timer1.Interval = speed;}else if (timer1.Interval == 999) {//當緩衝時間過了(999),便會以原來速度下去跑timer1.Interval = speed;}else timer1.Interval = 700; //到這裡速度太快,便不再加速了(想要更快速度可以再自行做調整)} private void timer2_Tick(object sender, EventArgs e)//倒數計時器{if (time > 0) time--;else if (time <= 0){for (int i = 0; i < 25; i++){button[i].BackColor = SystemColors.Control;}timer1.Stop();timer2.Stop();MessageBox.Show("時間到囉!!nn您的分數為" + score.ToString());}label1.Text = time.ToString();}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement