Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import pygame
- import random
- pygame.init()
- screen = pygame.display.set_mode([800,600])
- done = False
- clock = pygame.time.Clock()
- #DEFINE COLORS
- WHITE = (255,255,255)
- BLUE=(0,102,204)
- LIGHT_BLUE=(0,0,204)
- pink=(238,130,238)
- #import images
- #lives = pygame.image.load("heart.png")
- # initialize font; must be called after 'pygame.init()' to avoid 'Font not Initialized' error
- myfont = pygame.font.SysFont("monospace", 15)
- #var carying the space of the game
- top = -100
- left= -50
- right= -750
- bottom= -600
- angle = math.radians(random.randint(-140,-30))
- class game_screen():
- def draw_screen():
- pygame.draw.rect(screen,BLUE,(50,100,700,600))
- def save_button():
- pygame.draw.rect(screen,pink,(500,20,50,30),0)
- save = "Save"
- label = myfont.render(save, 40, (0,0,0))
- screen.blit(label, (505, 20))
- def quit_button():
- pygame.draw.rect(screen,pink,(600,20,50,30),0)
- quit1 = "Quit"
- label = myfont.render(quit1, 40, (0,0,0))
- screen.blit(label, (605, 20))
- mX, mY = pygame.mouse.get_pos()
- mouseButtons = pygame.mouse.get_pressed()
- if mouseButtons[0] == True:
- if (mX in range (600-20,600+20) and mY in range (20-20,20+20)):
- pygame.quit()
- #def display_lives():
- #lives_counter = screen.blit(lives,(50,30))
- #lives_counter2 = screen.blit(lives,(120,30))
- #lives_counter3 = screen.blit(lives,(190,30))
- class PADDLE:
- def __init__(self,xpos,ypos):
- self.x = xpos
- self.y = ypos
- def draw(self): # draws paddle
- pygame.draw.rect(screen,pink,(self.x,self.y,70,20))
- def move(self):
- keys = pygame.key.get_pressed() #checking pressed keys
- if keys[pygame.K_LEFT]:
- if self.x<=50:
- self.x=50
- else:
- self.x -= 10
- elif keys[pygame.K_RIGHT]:
- if self.x >=680:
- self.x = 680
- else:
- self.x += 10
- class BALL:
- def __init__(self,paddle1):
- self.x = (paddle1.x+35)
- self.y = (paddle1.y-5)
- self.speed = 0
- self.speedX = 0
- self.speedY = 0
- self.direction = 200
- def draw(self):
- pygame.draw.circle(screen,WHITE,(self.x,self.y),10)
- def bounce(self):
- self.direction = (180 - self.direction) % 360
- def move_ball(self):
- keys = pygame.key.get_pressed() #checking pressed keys
- ball_on_paddle = True
- if ball_on_paddle == True :
- self.x = (paddle1.x+35)
- self.y = (paddle1.y-5)
- self.speed = 0
- if keys[pygame.K_UP] == True:
- ball_on_paddle = False
- print("a")
- self.speed = 10
- self.speedX += int(math.cos(angle)* self.speed)
- self.speedY += int(math.sin(angle)* self.speed)
- print(bottom)
- print(self.speedY)
- if self.y <= 0:
- self.bounce(0)
- self.y = 1
- if self.x <= 0:
- self.direction = (360 - self.direction) % 360
- self.x = 1
- if self.x > self.screenwidth - self.width:
- self.direction = (360 - self.direction) % 360
- self.x = self.screenwidth - self.width - 1
- paddle1 = PADDLE(350,550)
- ball1 = BALL(paddle1)
- # MAIN LOOP
- while not done:
- screen.fill((LIGHT_BLUE))
- game_screen.draw_screen()
- game_screen.save_button()
- game_screen.quit_button()
- paddle1.draw()
- paddle1.move()
- ball1.draw()
- ball1.move_ball()
- ball1.bounce()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
- if self.y < ball_size:
- self.angle = - self.angle
- self.y = 2*ball_size - self.y
- elif self.y > DISPLAY_HEIGHT - ball_size:
- self.angle = - self.angle
- self.y = 2*(DISPLAY_HEIGHT - ball_size) - self.y
Add Comment
Please, Sign In to add comment