Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.72 KB | None | 0 0
  1. import pygame
  2. import math
  3. import time
  4. from datetime import datetime
  5.  
  6. # to debug, we'll print to log
  7. ########## having issues with this, works on one machine but not another
  8. # import sys
  9. # sys.stdout = open('C:/breakout/output.txt', 'w')
  10. # print('test')
  11.  
  12.  
  13. # Define some colors
  14. BLACK = (0, 0, 0)
  15. WHITE = (255, 255, 255)
  16. OFFWHITE = (237, 240, 245)
  17. LIGHTBLUE = (73, 122, 204)
  18. GREEN = (0, 255, 0)
  19. RED = (255, 0, 0)
  20.  
  21. paddle_y_pos = 400
  22. size = [700, 500]
  23. points_dic = {1: WHITE, 2: GREEN, 3: RED}
  24. target_dic = {}
  25. reset = False
  26. lives = 1
  27. score = 0
  28.  
  29. def reset_game(screen):
  30. font = pygame.font.Font('freesansbold.ttf', 32)
  31. text1 = font.render('GAME OVER', True, WHITE, BLACK)
  32. text2 = font.render('A TO RESTART', True, WHITE, BLACK)
  33. text3 = font.render('X TO EXIT', True, WHITE, BLACK)
  34. text1_rect = text1.get_rect()
  35. text2_rect = text2.get_rect()
  36. text3_rect = text3.get_rect()
  37. screen.fill(BLACK)
  38. text1_rect.center = size[0]/2, size[1]/2 - 100
  39. text2_rect.center = size[0]/2, size[1]/2
  40. text3_rect.center = size[0]/2, size[1]/2 + 100
  41. screen.blit(text1, text1_rect)
  42. screen.blit(text2, text2_rect)
  43. screen.blit(text3, text3_rect)
  44. pygame.display.flip()
  45. return True
  46.  
  47. def print_score():
  48. font = pygame.font.Font('freesansbold.ttf', 10)
  49. score_text = font.render('{}'.format(score), True, WHITE, BLACK)
  50. score_text_rect = score_text.get_rect()
  51. score_text_rect.center = size[0]/2, 495
  52. screen.blit(score_text, score_text_rect)
  53.  
  54. def get_target_area(target_dic):
  55. # x0 leftmost, x1 rightmost
  56. # y0 topmost, y1 bottommost
  57. target_area = {}
  58. for key in target_dic:
  59. x0y0 = target_dic[key].x_pos, target_dic[key].y_pos
  60. x0y1 = target_dic[key].x_pos, target_dic[key].y_pos + target_dic[key].height
  61. x1y0 = target_dic[key].x_pos + target_dic[key].width, target_dic[key].y_pos
  62. x1y1 = target_dic[key].x_pos + target_dic[key].width, target_dic[key].y_pos + target_dic[key].height
  63. target_area[key] = (x0y0, x0y1, x1y0, x1y1)
  64. return target_area
  65.  
  66.  
  67. def initialize_screen(level=1):
  68. if level == 1:
  69. for i in range(6):
  70. target_dic[i] = Target(70 * i, 50, 2, i)
  71.  
  72.  
  73. def populate_screen(level=1):
  74. for key in target_dic:
  75. target_dic[key].draw(screen)
  76. pygame.draw.rect(screen, WHITE, [0, size[1] - 20, size[0], 10])
  77. for i in range(0, ball.lives - 1):
  78. pygame.draw.circle(screen, WHITE, (5 + 10*i, size[1] - 5), 5)
  79.  
  80.  
  81. class Target:
  82. def __init__(self, x_pos=0, y_pos=0, points=1, key=0, width=70, height=10):
  83. self.points = points
  84. self.x_pos = x_pos
  85. self.y_pos = y_pos
  86. self.width = width
  87. self.height = height
  88. self.color = points_dic[self.points]
  89. self.key = key
  90.  
  91. def draw(self, screen):
  92. pygame.draw.rect(screen, LIGHTBLUE, [self.x_pos, self.y_pos, self.width, self.height])
  93. pygame.draw.rect(screen, self.color, [self.x_pos + 2, self.y_pos + 2, self.width - 4, self.height - 4])
  94.  
  95. def collision(self):
  96. # print('collision! on key {}'.format(self.key))
  97. # print('upper x:{}, upper y:{}, lower x:{}, lower y:{}'.format(self.x_pos, self.y_pos, self.x_pos +
  98. # self.width, self.y_pos + self.height))
  99. self.points -= 1
  100. if self.points == 0:
  101. target_dic.pop(self.key)
  102. return
  103. self.color = points_dic[self.points]
  104.  
  105.  
  106. class Paddle:
  107. def __init__(self, x_pos=300, y_pos=paddle_y_pos, width=100, height=10, color=WHITE):
  108. self.x_pos = x_pos
  109. self.y_pos = y_pos
  110. self.width = width
  111. self.height = height
  112. self.color = color
  113. self.speed = 0
  114.  
  115. def draw(self, screen):
  116. pygame.draw.rect(screen, self.color, [self.x_pos, self.y_pos, self.width, self.height])
  117.  
  118. def move(self):
  119. self.x_pos = self.x_pos + self.speed
  120. if self.x_pos <= 0:
  121. self.x_pos = 0
  122. if self.x_pos >= 700 - self.width:
  123. self.x_pos = 700 - self.width
  124.  
  125. class Ball:
  126. def __init__(self, x_pos=200, y_pos=200, width=10, height=10, color=WHITE, speed=5, angle=.75*math.pi, lives=3):
  127. self.x_pos = x_pos
  128. self.y_pos = y_pos
  129. self.width = width
  130. self.height = height
  131. self.color = color
  132. self.speed = [speed*math.cos(angle), speed*math.sin(angle)]
  133. self.angle = angle
  134. self.lives = lives
  135.  
  136. def check_lose(self):
  137. return self.y_pos + self.height >= size[1] - 20
  138.  
  139. def reset(self):
  140. self.x_pos = 200
  141. self.y_pos = 200
  142. self.angle = .75*math.pi
  143.  
  144. def draw(self, screen):
  145. pygame.draw.rect(screen, self.color, [self.x_pos, self.y_pos, self.width, self.height])
  146.  
  147. def move(self):
  148. self.x_pos = self.x_pos + self.speed[0]*math.cos(self.angle)
  149. self.y_pos = self.y_pos + self.speed[1]*math.sin(self.angle)
  150. if self.x_pos <= 0:
  151. self.x_pos = 0
  152. if self.x_pos >= 700 - self.width:
  153. self.x_pos = 700 - self.width
  154. if self.x_pos >= 700 - self.width or self.x_pos <= 0:
  155. self.speed[0] = self.speed[0] * -1
  156. if self.y_pos <= 0:
  157. self.y_pos = 0
  158. if self.y_pos >= 500 - self.width:
  159. self.y_pos = 500 - self.width
  160. if self.y_pos >= 500 - self.width or self.y_pos <= 0:
  161. self.speed[1] = self.speed[1] * -1
  162.  
  163.  
  164. def check_paddle(paddle, ball):
  165. if ball.y_pos + ball.height >= paddle.y_pos:
  166. # if (paddle.x_pos - ball.width <= ball.x_pos <= paddle.x_pos + paddle.width) and ball.y_pos + ball.height >= paddle.y_pos:
  167. ball.speed[1] *= -1
  168.  
  169.  
  170. def check_target(target_area, ball):
  171. global score
  172. v, h = 1, 1
  173. collision = False
  174. # print('starting list: {}'.format('.'.join([str(x) for x in list(target_dic.keys())])))
  175. for key in list(target_dic.keys()):
  176. collision_0 = False
  177. if target_dic[key].x_pos - ball.width <= ball.x_pos <= target_dic[key].x_pos + ball.width + target_dic[
  178. key].width and (
  179. target_dic[key].y_pos - ball.height <= ball.y_pos <= target_dic[key].y_pos or
  180. target_dic[key].y_pos + target_dic[key].height - abs(ball.speed[1]) <= ball.y_pos <=
  181. target_dic[key].y_pos + target_dic[key].height):
  182. print('collision detected - vertical')
  183. print('ball location: x_pos = {}, y_pos = {}'.format(ball.x_pos, ball.y_pos))
  184. print('target location: x_pos = {}, y_pos = {}, width = {}, height = {}'.format(target_dic[key].x_pos,
  185. target_dic[key].y_pos,
  186. target_dic[key].width,
  187. target_dic[key].height))
  188. if target_dic[key].y_pos - ball.height <= ball.y_pos <= target_dic[key].y_pos:
  189. print('top')
  190. if target_dic[key].y_pos + target_dic[key].height - abs(ball.speed[1]) <= ball.y_pos <= target_dic[key].y_pos + target_dic[key].height:
  191. print('bottom')
  192. # print('test used to detect collision:')
  193. # print('(target_dic[key].x_pos - ball.width <= ball.x_pos <= target_dic[key].x_pos + paddle.width) and ball.y_pos <= target_dic[key].y_pos + target_dic[key].height')
  194. # print('subbed values:')
  195. # print('{} - {} <= {} <= {} + {}) and {} <= {} + {}'.format(target_dic[key].x_pos, ball.width, ball.x_pos, target_dic[key].x_pos, paddle.width, ball.y_pos, target_dic[key].y_pos, target_dic[key].height))
  196.  
  197. collision = True
  198. collision_0 = True
  199. score += 1
  200. v = -1
  201. if target_dic[key].y_pos - ball.height <= ball.y_pos <= target_dic[key].y_pos + target_dic[key].height + ball.height \
  202. and (target_dic[key].x_pos - ball.width <= ball.x_pos <= target_dic[key].x_pos
  203. or target_dic[key].x_pos + target_dic[key].width - abs(ball.speed[0]) <= ball.x_pos <= target_dic[key].x_pos + target_dic[key].width):
  204. print('collision detected - horizontal')
  205. print('ball location: x_pos = {}, y_pos = {}'.format(ball.x_pos, ball.y_pos))
  206. print('target location: x_pos = {}, y_pos = {}, width = {}, height = {}'.format(target_dic[key].x_pos,
  207. target_dic[key].y_pos,
  208. target_dic[key].width,
  209. target_dic[key].height))
  210. collision = True
  211. collision_0 = True
  212. score += 1
  213. h = -1
  214. # if target_dic[key].x_pos - ball.width <= ball.x_pos <= target_dic[key].x_pos:
  215. # print('1')
  216. # if target_dic[key].x_pos + target_dic[key].width - ball.width <= ball.x_pos <= target_dic[key].x_pos + target_dic[key].width:
  217. # print('2')
  218. if collision_0:
  219. print('removing target {}'.format(key))
  220. target_dic[key].collision()
  221. print('ending list: {}'.format('.'.join([str(x) for x in list(target_dic.keys())])))
  222. print('exiting out of target loop')
  223. return collision, v, h
  224.  
  225.  
  226. # Setup
  227.  
  228. pygame.init()
  229.  
  230. # Set the width and height of the screen [width,height]
  231.  
  232. screen = pygame.display.set_mode(size)
  233.  
  234. pygame.display.set_caption("My Game")
  235.  
  236. # Loop until the user clicks the close button.
  237. done = False
  238.  
  239. # Used to manage how fast the screen updates
  240. clock = pygame.time.Clock()
  241.  
  242. # Hide the mouse cursor
  243. pygame.mouse.set_visible(0)
  244.  
  245. paddle = Paddle()
  246. ball = Ball()
  247. initialize_screen(1)
  248.  
  249. # find target with max y_pos
  250. max_y_pos = 0
  251. for key in target_dic:
  252. if target_dic[key].y_pos > max_y_pos:
  253. max_y_pos = target_dic[key].y_pos + target_dic[key].height + max(20, target_dic[key].height)
  254.  
  255.  
  256. i = 0
  257. # -------- Main Program Loop -----------
  258. while not done:
  259. i += 1
  260. # --- Event Processing
  261. for event in pygame.event.get():
  262. if event.type == pygame.QUIT:
  263. done = True
  264. # User pressed down on a key
  265.  
  266. elif event.type == pygame.KEYDOWN:
  267. # Figure out if it was an arrow key. If so
  268. # adjust speed.
  269. if event.key == pygame.K_LEFT:
  270. paddle.speed = -6
  271. elif event.key == pygame.K_RIGHT:
  272. paddle.speed = 6
  273. if reset and event.key == pygame.K_a:
  274. ball.lives = lives
  275. reset = False
  276. lose = False
  277. score = 0
  278. ball.reset()
  279. if reset and event.key == pygame.K_x:
  280. done = True
  281.  
  282. # User let up on a key
  283. elif event.type == pygame.KEYUP:
  284. # If it is an arrow key, reset vector back to zero
  285. if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
  286. paddle.speed = 0
  287.  
  288. # --- Game Logic
  289. lose = ball.check_lose()
  290. if lose:
  291. time.sleep(1)
  292. ball.lives -= 1
  293. if ball.lives > 0:
  294. ball.reset()
  295. else:
  296. target_area = get_target_area(target_dic)
  297.  
  298. # Move the object according to the speed vector.
  299. paddle.move()
  300. # ball.move()
  301. result = [False]
  302. if ball.y_pos > paddle.y_pos - 20:
  303. check_paddle(paddle, ball)
  304. if ball.y_pos < max_y_pos:
  305. result = check_target(target_area, ball)
  306. print('ball location: x_pos = {}, y_pos = {}'.format(ball.x_pos, ball.y_pos))
  307. if result[0]:
  308. print('flipped')
  309. print(result)
  310. ball.speed[1] *= result[1]
  311. ball.speed[0] *= result[2]
  312. ball.move()
  313.  
  314.  
  315. # --- Drawing Code
  316.  
  317. # First, clear the screen to WHITE. Don't put other drawing commands
  318. # above this, or they will be erased with this command.
  319. screen.fill(BLACK)
  320. populate_screen(1)
  321. paddle.draw(screen)
  322. ball.draw(screen)
  323. print_score()
  324. if lose:
  325. reset = reset_game(screen)
  326. # Go ahead and update the screen with what we've drawn.
  327. pygame.display.flip()
  328.  
  329. # for debugging, we want to print
  330.  
  331. # if i % 2 == 0:
  332. # dateTimeObj = datetime.now()
  333. # timestampStr = dateTimeObj.strftime("%m_%d_%Y_%H_%M_%S_%f")
  334. # print('{}, x: {}, y: {}'.format(timestampStr, ball.x_pos, ball.y_pos))
  335. # pygame.image.save(screen, "C:/breakout/screenshot" + timestampStr + ".jpg")
  336.  
  337. # Limit frames per second
  338. clock.tick(60)
  339.  
  340. # Close the window and quit.
  341. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement