Advertisement
Guest User

Tetris (form1.cs)

a guest
Dec 25th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace Tetris
  11. {
  12.     public partial class frm_Main : Form
  13.     {
  14.         const int spalten = 11;
  15.         const int zeilen = 22;
  16.         PictureBox[,] matrix = new PictureBox[spalten, zeilen];
  17.         int field_size = 20;
  18.         int stone_x = spalten/2;
  19.         int m_stone_y = 0;
  20.         int m_zeitspanne; //Wert für den Timertick, welcher angibt in welchen Sprüngen der Block nach unten fällt. (Je niedriger desto schneller)
  21.  
  22.         public frm_Main()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void Form1_Load(object sender, EventArgs e)
  28.         {
  29.             SpielfeldErstellen();
  30.             fall();
  31.         }
  32.  
  33.         public void fall()
  34.         {
  35.             m_zeitspanne = 100;
  36.             Timer t1 = new Timer();
  37.             t1.Interval = m_zeitspanne;
  38.             t1.Tick += new EventHandler(t1_Tick);
  39.             t1.Start();
  40.         }
  41.  
  42.         void t1_Tick(object sender, EventArgs e)
  43.         {
  44.             if (++m_stone_y < zeilen) //"++ston_y" - Wird vor der Abfrage um einen hochgezählt
  45.             {
  46.                 if (matrix[stone_x, m_stone_y].Image == null)
  47.                 {
  48.                     matrix[stone_x, (m_stone_y - 1 >= 0) ? m_stone_y - 1 : 0].Image = null;
  49.                     matrix[stone_x, m_stone_y].Image = new Bitmap("stein.jpg");
  50.                     matrix[stone_x, m_stone_y].SizeMode = PictureBoxSizeMode.StretchImage;
  51.                 }
  52.                 else
  53.                     m_stone_y = 0;
  54.             }
  55.             else
  56.                 m_stone_y = 0;
  57.         }
  58.  
  59.         public void SpielfeldErstellen()
  60.         {
  61.             for (int y = 0;y < zeilen;y++)
  62.             {
  63.                 for (int x = 0; x < spalten; x++)
  64.                 {
  65.                     matrix[x,y] = new PictureBox();
  66.                     matrix[x,y].Size = new System.Drawing.Size (field_size,field_size);
  67.                     matrix[x,y].Location = new System.Drawing.Point (x*field_size+1,y*field_size+1);
  68.                     this.Controls.Add (matrix [x,y]);
  69.                     matrix[x,y].Show();
  70.                 }
  71.             }
  72.         }
  73.  
  74.         void MainFormKeyDown(object sender, KeyEventArgs e)
  75.         {
  76.             int tmpstone_x = stone_x;
  77.             int tmpstone_y = m_stone_y;
  78.  
  79.             if (e.KeyData == Keys.Left)
  80.             {
  81.                 matrix[tmpstone_x, tmpstone_y].Image = null;
  82.                 if ((tmpstone_x - 1) > 0)
  83.                 {
  84.                     tmpstone_x--;
  85.                     stone_x = tmpstone_x;
  86.                 }
  87.                 else
  88.                 {
  89.                     tmpstone_x = 0;
  90.                 }
  91.             }
  92.  
  93.             if (e.KeyData == Keys.Right)
  94.             {
  95.                 matrix[tmpstone_x, tmpstone_y].Image = null;
  96.                 if ((tmpstone_x + 1) < 9)
  97.                 {
  98.                     tmpstone_x++;
  99.                     stone_x = tmpstone_x;
  100.                 }
  101.                 else
  102.                 {
  103.                     tmpstone_x = 0;
  104.                 }
  105.             }
  106.             if (e.KeyData == Keys.Escape) //Form wird geschlossenn, wenn man Escape drückt
  107.             {
  108.                 Application.Exit();
  109.             }
  110.  
  111.             stone_x = tmpstone_x;
  112.             m_stone_y = tmpstone_y;
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement