Notes
![]() ![]() Notes - notes.io |
using UnityEngine.UI;
public class BlockBreaker : MonoBehaviour
{
public int score = 0;
public Text scoreText;
public Rigidbody2D ballPrefab;
public Transform ballSpawnPoint;
public float ballInitialSpeed = 5f;
public float paddleSpeed = 5f;
public Transform paddle;
public GameObject[] blocks;
public int numBlocks = 20;
public float blockSpacing = 0.5f;
public float blockMoveSpeed = 1f;
private Rigidbody2D ball;
private bool ballInPlay = false;
private bool gameOver = false;
void Start()
{
SpawnBall();
SpawnBlocks();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && !ballInPlay)
{
ball.velocity = new Vector2(ballInitialSpeed, ballInitialSpeed);
ballInPlay = true;
}
float horizontalInput = Input.GetAxis("Horizontal");
paddle.Translate(new Vector3(horizontalInput * paddleSpeed * Time.deltaTime, 0f, 0f));
// clamp paddle position to screen bounds
Vector3 paddlePosition = paddle.position;
paddlePosition.x = Mathf.Clamp(paddlePosition.x, -8f, 8f);
paddle.position = paddlePosition;
if (gameOver)
{
if (Input.GetKeyDown(KeyCode.Space))
{
UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name);
}
}
}
void FixedUpdate()
{
if (!gameOver)
{
// move blocks down
for (int i = 0; i < numBlocks; i++)
{
blocks[i].transform.Translate(new Vector3(0f, -blockMoveSpeed * Time.deltaTime, 0f));
if (blocks[i].transform.position.y < -5f)
{
// game over
gameOver = true;
ball.velocity = Vector2.zero;
ballInPlay = false;
scoreText.text = "Game Over! Press Space to Restart.";
return;
}
}
}
}
void SpawnBall()
{
ball = Instantiate(ballPrefab, ballSpawnPoint.position, Quaternion.identity);
}
void SpawnBlocks()
{
blocks = new GameObject[numBlocks];
float startX = -3f;
float startY = 3f;
for (int i = 0; i < numBlocks; i++)
{
blocks[i] = Instantiate(Resources.Load<GameObject>("Block"), new Vector3(startX + (i % 10) * blockSpacing, startY - (i / 10) * blockSpacing, 0f), Quaternion.identity);
}
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Block"))
{
Destroy(other.gameObject);
score += 10;
scoreText.text = "Score: " + score.ToString();
if (GameObject.FindGameObjectsWithTag("Block").Length == 0)
{
// level complete
gameOver = true;
ball.velocity = Vector2.zero;
ballInPlay = false;
scoreText.text = "Level Complete! Press Space to Restart.";
}
}
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team