Hi, I'm hoping the this was not a homework assignment (I am a teacher). But, because you showed your code, I'll help you out. Most of your code was fine, but some things were in the wrong place. The basic idea is that there should be a main loop, and in that loop, you check for events (key down, etc.), adjust anything you need to adjust, then draw everything. Hope this helps: #!/usr/bin/env python import pygame import sys # needed to quit gracefully #import pygame.draw N_PIXELS_TO_MOVE = 5 # how many pixels to move on each keypress FRAMES_PER_SECOND = 30 # How fast to draw - this says draw every 30th of a second def main(): pygame.init() clock = pygame.time.Clock() # added to allow things to slow down screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption('pygame test') # You don't need this. screen.fill below does this for you. #s = pygame.Surface(screen.get_size(), pygame.SRCALPHA, 32) # Pick a starting X, Y location for the circle circleX = 0 circleY = 0 while True: # You don't need this #screen.blit(s, (0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() # quit pygame sys.exit() # quit the program if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE or event.unicode == 'q': pygame.quit() sys.exit() # If user presses left, right, up, or down, change the appropriate coordinate elif event.key == pygame.K_LEFT: circleX = circleX - N_PIXELS_TO_MOVE elif event.key == pygame.K_RIGHT: circleX = circleX + N_PIXELS_TO_MOVE elif event.key == pygame.K_UP: circleY = circleY - N_PIXELS_TO_MOVE elif event.key == pygame.K_DOWN: circleY = circleY + N_PIXELS_TO_MOVE # Now do all the drawing, first the background (fill) screen.fill((0, 0, 0)) # Then draw the circle at the circle's coordinates pygame.draw.circle(screen, (255, 255, 255, 255), (circleX, circleY), 50) pygame.display.flip() # or: pygame.display.update() clock.tick(FRAMES_PER_SECOND) # make PyGame wait the correct amount if __name__ == '__main__': main() Irv
|