# Try to keep the ball bouncing # Ball will lose energy when it touches the ground # import pygame from pygame.locals import * from sys import exit #import random pygame.init() screen=pygame.display.set_mode((640,480),0,32) pygame.display.set_caption("Bounce") #creating a black background back = pygame.Surface((640,480)) #background dimensions background = back.convert() background.fill((0,0,0)) # background color # creating first bar bar = pygame.Surface((50,10)) # bar dimensions bar1 = bar.convert() bar1.fill((0,0,255)) #bar color # creating ball circ_sur = pygame.Surface((30,30)) # defining surface size allocated for circel circ = pygame.draw.circle(circ_sur,(0,255,0),(15,15),15) #(screen,color,center,radius) (320,20) ? circle = circ_sur.convert() circle.set_colorkey((0,0,0)) # defining the tuple arrangement of color # Starting Positions of bar and circle and intial speeds # Defining bar1 inital position bar1_x = 320. bar1_y= 240. # Defining initial ball position circle_x, circle_y = 15., 30. # Initial speeds bar1_move = 0. speed_x, speed_y, speed_circ = 250., 250., 250. #clock object clock = pygame.time.Clock() #Defining the font font=pygame.font.SysFont("Calibri",40) restitution=1.0 count=0 while True: for event in pygame.event.get(): if event.type == QUIT: # makes sure system exits when closing window exit() if event.type == KEYDOWN: if event.key == K_LEFT: bar1_move = -ai_speed # when left key is pressed bar1 is moved to the left elif event.key == K_RIGHT: bar1_move = ai_speed # when right key is pressed bar1 is moved to the right # when key press is removed bar stops moving elif event.type == KEYUP: if event.key == K_LEFT: bar1_move = 0. elif event.key == K_RIGHT: bar1_move = 0. gameOver=font.render("Game On",True,(255,255,255)) screen.blit(background,(0,0)) screen.blit(gameOver,(250.,400.)) frame = pygame.draw.rect(screen,(255,255,255),Rect((5,5),(630,470)),2) #? screen.color,position,size screen.blit(bar1,(bar1_x,bar1_y)) screen.blit(circle,(circle_x,circle_y)) #moves bars with the speed defined above bar1_x += bar1_move # movement of circle time_passed = clock.tick(30) time_sec = time_passed / 1000.0 circle_x += speed_x * time_sec circle_y += speed_y * time_sec ai_speed = speed_circ * time_sec #A very basic collision definition of circle with bars #Collision with bar1 ? Not working but was working before if circle_x >= bar1_x-30 and circle_x <= bar1_x+30.: if circle_y >= 230. and circle_y <= 250.: speed_y = -speed_y #Collision with vertical walls if circle_x < 15.: speed_x = -speed_x elif circle_x > 630.: speed_x = -speed_x #Collision with roof if circle_y <= 10.: speed_y = -speed_y #Collision with ground elif circle_y >= 450: speed_y = -speed_y *restitution count+=1 restitution=restitution-count/25. if abs(speed_y)