Note - Double Click to Copy Code Contact Us!

2D Drag Racing Game using Python

Tech Doubility
2D Drag Racing Game using Python


  1. import pygame
  2. import sys
  3. import random
  4.  
  5. # Initialize pygame
  6. pygame.init()
  7.  
  8. # Set up the screen
  9. screen_width = 800
  10. screen_height = 400
  11. screen = pygame.display.set_mode((screen_width, screen_height))
  12. pygame.display.set_caption("2D Drag Racing")
  13.  
  14. # Define colors
  15. WHITE = (255, 255, 255)
  16. GREEN = (0, 255, 0)
  17. RED = (255, 0, 0)
  18.  
  19. # Define the car
  20. car_width = 50
  21. car_height = 100
  22. car_x = 50
  23. car_y = screen_height // 2 - car_height // 2
  24. car_speed = 0
  25.  
  26. # Define the opponent car
  27. opponent_width = 50
  28. opponent_height = 100
  29. opponent_x = screen_width - 50 - opponent_width
  30. opponent_y = screen_height // 2 - opponent_height // 2
  31. opponent_speed = random.randint(5, 10)
  32.  
  33. # Define game variables
  34. score = 0
  35. clock = pygame.time.Clock()
  36.  
  37. # Load images
  38. car_image = pygame.image.load("car.png")
  39. car_image = pygame.transform.scale(car_image, (car_width, car_height))
  40. opponent_image = pygame.image.load("opponent_car.png")
  41. opponent_image = pygame.transform.scale(opponent_image, (opponent_width, opponent_height))
  42.  
  43. # Game loop
  44. while True:
  45. for event in pygame.event.get():
  46. if event.type == pygame.QUIT:
  47. pygame.quit()
  48. sys.exit()
  49.  
  50. # Handle key presses
  51. if event.type == pygame.KEYDOWN:
  52. if event.key == pygame.K_UP:
  53. car_speed = 5
  54. elif event.key == pygame.K_DOWN:
  55. car_speed = -5
  56.  
  57. # Handle key releases
  58. if event.type == pygame.KEYUP:
  59. if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
  60. car_speed = 0
  61.  
  62. # Update car position
  63. car_y += car_speed
  64.  
  65. # Update opponent car position
  66. opponent_y += opponent_speed
  67.  
  68. # Check for collisions
  69. if car_y < 0 or car_y + car_height > screen_height:
  70. score -= 10
  71. car_y = screen_height // 2 - car_height // 2
  72.  
  73. if opponent_y < 0 or opponent_y + opponent_height > screen_height:
  74. score += 10
  75. opponent_y = screen_height // 2 - opponent_height // 2
  76. opponent_speed = random.randint(5, 10)
  77.  
  78. # Check for overlap
  79. if car_x < opponent_x + opponent_width and car_x + car_width > opponent_x and car_y < opponent_y + opponent_height and car_y + car_height > opponent_y:
  80. score -= 50
  81. car_y = screen_height // 2 - car_height // 2
  82.  
  83. # Draw the background
  84. screen.fill(WHITE)
  85.  
  86. # Draw the car
  87. screen.blit(car_image, (car_x, car_y))
  88.  
  89. # Draw the opponent car
  90. screen.blit(opponent_image, (opponent_x, opponent_y))
  91.  
  92. # Draw the score
  93. font = pygame.font.SysFont(None, 30)
  94. score_text = font.render("Score: " + str(score), True, GREEN)
  95. screen.blit(score_text, (10, 10))
  96.  
  97. # Update the display
  98. pygame.display.flip()
  99.  
  100. # Set the frame rate
  101. clock.tick(60)
  102.  
Please note that this code assumes you have two images for the car and the opponent car, named "car.png" and "opponent_car.png" respectively, located in the same directory as the Python script. Also, make sure you have the Pygame library installed (pip install pygame) before running the code.

This is a simple implementation to get you started. You can enhance it by adding acceleration, sound effects, lap times, and other features according to your preferences

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.