Error from the global variables

#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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值