Guest User

Untitled

a guest
Jul 18th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. import pygame, math
  2. pygame.init()
  3.  
  4. width = 700
  5. height = 700
  6. window = pygame.display.set_mode((width, height), pygame.NOFRAME)
  7.  
  8. pygame.display.set_caption("car game")
  9. img = pygame.image.load("car7_red.png")
  10.  
  11. class black:
  12. def __init__(self, x, y, height, width, color):
  13. self.x = x
  14. self.y = y
  15. self.height = height
  16. self.width = width
  17. self.color = color
  18. self.blackimage = pygame.image.load("downwhite.png")
  19. self.blackimage = pygame.transform.scale(self.blackimage, (self.blackimage.get_width() //2,self.blackimage.get_height()//2))
  20. self.rect = pygame.Rect(x, y, height, width) def draw(self):
  21. self.rect.topleft = (self.x, self.y) window.blit(self.blackimage, self.rect)
  22.  
  23. class carline:
  24. def __init__(self, x, y, height, width, color):
  25. self.x = x self.y = y self.height = height self.width = width self.color = color self.image = pygame.image.load("downs.png") self.rect = pygame.Rect(x, y, height, width) def draw(self):
  26. self.rect.topleft = (self.x, self.y) window.blit(self.image, self.rect)
  27.  
  28. class hole:
  29. def __init__(self, x, y, height, width, color):
  30. self.x = x self.y = y self.height = height self.width = width self.color = color self.holeimage = pygame.image.load("hole.png") self.rect = pygame.Rect(x, y, height, width) def draw(self):
  31. self.rect.topleft = (self.x, self.y) pygame.draw.rect(window, self.color, self.rect, 2)
  32.  
  33. white = (255, 255, 255) hole1 = hole(-10, 500, 468, 60, white)
  34.  
  35. class movecar:
  36. def __init__(self, x, y, height, width, color):
  37. self.x = x self.y = y self.height = height self.width = width self.color = color self.speed = 3 self.carimage = pygame.image.load("car12.png") self.rect = pygame.Rect(x, y, height, width) def draw(self):
  38. self.rect.topleft = (self.x, self.y) window.blit(self.carimage, self.rect)
  39.  
  40. class movecar2:
  41. def __init__(self, x, y, height, width, color):
  42. self.x = x self.y = y self.height = height self.width = width self.color = color self.speed = 3 self.carimage = pygame.image.load("car23.png") self.rect = pygame.Rect(x, y, height, width) self.hitbox = (self.x + 17, self.y + 11, 29, 52)# NEW def draw(self):
  43. self.rect.topleft = (self.x, self.y) window.blit(self.carimage, self.rect)
  44.  
  45. white = (255, 255, 255) car2 = movecar(514, 628, 10, 10, white) car3 = movecar2(389, -100, 10, 10, white)
  46.  
  47. class line4:
  48. def __init__(self, x, y, height, width, color):
  49. self.x = x self.y = y self.height = height self.width = width self.color = color self.image = pygame.image.load("lineup45.png") self.rect = pygame.Rect(x, y, height, width)
  50.  
  51. def draw(self):
  52. self.rect.topleft = (self.x, self.y) window.blit(self.image, self.rect)
  53.  
  54. # -- -- -- -- -- --
  55. lineup1 = []
  56.  
  57. #-- -- -- -- -- --
  58.  
  59. blacks = [] carlines = [] platformGroup = pygame.sprite.Group
  60.  
  61. level = [
  62. " s d s",
  63. " v s d s",
  64. " s d s",
  65. " s d s",
  66. " s d s",
  67. " s d s",
  68. " s d s",
  69. " s d s",
  70. " s d s",
  71. " s d s",
  72. " s d s",
  73. " s d s",
  74. " s d s",
  75. " s d s",
  76. " s d s",
  77. " s d s",
  78. " s d s",
  79. " s d s"
  80. ]
  81. for iy, row in enumerate(level):
  82. for ix, col in enumerate(row):
  83. if col == "d":
  84. new_platforms = black(ix * 9.8, iy * 50, 10, 10, (255, 255, 255)) blacks.append(new_platforms)
  85.  
  86. for iy, row in enumerate(level):
  87. for ix, col in enumerate(row):
  88. if col == "s":
  89. new_platforms = carline(ix * 9.8, iy * 50, 10, 10, (255, 255, 255)) carlines.append(new_platforms)
  90.  
  91. # === === === === === === === === === === === = Line Ups
  92. for iy, row in enumerate(level):
  93. for ix, col in enumerate(row):
  94. if col == "v":
  95. new_platforms = line4(ix * -10, iy * -120, 10, 10, (255, 255, 255)) lineup1.append(new_platforms)
  96.  
  97. # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
  98.  
  99. class Car:
  100. def __init__(self, x, y, height, width, color):
  101. self.x = x - width / 2 self.y = y - height / 2 self.height = height self.width = width self.color = color self.rect = pygame.Rect(x, y, height, width) self.surface = pygame.Surface((height, width), pygame.SRCALPHA) self.surface.blit(img, (0, 0)) self.angle = 250 self.speed = 0# 2
  102.  
  103. self.hitbox = (self.x + 90, self.y + 620, 370, 63)# NEW pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)
  104.  
  105. def draw(self): #3 self.rect.topleft = (int(self.x), int(self.y)) rotated = pygame.transform.rotate(self.surface, self.angle) surface_rect = self.surface.get_rect(topleft = self.rect.topleft) new_rect = rotated.get_rect(center = surface_rect.center) window.blit(rotated, new_rect.topleft) self.hitbox = (self.x, self.y, 70, 40)# NEW pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)
  106.  
  107. white = (255, 255, 255) car1 = Car(100, 630, 73, 73, white)# 4 clock = pygame.time.Clock()
  108.  
  109. # ReDrawEvertyhing def redraw():
  110.  
  111. for carline in carlines:
  112. carline.draw()
  113.  
  114. for black in blacks:
  115. black.draw()
  116.  
  117. # line ups
  118. for line1 in lineup1:
  119. line1.draw()
  120.  
  121. hole1.draw()
  122.  
  123. car1.draw() car3.draw()# moving car car2.draw()
  124.  
  125. def movingcars(): #moving cars car2.y -= car2.speed
  126. if car2.y < -450:
  127. car2.y = 760 car3.y += car3.speed
  128. if car3.y > 450:
  129. car3.y = -390
  130.  
  131. runninggame = True
  132. while runninggame:
  133. for event in pygame.event.get():
  134. if event.type == pygame.QUIT:
  135. runninggame = False
  136.  
  137. movingcars()
  138.  
  139. for lineup in lineup1:
  140. if hole1.rect.colliderect(car1.hitbox):
  141. car1.speed -= 1
  142.  
  143. pressed = pygame.key.get_pressed() car1.speed *= 0.8# 5
  144. if pressed[pygame.K_UP]: car1.speed += 0.5# 6
  145. if pressed[pygame.K_DOWN]: car1.speed -= 0.5# 6
  146.  
  147. if pressed[pygame.K_LEFT]: car1.angle += car1.speed / 2# 7
  148. if pressed[pygame.K_RIGHT]: car1.angle -= car1.speed / 2# 7 car1.x -= car1.speed * math.sin(math.radians(car1.angle))# 8 car1.y -= car1.speed * math.cos(math.radians(-car1.angle))# 8
  149.  
  150. window.fill((61, 61, 61))# 9
  151.  
  152. redraw()
  153.  
  154. pygame.display.flip() clock.tick(60)# 10 pygame.quit()
Add Comment
Please, Sign In to add comment