diff --git a/__pycache__/ball.cpython-39.pyc b/__pycache__/ball.cpython-39.pyc new file mode 100644 index 0000000..28c81ab Binary files /dev/null and b/__pycache__/ball.cpython-39.pyc differ diff --git a/__pycache__/brick.cpython-39.pyc b/__pycache__/brick.cpython-39.pyc new file mode 100644 index 0000000..d56d372 Binary files /dev/null and b/__pycache__/brick.cpython-39.pyc differ diff --git a/__pycache__/config.cpython-39.pyc b/__pycache__/config.cpython-39.pyc new file mode 100644 index 0000000..1653c6d Binary files /dev/null and b/__pycache__/config.cpython-39.pyc differ diff --git a/__pycache__/paddle.cpython-39.pyc b/__pycache__/paddle.cpython-39.pyc new file mode 100644 index 0000000..e9b9977 Binary files /dev/null and b/__pycache__/paddle.cpython-39.pyc differ diff --git a/ball.py b/ball.py index d494876..954b5ee 100644 --- a/ball.py +++ b/ball.py @@ -17,7 +17,8 @@ def __init__(self,color,width,height): # Drawinng the ball rectangle pygame.draw.rect(self.image,color,[0,0,width,height]) - self.velocity=[randint(4,8),randint(-8,8)] + # self.velocity=[randint(4,8),randint(-8,8)] + self.velocity=[0,-10] # Fetch the rectangle object that has the dimension of the image self.rect=self.image.get_rect() @@ -25,7 +26,16 @@ def __init__(self,color,width,height): def update(self): self.rect.x+=self.velocity[0] self.rect.y+=self.velocity[1] + # ball always vertical direction + def bounce(self): self.velocity[0]= -self.velocity[0] - self.velocity[1] =randint(-8,8) \ No newline at end of file + # self.velocity[1] =randint(-8,8) + self.velocity[1]=-10 + +# make the ball object +if __name__ == '__main__': + ball=Ball(BLACK,10,10) + print(ball.rect.x,ball.rect.y) + print(ball.velocity) \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..0b8506b --- /dev/null +++ b/config.py @@ -0,0 +1,11 @@ +WHITE=(255,255,255) +DARKBLUE=(36,90,190) +LIGHTBLUE=(0,176,240) +RED=(255,0,0) +ORANGE=(255,100,0) +YELLOW=(255,255,0) +GREEN=(0,255,0) +WINDOW_HEIGHT=800 +WINDOW_WIDTH=600 +SCORE=0 +LIVES=10 diff --git a/main.py b/main.py index 0e81337..1562a69 100644 --- a/main.py +++ b/main.py @@ -3,194 +3,114 @@ """ import pygame # import paddle class +import config as cfg from paddle import Paddle from ball import Ball from brick import Brick pygame.init() -''' -Step 2: Define colours in the game -''' -#Define some colors in RGB format -WHITE=(255,255,255) -DARKBLUE=(36,90,190) -LIGHTBLUE=(0,176,240) -RED=(255,0,0) -ORANGE=(255,100,0) -YELLOW=(255,255,0) -GREEN=(0,255,0) - -score=100 -lives=10 - - -''' -Step 3: open a new window -''' + # set window size -size=(800,600) +size=(cfg.WINDOW_HEIGHT,cfg.WINDOW_WIDTH) screen=pygame.display.set_mode(size) pygame.display.set_caption("Shooter Aircraft Breakout Game") -''' -# Responding the keystroke events -paddleA=Paddle(WHITE,10,100) -paddleA.rect.x=20 -paddleA.rect.y=200 -paddleB=Paddle(WHITE,10,100) -paddleB.rect.x=670 -paddleB.rect.y=200 -''' # this will be a list that will contain all the sprites we intend all_sprites_list=pygame.sprite.Group() - -# Add thepaddles to the list of sprites -# all_sprites_list.add(paddleA) -# all_sprites_list.add(paddleB) +balls=[] # Create the paddle -paddle=Paddle(LIGHTBLUE,100,50) -paddle.rect.x=350 -paddle.rect.y=560 - -# Create the ball sprite -ball=Ball(LIGHTBLUE,10,10) -ball.rect.x=345 -ball.rect.y=195 - -all_bricks=pygame.sprite.Group() -for i in range(7): - brick=Brick(RED,80,30) - brick.rect.x=60+i*100 - brick.rect.y=100 - all_sprites_list.add(brick) - all_bricks.add(brick) - -for i in range(7): - brick = Brick(ORANGE,80,30) - brick.rect.x = 60 + i* 100 - brick.rect.y = 140 - all_sprites_list.add(brick) - all_bricks.add(brick) -for i in range(7): - brick = Brick(YELLOW,80,30) - brick.rect.x = 60 + i* 100 - brick.rect.y = 180 - all_sprites_list.add(brick) - all_bricks.add(brick) - -for i in range(7): - brick = Brick(GREEN,80,30) - brick.rect.x = 60 + i* 100 - brick.rect.y = 220 - all_sprites_list.add(brick) - all_bricks.add(brick) - -# add the paddle to the list of sprites -all_sprites_list.add(paddle) -all_sprites_list.add(ball) -print(all_sprites_list) - -''' -step 4: Main program loop -Capturing Events: Used to constantly “listen” to user inputs and react to these. It could be when the user use the keyboard or the mouse. -Implementing the Game Logic. What happens when the game is running? Are cars moving forward, aliens falling from the sky, ghosts chasing you, etc. -Refreshing the screen by redrawing the stage and the sprites. -''' -# the lopp will carry on until exit the game -carryOn=True - -#the clock will be used to control how fast screen upload updates -clock=pygame.time.Clock() +def brick_design(): + all_bricks=pygame.sprite.Group() + for i in range(7): + brick=Brick(cfg.RED,80,30) + brick.rect.x=60+i*100 + brick.rect.y=100 + all_sprites_list.add(brick) + all_bricks.add(brick) + + for i in range(7): + brick = Brick(cfg.ORANGE,80,30) + brick.rect.x = 60 + i* 100 + brick.rect.y = 140 + all_sprites_list.add(brick) + all_bricks.add(brick) + for i in range(7): + brick = Brick(cfg.YELLOW,80,30) + brick.rect.x = 60 + i* 100 + brick.rect.y = 180 + all_sprites_list.add(brick) + all_bricks.add(brick) + + for i in range(7): + brick = Brick(cfg.GREEN,80,30) + brick.rect.x = 60 + i* 100 + brick.rect.y = 220 + all_sprites_list.add(brick) + all_bricks.add(brick) +def paddle_on_game_board(): + paddle=Paddle(cfg.LIGHTBLUE,100,50) + paddle.rect.x=350 + paddle.rect.y=560 + all_sprites_list.add(paddle) + return paddle + +def initialize_ball_position(): + # Create the ball sprite + + ball=Ball(cfg.LIGHTBLUE,10,10) + ball.rect.x=400 + ball.rect.y=560 + balls.append(ball) + +def game_over_function(): + font=pygame.font.Font(None,74) + text=font.render("GAME OVER",1,cfg.WHITE) + screen.blit(text,(200,300)) + pygame.display.flip() + pygame.time.wait(3000) + + # stop the Game + carryOn=False -#------------ Main program loop -while carryOn: - # main event loop - for event in pygame.event.get(): #user did something - if event.type ==pygame.QUIT: # if user click close - carryOn=False # Flag that we are done to exit - elif event.type==pygame.KEYDOWN: - if event.key==pygame.K_x: - carryOn=False - - # Moving the paddle when the use uses the arrow key +def keyboard_functionalities(paddle,ball,all_bricks): keys=pygame.key.get_pressed() if keys[pygame.K_LEFT]: paddle.moveLeft(5) if keys[pygame.K_RIGHT]: paddle.moveRight(5) - if keys[pygame.K_UP]: - paddle.moveUp(5) if keys[pygame.K_DOWN]: paddle.moveDown(5) + if keys[pygame.K_UP]: - - #-------- Game logic should go there - all_sprites_list.update() - - - - - - # Check if the ball is bounhing against any of the 4 walls - if ball.rect.x>=790: - ball.velocity[0]= -ball.velocity[0] - if ball.rect.x<=0: - ball.velocity[0]= -ball.velocity[0] - if ball.rect.y>590: - ball.velocity[1]= -ball.velocity[1] - lives-=1 - if lives==0: - # Display Game over for 3 seconds - font=pygame.font.Font(None,74) - text=font.render("GAME OVER",1,WHITE) - screen.blit(text,(250,300)) - pygame.display.flip() - pygame.time.wait(3000) - - # stop the game - carryOn=False - - if ball.rect.y<40: - ball.velocity[1]= -ball.velocity[1] - - ''' - if the ball touch any side : up, down, left ,right wall - then the ball set same velocity but reverse position - ''' - # print('velocity :',ball.velocity) - # print("Position: ",ball.rect) - print('Brick x',brick.rect.x,end='') - print(' Brick y',brick.rect.y) + # ball=Ball(WHITE,10,10) + all_sprites_list.add(ball) + ball.rect.x=paddle.rect.x+50 + ball.rect.y=paddle.rect.y + balls.append(ball) - - - # Detect collisions between the ball and paddles - if pygame.sprite.collide_mask(ball,paddle): - ball.rect.x -=ball.velocity[0] - ball.rect.y -=ball.velocity[1] - ball.bounce() - - - - # Check if there is a car collision - brick_collision_list=pygame.sprite.spritecollide(ball,all_bricks,False) - + for brick in all_bricks: + brick.rect.y += 1 - + if brick.rect.y > 550: + game_over_function() + + +def brick_and_ball_collision(ball,all_bricks): + brick_collision_list=pygame.sprite.spritecollide(ball,all_bricks,False) + for brick in brick_collision_list: - ball.bounce() + # ball.bounce() score +=1 - brick.kill() - # Brick Fallen 5 pixels per loop - for brick in all_bricks: - brick.rect.y +=5 + ball.kill() + # brick.kill() + if len(all_bricks) ==0: # Display level complete 3 seconds font=pygame.font.Font(None,74) - text=font.render("LEVEL WIN",1,WHITE) + text=font.render("LEVEL WIN",1,cfg.WHITE) screen.blit(text,(200,300)) pygame.display.flip() pygame.time.wait(3000) @@ -198,27 +118,64 @@ # stop the Game carryOn=False - #--- Drawing code should go here - # First clear the screen to dark blue - screen.fill(DARKBLUE) - pygame.draw.line(screen,WHITE,[0,38],[800,38],2) + for ball in balls: + brick_collision_list=pygame.sprite.spritecollide(ball,all_bricks,False) - # now lets draw all the sprites in one go + + for brick in brick_collision_list: + # ball.bounce() + score +=1 + ball.kill() + brick.kill() + balls.remove(ball) + + if len(all_bricks) ==0: + # Display level complete 3 seconds + font=pygame.font.Font(None,74) + text=font.render("LEVEL WIN",1,cfg.WHITE) + screen.blit(text,(200,300)) + pygame.display.flip() + pygame.time.wait(3000) + + # stop the Game + carryOn=False + + +carryOn=True +clock=pygame.time.Clock() +while carryOn: + + for event in pygame.event.get(): + if event.type ==pygame.QUIT: + carryOn=False + all_sprites_list.update() + + screen.fill(cfg.DARKBLUE) + pygame.draw.line(screen,cfg.WHITE,[0,38],[800,38],2) all_sprites_list.draw(screen) - - # Display the score and the number of lives at the top of the window + pygame.display.flip() + clock.tick(60) + +pygame.quit() + + + +def score_and_live_calculation(): font=pygame.font.Font(None,34) - text=font.render("Score: "+str(score),1,WHITE) + text=font.render("Score: "+str(cfg.SCORE),1,cfg.WHITE) screen.blit(text,(20,10)) - text=font.render("Lives: "+str(lives),1,WHITE) + text=font.render("Lives: "+str(cfg.LIVES),1,cfg.WHITE) screen.blit(text,(650,10)) + + +if __name__ == '__main__': + brick_design() + padddle=paddle_on_game_board() + initialize_ball_position() + keyboard_functionalities() + brick_and_ball_collision() + score_and_live_calculation() + + - # Go ahead and update the screen with we've drawn - pygame.display.flip() - # Limit to 60 frame per second - clock.tick(60) -# Once we have exited the main program loop we can stop the game -pygame.quit() - - \ No newline at end of file