Python pong.

  1. X-13
    X-13
    I've nearly made pong... I r teh best!

    Just need to figure out enemy paddle AI [along with getting user_paddle stuff actually coded] and sort out a scoring system.


    So, basically, I have a ball bouncing around on it's own. Go me!
  2. tmcd35
    tmcd35


    So come on, lets see some code I actually haven't done much since the last post. I drew a stickman in paint and got Python to display him on screen - yay a sprite!!!
  3. X-13
    X-13
    Code:
    import sys, pygame
    pygame.init()
    
    size = width, height = 640, 480
    speed = [2,2]
    black = (0, 0, 0)
    white = (255, 255, 255)
    screen = pygame.display.set_mode(size)
    ball = pygame.image.load("ball.bmp")
    ballrect = ball.get_rect()
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
        ballrect = ballrect.move(speed)
        if ballrect.left < 0 or ballrect.right > width:
            speed[0] = -speed[0]
        if ballrect.top < 0 or ballrect.bottom > height:
            speed[1] = -speed[1]
        screen.fill(white)
        screen.blit(ball, ballrect)
        pygame.display.flip()
    "ball.bmp" is a 20x20 black square saved in the same place as the script.

    Even though I didn't code for it, it has the extra ability to crash on exit. [Window stops responding.]

    I need to go back through it to make sure I fully understand what does what.
  4. tmcd35
    tmcd35
    One thing I had drummed into me at college and uni was "comment everything", I still pretty poor at at it, but it does help when you go back and re-read a bit of code and bug finding. Especially when the code starts getting really long.
  5. X-13
    X-13
    I generally do that, but just burned through this quick.

    Hence the need to go back through it now... [I also just pinched a bit of your code "pygame.display.set_caption"]

    Le EDIT: Just added some controls

    esc/q to close it.plus, I pre-defined the up/down controls and space to start.
  6. X-13
    X-13
    OK, I have half of the controls done. [Down works.]

    Code:
    if event.type == pygame.KEYDOWN:
        if event.key in (down_):
            ballrect = ballrect.move(speed)
            speed[0] = -speed[0]
        if event.key in (up_):
            ballrect = ballrect.move(speed)
            speed[0] = [SOMETHING]
    What does [SOMETHING] need to be?

    In other words, what's the opposite of -speed[0]? [It's not +speed[0]...]
  7. tmcd35
    tmcd35
    Code:
    speed = [2,2]
    speed[0] = -speed[0]
    Slightly confused by the choice of variable name. Are you controlling speed or direction? Is the value of speed x,y screen coordinates - or the rate of which the x,y cordinates are being changed by

    Code:
    ballrect.move(speed)
    ?

    Also doesn't this line negate itself?

    Code:
    speed[0] = -speed[0]
    So that after that line 'speed[0]' is always equal to '0'?

    My gut instinct say the lines would probably read better...

    Code:
    if event.type == pygame.KEYDOWN:
        if event.key in (down_):
            speed[0] = speed[0] - 1
        if event.key in (up_):
            speed[0] = speed[0] + 1
    
    ballrect = ballrect.move(speed)
  8. X-13
    X-13
    NAILED IT!

    Code:
        # Paddle controls
        if event.type == pygame.KEYDOWN:
            if event.key in (down_):
                if paddlerect.bottom != game.bottom:
                    paddlerect = paddlerect.move(0, y)
            if event.key in (up_):
                if paddlerect.top != game.top:
                    paddlerect = paddlerect.move(0, -y)

    I should point out, I renamed "speed" to "position" and position = (x, y)
    Moves as smooth as butter and stops at the top/bottom.
  9. X-13
    X-13
    Here you go, gents.

    Code:
    import sys
    import pygame
    pygame.init()
    # Set global defaults
    size = width, height = 640, 480
    x = (1)
    y = (1)
    black = (0, 0, 0)
    white = (255, 255, 255)
    clock = pygame.time.Clock()
    # Set display settings
    screen = pygame.display.set_mode(size)
    game = screen.get_rect()
    pygame.display.set_caption ("Pong - Made by X-13")
    # asset defaults
    paddle = pygame.image.load("paddle.bmp")
    paddlerect = paddle.get_rect()
    paddlerect.centery = (height / 2)
    ball = pygame.image.load("ball.bmp")
    ballrect = ball.get_rect()
    
    #controls
    exit_ = pygame.K_ESCAPE, pygame.K_q
    up_ = pygame.K_UP, pygame.K_w
    down_ = pygame.K_DOWN, pygame.K_s
    start_ = pygame.K_SPACE
    # Main loop
    while 1:
        # You need an exit strategy!
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key in (exit_):
                    sys.exit(0)
        # Ball start location and behaviour
        ballrect.left = paddlerect.right
        ballrect.centery = paddlerect.centery
        # Paddle controls
        if event.type == pygame.KEYDOWN:
            if event.key in (down_):
                if paddlerect.bottom != game.bottom:
                    paddlerect = paddlerect.move(0, y)
            if event.key in (up_):
                if paddlerect.top != game.top:
                    paddlerect = paddlerect.move(0, -y)
        # Draw everything that needs to be seen
        screen.fill(white)
        screen.blit(paddle, paddlerect)
        screen.blit(ball, ballrect)
        pygame.display.flip()
    This is where I'm up to only a few changes here and there. I'm currently working on the ball physics with a "press space to start" trigger [The one I had I didn't really understand, so I'm starting again.]

    ATM, the ball sticks to the paddle as it moves. The plan is to get in position, press space then start playing.
Results 1 to 9 of 9