Advertisement
Guest User

Untitled

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