Lives









How do I use Lives
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: UIgame.cs Copy
59     public void ShowLives(int lives)
60     {
61         if (lives <= 0)
62         {
63             lifeBar.enabled = false;
64             FailScreen.SetActive(true);
65             return;
66         }
67         else
68             lifeBar.sprite = hearts[lives - 1];
69     }
File name: PlayerControl.cs Copy
100     void OnTriggerEnter(Collider col)
101     {
102         if (col.tag == "Enemy" || col.tag == "Danger")
103         {
104             GetDamage();
105         }
106         if (col.tag == "Bonus")
107         {
108             sounds.PlaySoundsPlayer(3);
109             GetBonusEffect(col.GetComponent().thisBonus);
110             ui.ShowLives(lifeCurrent);
111             col.gameObject.SetActive(false);
112         }
113     }
File name: PlayerControl.cs Copy
115     public void GetDamage(int damage = 1)
116     {
117         if (immortal)
118             return;
119
120         immortal = true;
121
122         lifeCurrent-= damage;
123
124         ui.ShowLives(lifeCurrent);
125
126         if (lifeCurrent > 0)
127         {
128             sounds.PlaySoundsPlayer(1);
129             StartCoroutine("Immortality");
130         }
131         else
132         {
133             sounds.PlaySoundsPlayer(2);
134             Die();
135         }
136     }
File name: FloorScript.cs Copy
8     void OnCollisionEnter2D(Collision2D col)
9     {
10         if (col.gameObject.tag == "Ball")
11             gameManager.DecreaseLives();
12     }
File name: GameManager.cs Copy
27     void Update()
28     {
29         switch (CurrentGameState)
30         {
31             case GameState.Start:
32                 if (InputTaken())
33                 {
34                     statusText.text = string.Format("Lives: {0} Score: {1}", Lives, Score);
35                     CurrentGameState = GameState.Playing;
36                     Ball.StartBall();
37                 }
38                 break;
39             case GameState.Playing:
40                 break;
41             case GameState.Won:
42                 if (InputTaken())
43                 {
44                     Restart();
45                     Ball.StartBall();
46                     statusText.text = string.Format("Lives: {0} Score: {1}", Lives, Score);
47                     CurrentGameState = GameState.Playing;
48                 }
49                 break;
50             case GameState.LostALife:
51                 if (InputTaken())
52                 {
53                     Ball.StartBall();
54                     statusText.text = string.Format("Lives: {0} Score: {1}", Lives, Score);
55                     CurrentGameState = GameState.Playing;
56                 }
57                 break;
58             case GameState.LostAllLives:
59                 if (InputTaken())
60                 {
61                     Restart();
62                     Ball.StartBall();
63                     statusText.text = string.Format("Lives: {0} Score: {1}", Lives, Score);
64                     CurrentGameState = GameState.Playing;
65                 }
66                 break;
67             default:
68                 break;
69         }
70     }
File name: GameManager.cs Copy
72     private void Restart()
73     {
74         foreach (var item in blocks)
75         {
76             item.SetActive(true);
77             item.GetComponent().InitializeColor();
78         }
79         Lives = 3;
80         Score = 0;
81     }
File name: GameManager.cs Copy
84     public void DecreaseLives()
85     {
86         if (Lives > 0)
87             Lives--;
88
89         if(Lives == 0)
90         {
91             statusText.text = "Lost all lives. Tap to play again";
92             CurrentGameState = GameState.LostAllLives;
93         }
94         else
95         {
96             statusText.text = "Lost a life. Tap to continue";
97             CurrentGameState = GameState.LostALife;
98         }
99         Ball.StopBall();
100     }
File name: GameManager.cs Copy
111     {
112         Start,
113         Playing,
114         Won,
115         LostALife,
116         LostAllLives
117     }
File name: Program.cs Copy
31     static void Main()
32     {
33         double speed = 100.0;
34         double acceleration = 0.5;
35         int playfieldWidth = 5;
36         int livesCount = 5;
37         Console.BufferHeight = Console.WindowHeight = 20;
38         Console.BufferWidth = Console.WindowWidth = 30;
39         Object userCar = new Object();
40         userCar.x = 2;
41         userCar.y = Console.WindowHeight - 1;
42         userCar.c = '@';
43         userCar.color = ConsoleColor.Yellow;
44         Random randomGenerator = new Random();
45         List objects = new List();
46         while (true)
47         {
48             speed += acceleration;
49             if (speed > 400)
50             {
51                 speed = 400;
52             }
53
54             bool hitted = false;
55             {
56                 int chance = randomGenerator.Next(0, 100);
57                 if (chance < 10)
58                 {
59                     Object newObject = new Object();
60                     newObject.color = ConsoleColor.Cyan;
61                     newObject.c = '-';
62                     newObject.x = randomGenerator.Next(0, playfieldWidth);
63                     newObject.y = 0;
64                     objects.Add(newObject);
65                 }
66                 else if (chance < 20)
67                 {
68                     Object newObject = new Object();
69                     newObject.color = ConsoleColor.Cyan;
70                     newObject.c = '*';
71                     newObject.x = randomGenerator.Next(0, playfieldWidth);
72                     newObject.y = 0;
73                     objects.Add(newObject);
74                 }
75                 else
76                 {
77                     Object newCar = new Object();
78                     newCar.color = ConsoleColor.Green;
79                     newCar.x = randomGenerator.Next(0, playfieldWidth);
80                     newCar.y = 0;
81                     newCar.c = '#';
82                     objects.Add(newCar);
83                 }
84             }
85
86             while (Console.KeyAvailable)
87             {
88                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
89                 //while (Console.KeyAvailable) Console.ReadKey(true);
90                 if (pressedKey.Key == ConsoleKey.LeftArrow)
91                 {
92                     if (userCar.x - 1 >= 0)
93                     {
94                         userCar.x = userCar.x - 1;
95                     }
96                 }
97                 else if (pressedKey.Key == ConsoleKey.RightArrow)
98                 {
99                     if (userCar.x + 1 < playfieldWidth)
100                     {
101                         userCar.x = userCar.x + 1;
102                     }
103                 }
104             }
105             List newList = new List();
106             for (int i = 0; i < objects.Count; i++)
107             {
108                 Object oldCar = objects[i];
109                 Object newObject = new Object();
110                 newObject.x = oldCar.x;
111                 newObject.y = oldCar.y + 1;
112                 newObject.c = oldCar.c;
113                 newObject.color = oldCar.color;
114                 if (newObject.c == '*' && newObject.y == userCar.y && newObject.x == userCar.x)
115                 {
116                     speed -= 20;
117                 }
118                 if (newObject.c == '-' && newObject.y == userCar.y && newObject.x == userCar.x)
119                 {
120                     livesCount++;
121                 }
122                 if (newObject.c == '#' && newObject.y == userCar.y && newObject.x == userCar.x)
123                 {
124                     livesCount--;
125                     hitted = true;
126                     speed += 50;
127                     if (speed > 400)
128                     {
129                         speed = 400;
130                     }
131                     if (livesCount <= 0)
132                     {
133                         PrintStringOnPosition(8, 10, "GAME OVER!!!", ConsoleColor.Red);
134                         PrintStringOnPosition(8, 12, "Press [enter] to exit", ConsoleColor.Red);
135                         Console.ReadLine();
136                         Environment.Exit(0);
137                     }
138                 }
139                 if (newObject.y < Console.WindowHeight)
140                 {
141                     newList.Add(newObject);
142                 }
143             }
144             objects = newList;
145             Console.Clear();
146             if (hitted)
147             {
148                 objects.Clear();
149                 PrintOnPosition(userCar.x, userCar.y, 'X', ConsoleColor.Red);
150             }
151             else
152             {
153                 PrintOnPosition(userCar.x, userCar.y, userCar.c, userCar.color);
154             }
155             foreach (Object car in objects)
156             {
157                 PrintOnPosition(car.x, car.y, car.c, car.color);
158             }
159
160             // Draw info
161             PrintStringOnPosition(8, 4, "Lives: " + livesCount, ConsoleColor.White);
162             PrintStringOnPosition(8, 5, "Speed: " + speed, ConsoleColor.White);
163             PrintStringOnPosition(8, 6, "Acceleration: " + acceleration, ConsoleColor.White);
164             //Console.Beep();
165             Thread.Sleep((int)(600 - speed));
166         }
167     }

Lives 111 lượt xem

Gõ tìm kiếm nhanh...