Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace Tetris
- {
- public partial class frm_Main : Form
- {
- const int spalten = 11;
- const int zeilen = 22;
- PictureBox[,] matrix = new PictureBox[spalten, zeilen];
- int field_size = 20;
- int stone_x = spalten/2;
- int m_stone_y = 0;
- int m_zeitspanne; //Wert für den Timertick, welcher angibt in welchen Sprüngen der Block nach unten fällt. (Je niedriger desto schneller)
- public frm_Main()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- SpielfeldErstellen();
- fall();
- }
- public void fall()
- {
- m_zeitspanne = 100;
- Timer t1 = new Timer();
- t1.Interval = m_zeitspanne;
- t1.Tick += new EventHandler(t1_Tick);
- t1.Start();
- }
- void t1_Tick(object sender, EventArgs e)
- {
- if (++m_stone_y < zeilen) //"++ston_y" - Wird vor der Abfrage um einen hochgezählt
- {
- if (matrix[stone_x, m_stone_y].Image == null)
- {
- matrix[stone_x, (m_stone_y - 1 >= 0) ? m_stone_y - 1 : 0].Image = null;
- matrix[stone_x, m_stone_y].Image = new Bitmap("stein.jpg");
- matrix[stone_x, m_stone_y].SizeMode = PictureBoxSizeMode.StretchImage;
- }
- else
- m_stone_y = 0;
- }
- else
- m_stone_y = 0;
- }
- public void SpielfeldErstellen()
- {
- for (int y = 0;y < zeilen;y++)
- {
- for (int x = 0; x < spalten; x++)
- {
- matrix[x,y] = new PictureBox();
- matrix[x,y].Size = new System.Drawing.Size (field_size,field_size);
- matrix[x,y].Location = new System.Drawing.Point (x*field_size+1,y*field_size+1);
- this.Controls.Add (matrix [x,y]);
- matrix[x,y].Show();
- }
- }
- }
- void MainFormKeyDown(object sender, KeyEventArgs e)
- {
- int tmpstone_x = stone_x;
- int tmpstone_y = m_stone_y;
- if (e.KeyData == Keys.Left)
- {
- matrix[tmpstone_x, tmpstone_y].Image = null;
- if ((tmpstone_x - 1) > 0)
- {
- tmpstone_x--;
- stone_x = tmpstone_x;
- }
- else
- {
- tmpstone_x = 0;
- }
- }
- if (e.KeyData == Keys.Right)
- {
- matrix[tmpstone_x, tmpstone_y].Image = null;
- if ((tmpstone_x + 1) < 9)
- {
- tmpstone_x++;
- stone_x = tmpstone_x;
- }
- else
- {
- tmpstone_x = 0;
- }
- }
- if (e.KeyData == Keys.Escape) //Form wird geschlossenn, wenn man Escape drückt
- {
- Application.Exit();
- }
- stone_x = tmpstone_x;
- m_stone_y = tmpstone_y;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement