Guest User

Untitled

a guest
Jun 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. import math
  2. import pygame
  3. import random
  4. pygame.init()
  5. screen = pygame.display.set_mode([800,600])
  6. done = False
  7. clock = pygame.time.Clock()
  8. #DEFINE COLORS
  9. WHITE = (255,255,255)
  10. BLUE=(0,102,204)
  11. LIGHT_BLUE=(0,0,204)
  12. pink=(238,130,238)
  13. #import images
  14. #lives = pygame.image.load("heart.png")
  15. # initialize font; must be called after 'pygame.init()' to avoid 'Font not Initialized' error
  16. myfont = pygame.font.SysFont("monospace", 15)
  17. #var carying the space of the game
  18. top = -100
  19. left= -50
  20. right= -750
  21. bottom= -600
  22. angle = math.radians(random.randint(-140,-30))
  23.  
  24. class game_screen():
  25. def draw_screen():
  26. pygame.draw.rect(screen,BLUE,(50,100,700,600))
  27. def save_button():
  28. pygame.draw.rect(screen,pink,(500,20,50,30),0)
  29. save = "Save"
  30. label = myfont.render(save, 40, (0,0,0))
  31. screen.blit(label, (505, 20))
  32. def quit_button():
  33. pygame.draw.rect(screen,pink,(600,20,50,30),0)
  34. quit1 = "Quit"
  35. label = myfont.render(quit1, 40, (0,0,0))
  36. screen.blit(label, (605, 20))
  37.  
  38. mX, mY = pygame.mouse.get_pos()
  39. mouseButtons = pygame.mouse.get_pressed()
  40. if mouseButtons[0] == True:
  41. if (mX in range (600-20,600+20) and mY in range (20-20,20+20)):
  42. pygame.quit()
  43. #def display_lives():
  44. #lives_counter = screen.blit(lives,(50,30))
  45.  
  46. #lives_counter2 = screen.blit(lives,(120,30))
  47.  
  48. #lives_counter3 = screen.blit(lives,(190,30))
  49.  
  50. class PADDLE:
  51. def __init__(self,xpos,ypos):
  52. self.x = xpos
  53. self.y = ypos
  54.  
  55. def draw(self): # draws paddle
  56. pygame.draw.rect(screen,pink,(self.x,self.y,70,20))
  57.  
  58.  
  59. def move(self):
  60. keys = pygame.key.get_pressed() #checking pressed keys
  61. if keys[pygame.K_LEFT]:
  62. if self.x<=50:
  63. self.x=50
  64. else:
  65. self.x -= 10
  66. elif keys[pygame.K_RIGHT]:
  67. if self.x >=680:
  68. self.x = 680
  69. else:
  70. self.x += 10
  71.  
  72. class BALL:
  73. def __init__(self,paddle1):
  74. self.x = (paddle1.x+35)
  75. self.y = (paddle1.y-5)
  76. self.speed = 0
  77. self.speedX = 0
  78. self.speedY = 0
  79. self.direction = 200
  80.  
  81. def draw(self):
  82. pygame.draw.circle(screen,WHITE,(self.x,self.y),10)
  83.  
  84. def bounce(self):
  85. self.direction = (180 - self.direction) % 360
  86.  
  87. def move_ball(self):
  88. keys = pygame.key.get_pressed() #checking pressed keys
  89. ball_on_paddle = True
  90. if ball_on_paddle == True :
  91. self.x = (paddle1.x+35)
  92. self.y = (paddle1.y-5)
  93. self.speed = 0
  94.  
  95. if keys[pygame.K_UP] == True:
  96. ball_on_paddle = False
  97. print("a")
  98. self.speed = 10
  99. self.speedX += int(math.cos(angle)* self.speed)
  100. self.speedY += int(math.sin(angle)* self.speed)
  101. print(bottom)
  102. print(self.speedY)
  103. if self.y <= 0:
  104. self.bounce(0)
  105. self.y = 1
  106.  
  107. if self.x <= 0:
  108. self.direction = (360 - self.direction) % 360
  109. self.x = 1
  110.  
  111. if self.x > self.screenwidth - self.width:
  112. self.direction = (360 - self.direction) % 360
  113. self.x = self.screenwidth - self.width - 1
  114.  
  115.  
  116. paddle1 = PADDLE(350,550)
  117. ball1 = BALL(paddle1)
  118.  
  119. # MAIN LOOP
  120. while not done:
  121. screen.fill((LIGHT_BLUE))
  122. game_screen.draw_screen()
  123. game_screen.save_button()
  124. game_screen.quit_button()
  125. paddle1.draw()
  126. paddle1.move()
  127. ball1.draw()
  128. ball1.move_ball()
  129. ball1.bounce()
  130.  
  131.  
  132.  
  133.  
  134. for event in pygame.event.get():
  135. if event.type == pygame.QUIT:
  136. done = True
  137.  
  138. pygame.display.flip()
  139. clock.tick(60)
  140.  
  141. pygame.quit()
  142.  
  143. if self.y < ball_size:
  144. self.angle = - self.angle
  145. self.y = 2*ball_size - self.y
  146. elif self.y > DISPLAY_HEIGHT - ball_size:
  147. self.angle = - self.angle
  148. self.y = 2*(DISPLAY_HEIGHT - ball_size) - self.y
Add Comment
Please, Sign In to add comment