From 371b9156da8974e1e83fc9d3fcbc5986e2333788 Mon Sep 17 00:00:00 2001 From: lmazardo Date: Tue, 30 Oct 2012 23:47:41 +0100 Subject: [PATCH 1/6] add headers --- brick.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/brick.py b/brick.py index e81ce10..0640f51 100644 --- a/brick.py +++ b/brick.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python + # -*- coding: utf-8 -*- + import os, pygame, random, math from pygame.locals import * pygame.init() @@ -248,4 +251,4 @@ def ballHasHitBrick(ball, b): waitForPlayerToPressKey() else: break -pygame.quit() \ No newline at end of file +pygame.quit() From 66e1e50bc5a99b2ab1389760935a6d1223241edd Mon Sep 17 00:00:00 2001 From: lmazardo Date: Wed, 31 Oct 2012 00:08:23 +0100 Subject: [PATCH 2/6] RENAME: globals in UPPERCASE, Run to isRunning --- readme.txt => readme.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename readme.txt => readme.md (100%) diff --git a/readme.txt b/readme.md similarity index 100% rename from readme.txt rename to readme.md From b98db7656b9171f681a706c2d408bb356c9edd99 Mon Sep 17 00:00:00 2001 From: lmazardo Date: Wed, 31 Oct 2012 00:09:36 +0100 Subject: [PATCH 3/6] RENAME: globals in UPPERCASE, Run to isRunning --- brick.py | 192 +++++++++++++++++++++++++++---------------------------- 1 file changed, 96 insertions(+), 96 deletions(-) diff --git a/brick.py b/brick.py index 0640f51..cc6d113 100644 --- a/brick.py +++ b/brick.py @@ -25,22 +25,22 @@ def drawText(text, font, surface, x, y): # Based on pythagoras theorem, func to find the height given the base (x or horizontal) and hypoteneuse (ballspeed/velocity). def pythag(x, speed): - if speed**2 != x**2 + ballSpeed[1]**2: - ballSpeed[1] = math.sqrt(speed**2 - x**2) - return ballSpeed[1] + if speed**2 != x**2 + BALLSPEED[1]**2: + BALLSPEED[1] = math.sqrt(speed**2 - x**2) + return BALLSPEED[1] else: - return ballSpeed[1] + return BALLSPEED[1] # Lays out bricks in 8x8 grid def drawLevel(): for i in range(3, 11): for j in range(3, 11): - newBrick = [pygame.Rect((i * WIDTH), (j * HEIGHT), WIDTH, HEIGHT), color[(random.randint(1, 5)-1)], None] - bricks.append(newBrick) + newBrick = [pygame.Rect((i * WIDTH), (j * HEIGHT), WIDTH, HEIGHT), COLOR[(random.randint(1, 5)-1)], None] + BRICKS.append(newBrick) # Function to check if the ball has hit the paddle - Under construction - currently does not recognise side collisions. def ballHasHitPaddle(ball, paddle): - if (paddle.top - devy) <= ball.bottom <= (paddle.top + devy): + if (paddle.top - DEVIATION_Y) <= ball.bottom <= (paddle.top + DEVIATION_Y): if ball.right >= paddle.left and ball.left <= paddle.right: return True else: @@ -48,10 +48,10 @@ def ballHasHitPaddle(ball, paddle): # Function to check if the ball has hit a brick. def ballHasHitBrick(ball, b): - if (b[0].top - devy) <= ball.bottom <= (b[0].top + devy) or (b[0].bottom - devy) <= ball.top <= (b[0].bottom + devy): + if (b[0].top - DEVIATION_Y) <= ball.bottom <= (b[0].top + DEVIATION_Y) or (b[0].bottom - DEVIATION_Y) <= ball.top <= (b[0].bottom + DEVIATION_Y): if ball.right >= b[0].left and ball.left <= b[0].right: return True - elif (b[0].right - devx) <= ball.left <= (b[0].right + devx) or (b[0].left - devx) <= ball.right <= (b[0].left + devx): + elif (b[0].right - DEVIATION_X) <= ball.left <= (b[0].right + DEVIATION_X) or (b[0].left - DEVIATION_X) <= ball.right <= (b[0].left + DEVIATION_X): if ball.bottom >= b[0].top and ball.top <= b[0].bottom: return True else: @@ -63,9 +63,9 @@ def ballHasHitBrick(ball, b): # basic pygame window stuff SCREENWIDTH = 700 SCREENHEIGHT = 500 -screen = pygame.display.set_mode((SCREENWIDTH,SCREENHEIGHT)) +SCREEN = pygame.display.set_mode((SCREENWIDTH,SCREENHEIGHT)) pygame.display.set_caption("Brick") -clock = pygame.time.Clock() +CLOCK = pygame.time.Clock() # set up colors BLACK = (0,0,0) @@ -78,51 +78,51 @@ def ballHasHitBrick(ball, b): # Set up the Paddle PADDLEWIDTH = 100 PADDLEHEIGHT = 20 -player = pygame.Rect(int((SCREENWIDTH/2)-(PADDLEWIDTH/2)), int(SCREENHEIGHT-PADDLEHEIGHT), PADDLEWIDTH, PADDLEHEIGHT) +PLAYER = pygame.Rect(int((SCREENWIDTH/2)-(PADDLEWIDTH/2)), int(SCREENHEIGHT-PADDLEHEIGHT), PADDLEWIDTH, PADDLEHEIGHT) # Handle paddle horizontal movement (the paddle will never move vertically) -paddleSpeed = 5 +PADDLESPEED = 5 # Set up the ball BALLWIDTH = 10 -ball = pygame.Rect(int(SCREENWIDTH/2), int(SCREENHEIGHT/2), BALLWIDTH, BALLWIDTH) +BALL = pygame.Rect(int(SCREENWIDTH/2), int(SCREENHEIGHT/2), BALLWIDTH, BALLWIDTH) # Handle ball movement and deviation. -speed = 5 -ballSpeed = [3, 3] # Values for x and y -ballSpeed[1] = pythag(ballSpeed[0], speed) +SPEED = 5 +BALLSPEED = [3, 3] # Values for x and y +BALLSPEED[1] = pythag(BALLSPEED[0], SPEED) # Deviation for collision detection. Returns Int value. -devx = int(round(ballSpeed[0]/2)) -devy = int(round((ballSpeed[1]/2)+0.6)) +DEVIATION_X = int(round(BALLSPEED[0]/2)) +DEVIATION_Y = int(round((BALLSPEED[1]/2)+0.6)) # set up fonts -font = pygame.font.SysFont(None, 36) +FONT = pygame.font.SysFont(None, 36) TEXTCOLOR = (255,255,255) # Setup the bricks WIDTH = 50 HEIGHT = 20 -color = [BLACK, WHITE, RED, GREEN, PURPLE] -bricks = [] +COLOR = [BLACK, WHITE, RED, GREEN, PURPLE] +BRICKS = [] # Game's opening screen -drawText('Welcome to Brick!', font, screen, 250, 200) -drawText('Press any key to play or Esc to quit.', font, screen, 150, 280) +drawText('Welcome to Brick!', FONT, SCREEN, 250, 200) +drawText('Press any key to play or Esc to quit.', FONT, SCREEN, 150, 280) pygame.display.update() waitForPlayerToPressKey() while True: - Run = True + isRunning = True lives = 3 score = 0 pause = False moveLeft = moveRight = False drawLevel() - # Run game loop - while Run: + # isRunning game loop + while isRunning: for event in pygame.event.get(): # handles quit event if event.type == pygame.QUIT: - Run = False + isRunning = False # Handles keybindings if event.type == KEYDOWN: if event.key == K_LEFT or event.key == ord('a'): @@ -133,7 +133,7 @@ def ballHasHitBrick(ball, b): moveLeft = False if event.type == KEYUP: if event.key == K_ESCAPE: - Run = False + isRunning = False if event.key == K_SPACE: pause = True if event.key == K_LEFT or event.key == ord('a'): @@ -143,110 +143,110 @@ def ballHasHitBrick(ball, b): # Handles control using the mouse if event.type == MOUSEMOTION: # If the mouse moves, move the paddle where the cursor is. - player.move_ip(event.pos[0] - player.centerx, 0) + PLAYER.move_ip(event.pos[0] - PLAYER.centerx, 0) # Move the paddle left and right. - if moveLeft and player.left > 0: - player.move_ip(-1 * paddleSpeed, 0) - if moveRight and player.right < SCREENWIDTH: - player.move_ip(paddleSpeed, 0) + if moveLeft and PLAYER.left > 0: + PLAYER.move_ip(-1 * PADDLESPEED, 0) + if moveRight and PLAYER.right < SCREENWIDTH: + PLAYER.move_ip(PADDLESPEED, 0) - # Stop the paddle from moving off screen - if player.left < 0: - player.left = 0 - if player.right > SCREENWIDTH: - player.right = SCREENWIDTH + # Stop the paddle from moving off SCREEN + if PLAYER.left < 0: + PLAYER.left = 0 + if PLAYER.right > SCREENWIDTH: + PLAYER.right = SCREENWIDTH # Move the mouse cursor to match the paddle. - pygame.mouse.set_pos(player.centerx, player.centery) - - # Ball bounces off the walls and ceiling, player loses a life when the ball hits the bottom of the screen. - ball = ball.move(ballSpeed) - if ball.left < 0 or ball.right > SCREENWIDTH: - ballSpeed[0] = -ballSpeed[0] - if ball.top < 0: - ballSpeed[1] = -ballSpeed[1] - if ball.bottom > SCREENHEIGHT: + pygame.mouse.set_pos(PLAYER.centerx, PLAYER.centery) + + # Ball bounces off the walls and ceiling, PLAYER loses a life when the BALL hits the bottom of the SCREEN. + BALL = BALL.move(BALLSPEED) + if BALL.left < 0 or BALL.right > SCREENWIDTH: + BALLSPEED[0] = -BALLSPEED[0] + if BALL.top < 0: + BALLSPEED[1] = -BALLSPEED[1] + if BALL.bottom > SCREENHEIGHT: lives -= 1 - ball.centerx = int(SCREENWIDTH/2) - ball.centery = int(SCREENHEIGHT/2) + BALL.centerx = int(SCREENWIDTH/2) + BALL.centery = int(SCREENHEIGHT/2) pause = True - if ballHasHitPaddle(ball, player): - if (player.top - devy) <= ball.bottom <= (player.top + devy): - if player.left <= ball.centerx <= (player.centerx - 25): - ballSpeed[0] = -3 - ballSpeed[1] = pythag(ballSpeed[0], speed) - if (player.centerx - 25) <= ball.centerx < player.centerx: - ballSpeed[0] = -1 - ballSpeed[1] = pythag(ballSpeed[0], speed) - if player.centerx < ball.centerx <= (player.centerx + 25): - ballSpeed[0] = 1 - ballSpeed[1] = pythag(ballSpeed[0], speed) - if (player.centerx + 25) <= ball.centerx <= player.right: - ballSpeed[0] = 3 - ballSpeed[1] = pythag(ballSpeed[0], speed) - ballSpeed[1] = -ballSpeed[1] - - screen.fill((20,50,150)) - - # Bricks collision detection, if the ball hits a brick, the brick is removed and the ball changes direction. - for b in bricks: - if ballHasHitBrick(ball, b): - if (b[0].right - devx) <= ball.left <= (b[0].right + devx) or (b[0].left - devx) <= ball.right <= (b[0].left + devx): - ballSpeed[0] = -ballSpeed[0] - elif (b[0].top - devy) <= ball.bottom <= (b[0].top + devy) or (b[0].bottom - devy) <= ball.top <= (b[0].bottom + devy): - ballSpeed[1] = -ballSpeed[1] + if ballHasHitPaddle(BALL, PLAYER): + if (PLAYER.top - DEVIATION_Y) <= BALL.bottom <= (PLAYER.top + DEVIATION_Y): + if PLAYER.left <= BALL.centerx <= (PLAYER.centerx - 25): + BALLSPEED[0] = -3 + BALLSPEED[1] = pythag(BALLSPEED[0], SPEED) + if (PLAYER.centerx - 25) <= BALL.centerx < PLAYER.centerx: + BALLSPEED[0] = -1 + BALLSPEED[1] = pythag(BALLSPEED[0], SPEED) + if PLAYER.centerx < BALL.centerx <= (PLAYER.centerx + 25): + BALLSPEED[0] = 1 + BALLSPEED[1] = pythag(BALLSPEED[0], SPEED) + if (PLAYER.centerx + 25) <= BALL.centerx <= PLAYER.right: + BALLSPEED[0] = 3 + BALLSPEED[1] = pythag(BALLSPEED[0], SPEED) + BALLSPEED[1] = -BALLSPEED[1] + + SCREEN.fill((20,50,150)) + + # Bricks collision detection, if the BALL hits a brick, the brick is removed and the BALL changes direction. + for b in BRICKS: + if ballHasHitBrick(BALL, b): + if (b[0].right - DEVIATION_X) <= BALL.left <= (b[0].right + DEVIATION_X) or (b[0].left - DEVIATION_X) <= BALL.right <= (b[0].left + DEVIATION_X): + BALLSPEED[0] = -BALLSPEED[0] + elif (b[0].top - DEVIATION_Y) <= BALL.bottom <= (b[0].top + DEVIATION_Y) or (b[0].bottom - DEVIATION_Y) <= BALL.top <= (b[0].bottom + DEVIATION_Y): + BALLSPEED[1] = -BALLSPEED[1] else: None - bricks.remove(b) + BRICKS.remove(b) score += 100 - pygame.draw.rect(screen, b[1], b[0]) + pygame.draw.rect(SCREEN, b[1], b[0]) - # Every 5000 points the player gains an extra life + # Every 5000 points the PLAYER gains an extra life if score != 0 and score % 5000 == 0: lives += 1 score += 1 - # Draw the no. of lives and current score on the screen - drawText('Score: %s' % (score), font, screen, 10, 0) - drawText('Lives: %s' % (lives), font, screen, 200, 0) + # Draw the no. of lives and current score on the SCREEN + drawText('Score: %s' % (score), FONT, SCREEN, 10, 0) + drawText('Lives: %s' % (lives), FONT, SCREEN, 200, 0) - # Draw the ball and the paddle - pygame.draw.rect(screen, WHITE, ball) - pygame.draw.rect(screen, BLACK, player) + # Draw the BALL and the paddle + pygame.draw.rect(SCREEN, WHITE, BALL) + pygame.draw.rect(SCREEN, BLACK, PLAYER) # Pause Loop. while pause == True: - drawText('PAUSED', font, screen, int((SCREENWIDTH/2)-50), int((SCREENHEIGHT/2)-7)) + drawText('PAUSED', FONT, SCREEN, int((SCREENWIDTH/2)-50), int((SCREENHEIGHT/2)-7)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pause = False - Run = False + isRunning = False if event.type == KEYUP: if event.key == K_ESCAPE: pause = False - Run = False + isRunning = False if event.key == K_SPACE: pause = False if lives == 0: break - if lives == 0 or len(bricks) == 0: - Run = False + if lives == 0 or len(BRICKS) == 0: + isRunning = False pygame.display.flip() - clock.tick(60) + CLOCK.tick(60) if lives == 0: - screen.fill((20,50,150)) - drawText('GAME OVER, YEAH', font, screen, 250, 200) - drawText('Press any key to play again or Esc to quit.', font, screen, 100, 240) + SCREEN.fill((20,50,150)) + drawText('GAME OVER, YEAH', FONT, SCREEN, 250, 200) + drawText('Press any key to play again or Esc to quit.', FONT, SCREEN, 100, 240) pygame.display.update() waitForPlayerToPressKey() - elif len(bricks) == 0: - drawText('You Won!', font, screen, 250, 200) - drawText('Press any key to play again or Esc to quit.', font, screen, 100, 240) + elif len(BRICKS) == 0: + drawText('You Won!', FONT, SCREEN, 250, 200) + drawText('Press any key to play again or Esc to quit.', FONT, SCREEN, 100, 240) pygame.display.update() waitForPlayerToPressKey() else: From bd60bc52bc5dfd608dcf7f536631cd9c50cf1257 Mon Sep 17 00:00:00 2001 From: lmazardo Date: Wed, 31 Oct 2012 00:10:02 +0100 Subject: [PATCH 4/6] add depencies install infos --- readme.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 16fb25d..aba0458 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,14 @@ First project by Francis Apaloo ------------------------------- -Re-build of classic brick/breakout games as a first project using Python 3.2 -+ Pygame extension. \ No newline at end of file +Re-build of classic brick/breakout games as a first project using Python 3.2 + Pygame extension. + +Installing Dependencies on Debian : +```bash + sudo apt-get install python-pygame +``` + +Running : +```bash + python brick.py +``` From f03dffde0c522da1d8f408eab5ea23d2ebfc8b42 Mon Sep 17 00:00:00 2001 From: lmazardo Date: Wed, 31 Oct 2012 00:33:02 +0100 Subject: [PATCH 5/6] untabiffy file and rename some functions using python convention --- brick.py | 368 +++++++++++++++++++++++++++---------------------------- 1 file changed, 184 insertions(+), 184 deletions(-) diff --git a/brick.py b/brick.py index cc6d113..6648d6c 100644 --- a/brick.py +++ b/brick.py @@ -1,61 +1,61 @@ #!/usr/bin/env python - # -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- import os, pygame, random, math from pygame.locals import * pygame.init() -def waitForPlayerToPressKey(): - while True: - for event in pygame.event.get(): - if event.type == QUIT: - pygame.quit() - if event.type == KEYDOWN: - if event.key == K_ESCAPE: # pressing escape quits - pygame.quit() - return +def wait_for_player_to_press_key(): + while True: + for event in pygame.event.get(): + if event.type == QUIT: + pygame.quit() + if event.type == KEYDOWN: + if event.key == K_ESCAPE: # pressing escape quits + pygame.quit() + return # creates a function to draw text -def drawText(text, font, surface, x, y): - textobj = font.render(text, 1, TEXTCOLOR) - textrect = textobj.get_rect() - textrect.topleft = (x, y) - surface.blit(textobj, textrect) - return textrect +def draw_text(text, font, surface, x, y): + textobj = font.render(text, 1, TEXTCOLOR) + textrect = textobj.get_rect() + textrect.topleft = (x, y) + surface.blit(textobj, textrect) + return textrect # Based on pythagoras theorem, func to find the height given the base (x or horizontal) and hypoteneuse (ballspeed/velocity). def pythag(x, speed): - if speed**2 != x**2 + BALLSPEED[1]**2: - BALLSPEED[1] = math.sqrt(speed**2 - x**2) - return BALLSPEED[1] - else: - return BALLSPEED[1] + if speed**2 != x**2 + BALLSPEED[1]**2: + BALLSPEED[1] = math.sqrt(speed**2 - x**2) + return BALLSPEED[1] + else: + return BALLSPEED[1] # Lays out bricks in 8x8 grid -def drawLevel(): - for i in range(3, 11): - for j in range(3, 11): - newBrick = [pygame.Rect((i * WIDTH), (j * HEIGHT), WIDTH, HEIGHT), COLOR[(random.randint(1, 5)-1)], None] - BRICKS.append(newBrick) +def draw_level(): + for i in range(3, 11): + for j in range(3, 11): + newBrick = [pygame.Rect((i * WIDTH), (j * HEIGHT), WIDTH, HEIGHT), COLOR[(random.randint(1, 5)-1)], None] + BRICKS.append(newBrick) # Function to check if the ball has hit the paddle - Under construction - currently does not recognise side collisions. -def ballHasHitPaddle(ball, paddle): - if (paddle.top - DEVIATION_Y) <= ball.bottom <= (paddle.top + DEVIATION_Y): - if ball.right >= paddle.left and ball.left <= paddle.right: - return True - else: - return False +def ball_has_hit_paddle(ball, paddle): + if (paddle.top - DEVIATION_Y) <= ball.bottom <= (paddle.top + DEVIATION_Y): + if ball.right >= paddle.left and ball.left <= paddle.right: + return True + else: + return False # Function to check if the ball has hit a brick. -def ballHasHitBrick(ball, b): - if (b[0].top - DEVIATION_Y) <= ball.bottom <= (b[0].top + DEVIATION_Y) or (b[0].bottom - DEVIATION_Y) <= ball.top <= (b[0].bottom + DEVIATION_Y): - if ball.right >= b[0].left and ball.left <= b[0].right: - return True - elif (b[0].right - DEVIATION_X) <= ball.left <= (b[0].right + DEVIATION_X) or (b[0].left - DEVIATION_X) <= ball.right <= (b[0].left + DEVIATION_X): - if ball.bottom >= b[0].top and ball.top <= b[0].bottom: - return True - else: - return False +def ball_has_hit_brick(ball, b): + if (b[0].top - DEVIATION_Y) <= ball.bottom <= (b[0].top + DEVIATION_Y) or (b[0].bottom - DEVIATION_Y) <= ball.top <= (b[0].bottom + DEVIATION_Y): + if ball.right >= b[0].left and ball.left <= b[0].right: + return True + elif (b[0].right - DEVIATION_X) <= ball.left <= (b[0].right + DEVIATION_X) or (b[0].left - DEVIATION_X) <= ball.right <= (b[0].left + DEVIATION_X): + if ball.bottom >= b[0].top and ball.top <= b[0].bottom: + return True + else: + return False # centre window os.environ['SDL_VIDEO_CENTERED'] = '1' @@ -105,150 +105,150 @@ def ballHasHitBrick(ball, b): BRICKS = [] # Game's opening screen -drawText('Welcome to Brick!', FONT, SCREEN, 250, 200) -drawText('Press any key to play or Esc to quit.', FONT, SCREEN, 150, 280) +draw_text('Welcome to Brick!', FONT, SCREEN, 250, 200) +draw_text('Press any key to play or Esc to quit.', FONT, SCREEN, 150, 280) pygame.display.update() -waitForPlayerToPressKey() +wait_for_player_to_press_key() while True: - isRunning = True - lives = 3 - score = 0 - pause = False - moveLeft = moveRight = False - drawLevel() - # isRunning game loop - while isRunning: - for event in pygame.event.get(): - # handles quit event - if event.type == pygame.QUIT: - isRunning = False - # Handles keybindings - if event.type == KEYDOWN: - if event.key == K_LEFT or event.key == ord('a'): - moveRight = False - moveLeft = True - if event.key == K_RIGHT or event.key == ord('d'): - moveRight = True - moveLeft = False - if event.type == KEYUP: - if event.key == K_ESCAPE: - isRunning = False - if event.key == K_SPACE: - pause = True - if event.key == K_LEFT or event.key == ord('a'): - moveLeft = False - if event.key == K_RIGHT or event.key == ord('d'): - moveRight = False - # Handles control using the mouse - if event.type == MOUSEMOTION: - # If the mouse moves, move the paddle where the cursor is. - PLAYER.move_ip(event.pos[0] - PLAYER.centerx, 0) - - # Move the paddle left and right. - if moveLeft and PLAYER.left > 0: - PLAYER.move_ip(-1 * PADDLESPEED, 0) - if moveRight and PLAYER.right < SCREENWIDTH: - PLAYER.move_ip(PADDLESPEED, 0) - - # Stop the paddle from moving off SCREEN - if PLAYER.left < 0: - PLAYER.left = 0 - if PLAYER.right > SCREENWIDTH: - PLAYER.right = SCREENWIDTH - - # Move the mouse cursor to match the paddle. - pygame.mouse.set_pos(PLAYER.centerx, PLAYER.centery) - - # Ball bounces off the walls and ceiling, PLAYER loses a life when the BALL hits the bottom of the SCREEN. - BALL = BALL.move(BALLSPEED) - if BALL.left < 0 or BALL.right > SCREENWIDTH: - BALLSPEED[0] = -BALLSPEED[0] - if BALL.top < 0: - BALLSPEED[1] = -BALLSPEED[1] - if BALL.bottom > SCREENHEIGHT: - lives -= 1 - BALL.centerx = int(SCREENWIDTH/2) - BALL.centery = int(SCREENHEIGHT/2) - pause = True - if ballHasHitPaddle(BALL, PLAYER): - if (PLAYER.top - DEVIATION_Y) <= BALL.bottom <= (PLAYER.top + DEVIATION_Y): - if PLAYER.left <= BALL.centerx <= (PLAYER.centerx - 25): - BALLSPEED[0] = -3 - BALLSPEED[1] = pythag(BALLSPEED[0], SPEED) - if (PLAYER.centerx - 25) <= BALL.centerx < PLAYER.centerx: - BALLSPEED[0] = -1 - BALLSPEED[1] = pythag(BALLSPEED[0], SPEED) - if PLAYER.centerx < BALL.centerx <= (PLAYER.centerx + 25): - BALLSPEED[0] = 1 - BALLSPEED[1] = pythag(BALLSPEED[0], SPEED) - if (PLAYER.centerx + 25) <= BALL.centerx <= PLAYER.right: - BALLSPEED[0] = 3 - BALLSPEED[1] = pythag(BALLSPEED[0], SPEED) - BALLSPEED[1] = -BALLSPEED[1] - - SCREEN.fill((20,50,150)) - - # Bricks collision detection, if the BALL hits a brick, the brick is removed and the BALL changes direction. - for b in BRICKS: - if ballHasHitBrick(BALL, b): - if (b[0].right - DEVIATION_X) <= BALL.left <= (b[0].right + DEVIATION_X) or (b[0].left - DEVIATION_X) <= BALL.right <= (b[0].left + DEVIATION_X): - BALLSPEED[0] = -BALLSPEED[0] - elif (b[0].top - DEVIATION_Y) <= BALL.bottom <= (b[0].top + DEVIATION_Y) or (b[0].bottom - DEVIATION_Y) <= BALL.top <= (b[0].bottom + DEVIATION_Y): - BALLSPEED[1] = -BALLSPEED[1] - else: - None - BRICKS.remove(b) - score += 100 - pygame.draw.rect(SCREEN, b[1], b[0]) - - # Every 5000 points the PLAYER gains an extra life - if score != 0 and score % 5000 == 0: - lives += 1 - score += 1 - - # Draw the no. of lives and current score on the SCREEN - drawText('Score: %s' % (score), FONT, SCREEN, 10, 0) - drawText('Lives: %s' % (lives), FONT, SCREEN, 200, 0) - - # Draw the BALL and the paddle - pygame.draw.rect(SCREEN, WHITE, BALL) - pygame.draw.rect(SCREEN, BLACK, PLAYER) - - # Pause Loop. - while pause == True: - drawText('PAUSED', FONT, SCREEN, int((SCREENWIDTH/2)-50), int((SCREENHEIGHT/2)-7)) - pygame.display.update() - for event in pygame.event.get(): - if event.type == pygame.QUIT: - pause = False - isRunning = False - if event.type == KEYUP: - if event.key == K_ESCAPE: - pause = False - isRunning = False - if event.key == K_SPACE: - pause = False - if lives == 0: - break - - if lives == 0 or len(BRICKS) == 0: - isRunning = False - - pygame.display.flip() - CLOCK.tick(60) - - if lives == 0: - SCREEN.fill((20,50,150)) - drawText('GAME OVER, YEAH', FONT, SCREEN, 250, 200) - drawText('Press any key to play again or Esc to quit.', FONT, SCREEN, 100, 240) - pygame.display.update() - waitForPlayerToPressKey() - elif len(BRICKS) == 0: - drawText('You Won!', FONT, SCREEN, 250, 200) - drawText('Press any key to play again or Esc to quit.', FONT, SCREEN, 100, 240) - pygame.display.update() - waitForPlayerToPressKey() - else: - break + isRunning = True + lives = 3 + score = 0 + pause = False + moveLeft = moveRight = False + draw_level() + # isRunning game loop + while isRunning: + for event in pygame.event.get(): + # handles quit event + if event.type == pygame.QUIT: + isRunning = False + # Handles keybindings + if event.type == KEYDOWN: + if event.key == K_LEFT or event.key == ord('a'): + moveRight = False + moveLeft = True + if event.key == K_RIGHT or event.key == ord('d'): + moveRight = True + moveLeft = False + if event.type == KEYUP: + if event.key == K_ESCAPE: + isRunning = False + if event.key == K_SPACE: + pause = True + if event.key == K_LEFT or event.key == ord('a'): + moveLeft = False + if event.key == K_RIGHT or event.key == ord('d'): + moveRight = False + # Handles control using the mouse + if event.type == MOUSEMOTION: + # If the mouse moves, move the paddle where the cursor is. + PLAYER.move_ip(event.pos[0] - PLAYER.centerx, 0) + + # Move the paddle left and right. + if moveLeft and PLAYER.left > 0: + PLAYER.move_ip(-1 * PADDLESPEED, 0) + if moveRight and PLAYER.right < SCREENWIDTH: + PLAYER.move_ip(PADDLESPEED, 0) + + # Stop the paddle from moving off SCREEN + if PLAYER.left < 0: + PLAYER.left = 0 + if PLAYER.right > SCREENWIDTH: + PLAYER.right = SCREENWIDTH + + # Move the mouse cursor to match the paddle. + pygame.mouse.set_pos(PLAYER.centerx, PLAYER.centery) + + # Ball bounces off the walls and ceiling, PLAYER loses a life when the BALL hits the bottom of the SCREEN. + BALL = BALL.move(BALLSPEED) + if BALL.left < 0 or BALL.right > SCREENWIDTH: + BALLSPEED[0] = -BALLSPEED[0] + if BALL.top < 0: + BALLSPEED[1] = -BALLSPEED[1] + if BALL.bottom > SCREENHEIGHT: + lives -= 1 + BALL.centerx = int(SCREENWIDTH/2) + BALL.centery = int(SCREENHEIGHT/2) + pause = True + if ball_has_hit_paddle(BALL, PLAYER): + if (PLAYER.top - DEVIATION_Y) <= BALL.bottom <= (PLAYER.top + DEVIATION_Y): + if PLAYER.left <= BALL.centerx <= (PLAYER.centerx - 25): + BALLSPEED[0] = -3 + BALLSPEED[1] = pythag(BALLSPEED[0], SPEED) + if (PLAYER.centerx - 25) <= BALL.centerx < PLAYER.centerx: + BALLSPEED[0] = -1 + BALLSPEED[1] = pythag(BALLSPEED[0], SPEED) + if PLAYER.centerx < BALL.centerx <= (PLAYER.centerx + 25): + BALLSPEED[0] = 1 + BALLSPEED[1] = pythag(BALLSPEED[0], SPEED) + if (PLAYER.centerx + 25) <= BALL.centerx <= PLAYER.right: + BALLSPEED[0] = 3 + BALLSPEED[1] = pythag(BALLSPEED[0], SPEED) + BALLSPEED[1] = -BALLSPEED[1] + + SCREEN.fill((20,50,150)) + + # Bricks collision detection, if the BALL hits a brick, the brick is removed and the BALL changes direction. + for b in BRICKS: + if ball_has_hit_brick(BALL, b): + if (b[0].right - DEVIATION_X) <= BALL.left <= (b[0].right + DEVIATION_X) or (b[0].left - DEVIATION_X) <= BALL.right <= (b[0].left + DEVIATION_X): + BALLSPEED[0] = -BALLSPEED[0] + elif (b[0].top - DEVIATION_Y) <= BALL.bottom <= (b[0].top + DEVIATION_Y) or (b[0].bottom - DEVIATION_Y) <= BALL.top <= (b[0].bottom + DEVIATION_Y): + BALLSPEED[1] = -BALLSPEED[1] + else: + None + BRICKS.remove(b) + score += 100 + pygame.draw.rect(SCREEN, b[1], b[0]) + + # Every 5000 points the PLAYER gains an extra life + if score != 0 and score % 5000 == 0: + lives += 1 + score += 1 + + # Draw the no. of lives and current score on the SCREEN + draw_text('Score: %s' % (score), FONT, SCREEN, 10, 0) + draw_text('Lives: %s' % (lives), FONT, SCREEN, 200, 0) + + # Draw the BALL and the paddle + pygame.draw.rect(SCREEN, WHITE, BALL) + pygame.draw.rect(SCREEN, BLACK, PLAYER) + + # Pause Loop. + while pause == True: + draw_text('PAUSED', FONT, SCREEN, int((SCREENWIDTH/2)-50), int((SCREENHEIGHT/2)-7)) + pygame.display.update() + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pause = False + isRunning = False + if event.type == KEYUP: + if event.key == K_ESCAPE: + pause = False + isRunning = False + if event.key == K_SPACE: + pause = False + if lives == 0: + break + + if lives == 0 or len(BRICKS) == 0: + isRunning = False + + pygame.display.flip() + CLOCK.tick(60) + + if lives == 0: + SCREEN.fill((20,50,150)) + draw_text('GAME OVER, YEAH', FONT, SCREEN, 250, 200) + draw_text('Press any key to play again or Esc to quit.', FONT, SCREEN, 100, 240) + pygame.display.update() + wait_for_player_to_press_key() + elif len(BRICKS) == 0: + draw_text('You Won!', FONT, SCREEN, 250, 200) + draw_text('Press any key to play again or Esc to quit.', FONT, SCREEN, 100, 240) + pygame.display.update() + wait_for_player_to_press_key() + else: + break pygame.quit() From 59f486250dd12ddec23bc3c1625087242b112bb3 Mon Sep 17 00:00:00 2001 From: lmazardo Date: Wed, 31 Oct 2012 00:45:56 +0100 Subject: [PATCH 6/6] better pylint report --- brick.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/brick.py b/brick.py index 6648d6c..8a7fcd5 100644 --- a/brick.py +++ b/brick.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import os, pygame, random, math +# pylint: disable-msg=w0614 from pygame.locals import * pygame.init() @@ -23,7 +24,8 @@ def draw_text(text, font, surface, x, y): surface.blit(textobj, textrect) return textrect -# Based on pythagoras theorem, func to find the height given the base (x or horizontal) and hypoteneuse (ballspeed/velocity). +# Based on pythagoras theorem, func to find the height given the base +# (x or horizontal) and hypoteneuse (ballspeed/velocity). def pythag(x, speed): if speed**2 != x**2 + BALLSPEED[1]**2: BALLSPEED[1] = math.sqrt(speed**2 - x**2) @@ -63,13 +65,13 @@ def ball_has_hit_brick(ball, b): # basic pygame window stuff SCREENWIDTH = 700 SCREENHEIGHT = 500 -SCREEN = pygame.display.set_mode((SCREENWIDTH,SCREENHEIGHT)) +SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT)) pygame.display.set_caption("Brick") CLOCK = pygame.time.Clock() # set up colors -BLACK = (0,0,0) -WHITE = (255,255,255) +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) @@ -96,7 +98,7 @@ def ball_has_hit_brick(ball, b): # set up fonts FONT = pygame.font.SysFont(None, 36) -TEXTCOLOR = (255,255,255) +TEXTCOLOR = (255, 255, 255) # Setup the bricks WIDTH = 50 @@ -111,18 +113,18 @@ def ball_has_hit_brick(ball, b): wait_for_player_to_press_key() while True: - isRunning = True + is_running = True lives = 3 score = 0 pause = False moveLeft = moveRight = False draw_level() - # isRunning game loop - while isRunning: + # Run game loop + while is_running: for event in pygame.event.get(): # handles quit event if event.type == pygame.QUIT: - isRunning = False + is_running = False # Handles keybindings if event.type == KEYDOWN: if event.key == K_LEFT or event.key == ord('a'): @@ -133,7 +135,7 @@ def ball_has_hit_brick(ball, b): moveLeft = False if event.type == KEYUP: if event.key == K_ESCAPE: - isRunning = False + is_running = False if event.key == K_SPACE: pause = True if event.key == K_LEFT or event.key == ord('a'): @@ -187,7 +189,7 @@ def ball_has_hit_brick(ball, b): BALLSPEED[1] = pythag(BALLSPEED[0], SPEED) BALLSPEED[1] = -BALLSPEED[1] - SCREEN.fill((20,50,150)) + SCREEN.fill((20, 50, 150)) # Bricks collision detection, if the BALL hits a brick, the brick is removed and the BALL changes direction. for b in BRICKS: @@ -222,24 +224,24 @@ def ball_has_hit_brick(ball, b): for event in pygame.event.get(): if event.type == pygame.QUIT: pause = False - isRunning = False + is_running = False if event.type == KEYUP: if event.key == K_ESCAPE: pause = False - isRunning = False + is_running = False if event.key == K_SPACE: pause = False if lives == 0: break if lives == 0 or len(BRICKS) == 0: - isRunning = False + is_running = False pygame.display.flip() CLOCK.tick(60) if lives == 0: - SCREEN.fill((20,50,150)) + SCREEN.fill((20, 50, 150)) draw_text('GAME OVER, YEAH', FONT, SCREEN, 250, 200) draw_text('Press any key to play again or Esc to quit.', FONT, SCREEN, 100, 240) pygame.display.update()