Advertisement
Guest User

putt putt game python

a guest
Jul 9th, 2019
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. import pygame
  2. import time
  3.  
  4. pygame.init()
  5.  
  6. orange = (255, 165, 0)
  7. red = (255, 0, 0)
  8. green = (0, 150, 0)
  9. white = (255, 255, 255)
  10. black = (0, 0, 0)
  11.  
  12. wind_width = 1200
  13. wind_height = 700
  14.  
  15. # Changeable variables
  16. wall_width = 10
  17. wall_length = 50
  18.  
  19. ball_size = 7
  20.  
  21. speed = 3
  22.  
  23. # Pygame setup
  24. gameDisplay = pygame.display.set_mode((wind_width, wind_height))
  25. pygame.display.set_caption("Mini Golf!")
  26.  
  27. pygame.display.update()
  28.  
  29. gameExit = False
  30.  
  31. clock = pygame.time.Clock()
  32.  
  33. # Class for vertical walls
  34. class Vwall:
  35.  
  36. def __init__(self,x,y):
  37. self.x = x
  38. self.y = y
  39. self.type = "vwall"
  40. # math to get all walls lined up right
  41. self.rect = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y, wall_width, wall_length)
  42.  
  43. # Draws walls based on the rect predefined
  44. def render(self):
  45. pygame.draw.rect(gameDisplay, orange, self.rect)
  46.  
  47. # Class for horizontal walls
  48. class Hwall:
  49.  
  50. def __init__(self,x,y):
  51. self.x = x
  52. self.y = y
  53. self.type = "hwall"
  54. self.rect = pygame.Rect(self.x, self.y + (wall_length / 2) - (wall_width / 2), wall_length, wall_width)
  55.  
  56. def render(self):
  57. pygame.draw.rect(gameDisplay, orange, self.rect)
  58.  
  59. # Class for corner pieces
  60. class Corner:
  61.  
  62. def __init__(self, x, y, orien):
  63. self.x = x
  64. self.y = y
  65. self.type = "corner"
  66. # orientation tells which was the corner faces
  67. self.orien = orien
  68. # 2 rects needed to make the corner, and 4 possible corner alingments
  69. if self.orien == 0:
  70. self.rect_h = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y + (wall_length / 2) - (wall_width / 2), (wall_length / 2) + wall_width, wall_width)
  71. self.rect_v = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y, wall_width, (wall_length / 2) + (wall_width / 2))
  72.  
  73. elif self.orien == 1:
  74. self.rect_h = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y + (wall_length / 2) - (wall_width / 2), wall_width, (wall_length / 2) + wall_width)
  75. self.rect_v = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y + (wall_length / 2) - (wall_width / 2), (wall_length / 2) + wall_width, wall_width)
  76.  
  77. elif self.orien == 2:
  78. self.rect_h = pygame.Rect(self.x, self.y + (wall_length / 2) - (wall_width / 2), (wall_length / 2) + (wall_width / 2), wall_width)
  79. self.rect_v = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y + (wall_length / 2) - (wall_width / 2), wall_width, (wall_length / 2) + wall_width)
  80.  
  81. elif self.orien == 3:
  82. self.rect_h = pygame.Rect(self.x, self.y + (wall_length / 2) - (wall_width / 2), (wall_length / 2) + (wall_width / 2), wall_width)
  83. self.rect_v = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y, wall_width, (wall_length / 2) + (wall_width / 2))
  84.  
  85. def render(self):
  86. pygame.draw.rect(gameDisplay, red, self.rect_h)
  87. pygame.draw.rect(gameDisplay, red, self.rect_v)
  88.  
  89. # Class for the golf ball
  90. class Ball:
  91.  
  92. def __init__(self, x, y):
  93. self.x = x
  94. self.y = y
  95. self.type = "ball"
  96. # determines direction of ball
  97. self.x_change = 0
  98. self.y_change = 0
  99. self.rect = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y + (wall_length / 2) - (wall_width / 2), ball_size, ball_size)
  100.  
  101. # draws ball and updates rectangle to new position each frame
  102. def render(self):
  103. self.rect = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y + (wall_length / 2) - (wall_width / 2), ball_size, ball_size)
  104. pygame.draw.rect(gameDisplay, white, self.rect)
  105.  
  106. # level design
  107. # H is a horizontal wall
  108. # V is a vertical wall
  109. # O is nothing but a placeholder
  110. # 1, 2, 3, and 4 are corners with different orientations
  111. # B is where the ball starts
  112. level = [
  113. ["1","H","H","H","H","2"],
  114. ["V","O","O","O","O","V"],
  115. ["V","O","O","O","O","V"],
  116. ["V","O","O","1","H","3"],
  117. ["V","O","O","V"],
  118. ["V","O","O","V"],
  119. ["V","O","O","V"],
  120. ["V","O","O","V"],
  121. ["V","O","O","V"],
  122. ["V","O","B","V"],
  123. ["0","H","H","3"]
  124. ]
  125.  
  126. # list for all wall objects
  127. walls = []
  128.  
  129. # translates the level into walls and corners stored in the walls list
  130. for y in range(len(level)):
  131. for x in range(len(level[y])):
  132. piece = level[y][x]
  133. if piece == "V":
  134. new_wall = Vwall(x * wall_length, y * wall_length)
  135. walls.append(new_wall)
  136. elif piece == "H":
  137. new_wall = Hwall(x * wall_length, y * wall_length)
  138. walls.append(new_wall)
  139. elif piece == "0" or piece == "1" or piece == "2" or piece == "3":
  140. new_wall = Corner(x * wall_length, y * wall_length, int(piece))
  141. walls.append(new_wall)
  142. elif piece == "B":
  143. ball = Ball(x * wall_length, y * wall_length)
  144.  
  145. # game loop
  146. while not(gameExit):
  147.  
  148. # events
  149. for event in pygame.event.get():
  150. if event.type == pygame.QUIT:
  151. gameExit = True
  152. # Starts moving ball when the mouse is clicked
  153. if event.type == pygame.MOUSEBUTTONUP:
  154. ball.x_change = -(speed)
  155. ball.y_change = -(speed)
  156.  
  157. gameDisplay.fill(green)
  158.  
  159. # draws walls
  160. for wall in walls:
  161. wall.render()
  162.  
  163. # Move in x direction
  164. ball.x += ball.x_change
  165. # Check for collision
  166. for wall in walls:
  167. if wall.type == "vwall":
  168. if wall.rect.colliderect(ball.rect):
  169. # If colliding with a vertical wall, undo the last two frames of movement (more than neccesary), and make the ball move in the opposite vertical direction
  170. ball.x -= ball.x_change * 2
  171. ball.x_change = -(ball.x_change)
  172. # same thing but for corners
  173. elif wall.type == "corner":
  174. if wall.rect_v.colliderect(ball.rect):
  175. ball.x -= ball.x_change * 2
  176. ball.x_change = -(ball.x_change)
  177.  
  178. # move in y direction
  179. ball.y += ball.y_change
  180. # check for collision
  181. for wall in walls:
  182. if wall.type == "hwall":
  183. if wall.rect.colliderect(ball.rect):
  184. # If colliding with a horizontal wall, undo the last two frames of movement (more than neccesary), and make the ball move in the opposite horizontal direction
  185. ball.y -= ball.y_change * 2
  186. ball.y_change = -(ball.y_change)
  187. # same thing for corners
  188. elif wall.type == "corner":
  189. if wall.rect_h.colliderect(ball.rect):
  190. ball.y -= ball.y_change * 2
  191. ball.y_change = -(ball.y_change)
  192.  
  193. ball.render()
  194.  
  195. pygame.display.update()
  196.  
  197. clock.tick(100)
  198.  
  199. pygame.quit()
  200. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement