flappy bird

This commit is contained in:
2026-03-28 09:31:02 +01:00
parent 11575ef9b1
commit a0a675676d
4 changed files with 225 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 078ea16f7b9620d4f839c1d44f968b45
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,214 @@
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class FlappyBirdAllInOne : MonoBehaviour, ITask
{
[Header("Player")]
public Rigidbody2D rb;
public float jumpForce = 5f;
public bool isDead = false;
[Header("Pipes")]
public GameObject pipePrefab;
public Transform spawnPoint;
public float spawnRate = 2f;
public float heightOffset = 2f;
public float pipeSpeed = 2f;
private float timer = 0;
[Header("UI")]
public TextMeshProUGUI scoreText;
public GameObject gameOverPanel;
private int score = 0;
private Action<ITask> _onCompleted;
private Action<ITask> _onExit;
public string TaskID { get; set; }
public TaskType TaskType { get; set; }
public string TaskName { get; set; }
public (double, double) TaskLocation { get; set; }
public bool IsCompleted { get; private set; }
void Start()
{
Time.timeScale = 1f;
score = 0;
UpdateScore();
}
void Update()
{
if (isDead) return;
HandleInput();
HandleSpawning();
}
void HandleInput()
{
if (Input.GetMouseButtonDown(0))
{
Jump();
}
}
void Jump()
{
rb.velocity = Vector2.zero;
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
void HandleSpawning()
{
timer += Time.deltaTime;
if (timer >= spawnRate)
{
SpawnPipe();
timer = 0;
}
}
void SpawnPipe()
{
float yOffset = UnityEngine.Random.Range(-heightOffset, heightOffset);
GameObject pipe = Instantiate(
pipePrefab,
spawnPoint.position + new Vector3(0, yOffset, 0),
Quaternion.identity
);
pipe.AddComponent<PipeMover>().Init(pipeSpeed, this);
}
public void AddScore()
{
score++;
UpdateScore();
}
void UpdateScore()
{
scoreText.text = score.ToString();
}
public void GameOver()
{
isDead = true;
gameOverPanel.SetActive(true);
Time.timeScale = 0f;
}
public void Restart()
{
Time.timeScale = 1f;
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
private void OnCollisionEnter2D(Collision2D collision)
{
GameOver();
}
public void Initialize(Action<ITask> onCompleted)
{
IsCompleted = false;
_onCompleted = onCompleted;
}
public void Complete()
{
if (IsCompleted) return;
IsCompleted = true;
_onCompleted?.Invoke(this);
ExitTask(_onExit);
}
public void ExitTask(Action<ITask> onExit)
{
onExit?.Invoke(this);
}
}
public class PipeMover : MonoBehaviour
{
private float speed;
private FlappyBirdAllInOne game;
public void Init(float moveSpeed, FlappyBirdAllInOne gm)
{
speed = moveSpeed;
game = gm;
}
void Update()
{
transform.position += Vector3.left * speed * Time.deltaTime;
if (transform.position.x < -10f)
{
Destroy(gameObject);
}
}
}
public class ScoreTrigger : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
FlappyBirdAllInOne gm = FindObjectOfType<FlappyBirdAllInOne>();
if (collision.CompareTag("Player") && gm != null)
{
gm.AddScore();
}
}
}
// =====================
// navod pro desing t<>m
// =====================
/*
1. Player:
- Sprite + Rigidbody2D (Gravity ~2-3)
- BoxCollider2D
- PlayerController script
- Tag = Player
2. Pipes:
- Prefab se 2 kolidery (top/bottom)
- Mezera mezi nimi
- PipeMove script
3. Score Zone:
- Trigger collider mezi trubkami
- ScoreZone script
4. Spawner:
- Empty GameObject
- PipeSpawner script
5. UI:
- TextMeshPro pro score
- GameOver panel + restart button
6. Mobile:
- Input.GetMouseButtonDown funguje i na tap
*/

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 19096191e142d154e956c7169cca9a1e

View File

@@ -12,6 +12,7 @@ public class DraggableKey : MonoBehaviour,
public string correctSlotID; public string correctSlotID;
public string previousSceneName; public string previousSceneName;
[Header("Visual")] [Header("Visual")]
public Color wrongAttemptColor = Color.red; public Color wrongAttemptColor = Color.red;
public float blinkDuration = 0.2f; public float blinkDuration = 0.2f;