0% found this document useful (0 votes)
34 views

Using Using Using Using Using Using Using Using Using Namespace Public Partial Class Public

This document contains C# code that defines a PinPon game form. It initializes two timers, one that moves a picture box randomly and another that tracks time. It detects collisions between the picture box and a panel, changing the picture box direction. Keyboard inputs move the panel left and right. The timer counts time and ends the game after 30 seconds, displaying a win message.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Using Using Using Using Using Using Using Using Using Namespace Public Partial Class Public

This document contains C# code that defines a PinPon game form. It initializes two timers, one that moves a picture box randomly and another that tracks time. It detects collisions between the picture box and a panel, changing the picture box direction. Keyboard inputs move the panel left and right. The timer counts time and ends the game after 30 seconds, displaying a win message.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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 PracticaAplicadaI
{
public partial class PinPon : Form
{
public PinPon()
{
InitializeComponent();
}

private void PinPon_Load(object sender, EventArgs e)


{

timer1.Enabled = true;
timer2.Enabled = true;

Random r = new Random();


pictureBox1.Location = new Point(0, r.Next(this.Height));
}

bool movDer, movArrib;


private void timer1_Tick(object sender, EventArgs e)
{

if (movDer == true)
{
if (pictureBox1.Left <= this.ClientRectangle.Width -
pictureBox1.Width)
pictureBox1.Left += 2;
else
movDer = false;
}
else
{
if (pictureBox1.Left >= 0)
pictureBox1.Left -= 2;
else
movDer = true;
}

if (movArrib == true)
{
if (pictureBox1.Top >= 0)
pictureBox1.Top -= 2;
else
movArrib = false;
}
else
pictureBox1.Top += 2;

//rebote de la pelota
if (movArrib == true)
{
if (pictureBox1.Top >= 0)
pictureBox1.Top -= 2;
else
movArrib = false;
}
if (pictureBox1.Top + pictureBox1.Height >= panel1.Top &&
pictureBox1.Top + pictureBox1.Height <= panel1.Top + panel1.Height
&&
pictureBox1.Left + pictureBox1.Width >= panel1.Left &&
pictureBox1.Left + pictureBox1.Width <= panel1.Left + panel1.Width)
{
movArrib = true;
}
}

//Cronometro del juego


int i = 0;
private void timer2_Tick(object sender, EventArgs e)
{
i++;
if (i > 0 && i <= 9)
label1.Text = "Tiempo: " + "0" + i.ToString();
else
label1.Text = "Tiempo: " + i.ToString();

if (i >= 30)
{
timer1.Enabled = false;
timer2.Stop();
MessageBox.Show("Ganaste!!!");
}
}

//Movimiento del panel hacia la izquierda y derecha


private void PinPon_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left && panel1.Left > 0)
panel1.Left -= 7;
else if (e.KeyCode == Keys.Right && panel1.Left <
this.ClientRectangle.Width - panel1.Width)
panel1.Left += 7;
}
}
}

You might also like