[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Game stutters



Hi, i'm currently working on a breakout clone and i'm pretty much done. However every few seconds the game freezes for like a split second, causing my game to stutter. It's not really a big deal but it is annoying. I stripped out everything except the code to make the ball bounce around the screen but it's still doing it. I'm using python 2.4.3 and pygame 1.7.1 on Windows XP.

Heres the code:


import pygame
from pygame.locals import *

def main():
  pygame.init()
  screen = pygame.display.set_mode((640, 480))
  screenRect = screen.get_rect()
  clock = pygame.time.Clock()
 
  #ball Variables
  ballColour = (255, 0, 0)
  ballX, ballY = screenRect.center
  ballRadius = 5
  ballSpeedX, ballSpeedY = 5, 5
  ballRect = pygame.Rect (ballX, ballY, ballRadius * 2, ballRadius * 2)
  ballImage = pygame.Surface((ballRadius * 2, ballRadius * 2))
  pygame.draw.circle(ballImage, ballColour, (ballRadius, ballRadius), ballRadius)
 
  while True:
    #handle user input
    for event in pygame.event.get():
      if event.type == QUIT:
        pygame.quit()
        return
       
    #move ball
    ballRect.move_ip(ballSpeedX, ballSpeedY)
   
    #make sure ball stays within screen boundaries
    if ballRect.left <= screenRect.left:
      ballSpeedX = -ballSpeedX
    elif ballRect.right >= screenRect.right:
      ballSpeedX = -ballSpeedX
    elif ballRect.top <= screenRect.top:
      ballSpeedY = -ballSpeedY
    elif ballRect.bottom >= screenRect.bottom:
      ballSpeedY = -ballSpeedY
   
    #clear screen
    screen.fill((0, 0, 0))
   
    #draw ball
    screen.blit(ballImage, ballRect)
   
    pygame.display.update()
   
    clock.tick(40)

if __name__ == "__main__":
  main()