Skip to content

Commit b44427d

Browse files
Merge pull request #2616 from Shikhar9425/master-2
Create Drag.py
2 parents ee6d569 + 9e4d714 commit b44427d

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

Drag Racing Game/Drag.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import time
2+
3+
class Car:
4+
def __init__(self, name, top_speed, acceleration):
5+
self.name = name
6+
self.top_speed = top_speed
7+
self.acceleration = acceleration
8+
self.current_speed = 0
9+
self.gear = 1
10+
11+
def accelerate(self):
12+
if self.gear == 1:
13+
rpm = self.current_speed / self.top_speed
14+
else:
15+
rpm = self.current_speed / (self.top_speed * (self.gear - 1))
16+
self.current_speed += self.acceleration * self.gear
17+
print(f"{self.name}: {self.current_speed:.2f} km/h (Gear: {self.gear}, RPM: {rpm:.2f})")
18+
19+
def shift_gear(self):
20+
if self.gear < 5:
21+
self.gear += 1
22+
23+
def race(self):
24+
print(f"{self.name} is racing!")
25+
while self.current_speed < self.top_speed:
26+
time.sleep(0.5)
27+
self.accelerate()
28+
if self.current_speed >= self.top_speed * 0.8:
29+
self.shift_gear()
30+
31+
if __name__ == "__main__":
32+
car1 = Car("Player Car", top_speed=200, acceleration=20)
33+
car2 = Car("AI Car", top_speed=180, acceleration=18)
34+
35+
car1.race()
36+
print("\n")
37+
car2.race()

Drag Racing Game/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Package/Script Name: Drag Racing AI
2+
3+
Short description of package/script: This is a simple Python script that simulates a Drag Racing AI game. It allows the user to race against an AI opponent. The AI will automatically shift gears based on the engine's RPM to achieve the fastest acceleration.
4+
5+
Functionalities/Scripts:
6+
7+
Car class: Represents a car in the Drag Racing game and includes methods for acceleration, shifting gears, and simulating the race.
8+
Setup Instructions:
9+
10+
Make sure you have Python installed on your system.
11+
Download the Drag Racing AI script from the provided source.
12+
Save the script in a directory of your choice.
13+
Explain how to set up and run your package/script on the user's system:
14+
15+
Open a terminal or command prompt.
16+
Navigate to the directory where you saved the Drag Racing AI script using the cd command.
17+
Run the script using the command: python drag_racing_ai.py
18+
Detailed Explanation:
19+
The script defines a Car class to represent the car in the Drag Racing game. The car has attributes like name, top speed, acceleration, current speed, and gear. The accelerate() method calculates the new speed based on the gear and acceleration and updates the current speed. The shift_gear() method increases the gear by one if the current gear is less than 5 (the maximum gear). The race() method simulates the race, continuously accelerating the car until it reaches its top speed while automatically shifting gears for optimal performance.
20+
21+
Output:
22+
The output of the script will be displayed in the terminal or command prompt. It will show the current speed, gear, and RPM of both the player's and AI's cars during the race. The race will continue until one of the cars reaches its top speed.
23+
24+
Author(s):
25+
Author:Shikhar9425

0 commit comments

Comments
 (0)