All pastes #618335 Raw Edit

Stuff

public text v1 · immutable
·published 2007-07-13 22:03 UTC
rendered paste body
import sys, pygame

pygame.init()

size = width, height = 800, 600
speed = [2, 2]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

ball = pygame.image.load("ball.bmp")
ballrect = ball.get_rect()
ballRealPos = [0.0, 0.0]

upKeyHeld = False
rightKeyHeld = False
downKeyHeld = False
leftKeyHeld = False

clock = pygame.time.Clock()
clockFont = pygame.font.Font(None, 24)
fps = 60
acel = 1

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                leftKeyHeld = True
                speed[0] = speed[0] + 1
            if event.key == pygame.K_RIGHT:
                rightKeyHeld = True
                speed[0] = speed[0] - 1
            if event.key == pygame.K_UP:
                upKeyHeld = True
                speed[1] = speed[1] - 1
            if event.key == pygame.K_DOWN:
                downKeyHeld = True
                speed[1] = speed[1] + 1
            if event.key == pygame.K_RETURN:
                upKeyHeld = False
                rightKeyHeld = False
                downKeyHeld = False
                leftKeyHeld = False
                speed = [0, 0]
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT: leftKeyHeld = False
            if event.key == pygame.K_RIGHT: rightKeyHeld = False
            if event.key == pygame.K_UP: upKeyHeld = False
            if event.key == pygame.K_DOWN: downKeyHeld = False

    timeSinceLast = clock.get_time() / 1000.0

    if upKeyHeld == True:
        speed[1] -= acel * timeSinceLast
    if downKeyHeld == True:
        speed[1] += acel * timeSinceLast
    if rightKeyHeld == True:
        speed[0] += acel * timeSinceLast
    if leftKeyHeld == True:
        speed[0] -= acel * timeSinceLast

    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    ballRealPos[0] = ballRealPos[0] + speed[0] * timeSinceLast
    ballRealPos[1] = ballRealPos[1] + speed[1] * timeSinceLast
    #ballrect = ballrect.move(int(ballRealPos[0]), int(ballRealPos[1]))

    fpsInfo = clockFont.render(str(clock.get_fps()), True, (255, 255, 255))
    speedy = clockFont.render(str(speed[1]), True, (255, 255, 255))
    speedx = clockFont.render(str(speed[0]), True, (255, 255, 255))
    posy = clockFont.render(str(ballRealPos[0]), True, (255, 255, 255))
    posx = clockFont.render(str(ballRealPos[1]), True, (255, 255, 255))

    screen.fill(black)
    screen.blit(ball, ballrect)
    screen.blit(fpsInfo, (5, 5))
    screen.blit(speedy, (5, 35))
    screen.blit(speedx, (5, 65))
    screen.blit(posy, (5, 95))
    screen.blit(posx, (5, 125))
    pygame.display.flip()

    clock.tick(fps)