#initially code:
import pygame,sys pygame.init() #vInfo = pygame.display.Info() #size = width,height=vInfo.current_w,vInfo.current_h pygame.display.set_mode(size,pygame.FULLSCREEN) size = width,height=600,400 Black= 0,0,0 speed = [1,1] ball = pygame.image.load("./football.png") ballrect = ball.get_rect() #screen = pygame.display.set_mode(size,pygame.FULLSCREEN) screen = pygame.display.set_mode(size,pygame.RESIZABLE) #screen = pygame.display.set_mode(size,pygame.NOFRAME) pygame.display.set_caption("property and monney") fps = 500 skill = True centerx = (ballrect.right - ballrect.left) / 2 + ballrect.left centery = (ballrect.bottom - ballrect.top) / 2 + ballrect.top fClock = pygame.time.Clock() while True: # centerx = (ballrect.right - ballrect.left) / 2 + ballrect.left # centery = (ballrect.bottom - ballrect.top) / 2 + ballrect.top for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: speed[0]=speed[0] if speed[0]==0 else (abs(speed[0]-1))*int(speed[0]/abs(speed[0])) elif event.key == pygame.K_RIGHT: speed[0] += 1 if speed[0]>0 else speed[0]-1 elif event.key == pygame.K_UP: speed[1]= speed[1]+1 if speed[1]>0 else speed[1]-1 elif event.key == pygame.K_DOWN: speed[1]=-speed[1] if speed[1]==0 else (abs(speed[1]-1))*int(speed[1]/abs(speed[1])) elif event.key == pygame.K_ESCAPE: sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: skill = False if event.button == 1: ballrect = ballrect.move(event.pos[0] - centerx, event.pos[1] - centery) elif event.type == pygame.MOUSEMOTION: if event.buttons[0] == 1: ballrect = ballrect.move(event.pos[0]-centerx,event.pos[1]-centery) elif event.type == pygame.MOUSEBUTTONUP: skill = True elif event.type == pygame.VIDEORESIZE: size = width,height = event.size[0],event.size[1] screen = pygame.display.set_mode(size,pygame.RESIZABLE) if pygame.display.get_active() and skill : ballrect=ballrect.move(speed) if ballrect.left<0 or ballrect.right>width: speed[0] = -speed[0] if ballrect.right>width and ballrect.right+speed[0]>ballrect.right: speed[0] = -speed[0] if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] if ballrect.bottom > height and ballrect.bottom + speed[1] > ballrect.bottom: speed[1] = -speed[1] screen.fill(Black) screen.blit(ball, ballrect) pygame.display.update() fClock.tick(fps)
##################################################################
but the result was my ball flied away, thing the reason is due to the “centerx” and “centery” variables is global. In the circle main programe can't change the contents , so the values are become uncertains .Move to the variables into the circle,the question was solved wonderfuly.