趣学python3(21)-pygame-surface位置及角色移动

    import sys, pygame
    import random
   #code:刘兴
    pygame.init()
    
    size = width, height = 320, 240
    black = 0, 0, 0
    snakeNodeSize=(24,24)
    
    screen = pygame.display.set_mode(size)
    
    snakeNodeImage = pygame.image.load("snake1.png")
    snakeNode = pygame.transform.scale(snakeNodeImage,snakeNodeSize)
    snakeNodeRect = snakeNode.get_rect()
    print(snakeNode.get_size())
    print(snakeNodeRect.width,snakeNodeRect.height)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        runSpeed = [24, 24]
        runDirection=random.randint(1,4)#left,right,up,down
        if runDirection==1:
            runSpeed[1]=0 
            runSpeed[0]=-runSpeed[0]
        elif runDirection==2:
            runSpeed[1]=0     
        elif runDirection==3:
            runSpeed[0]=0
            runSpeed[1]=-runSpeed[1]
        elif runDirection==4:
            runSpeed[0]=0     
            
        snakeNodeRect = snakeNodeRect.move(runSpeed)
        if snakeNodeRect.left < 0 :
            snakeNodeRect.x=width-snakeNodeRect.width
        if snakeNodeRect.right > width:
            snakeNodeRect.x=0    
        if snakeNodeRect.top < 0:
            snakeNodeRect.y=height-snakeNodeRect.height
        if snakeNodeRect.bottom > height:
            snakeNodeRect.y=0
     
        pygame.time.delay(500)
    
        screen.fill(black)
        screen.blit(snakeNode,snakeNodeRect)
        pygame.display.flip()

方块在自由移动,遇到壁会自动到另一边
趣学python3(21)-pygame-surface位置及角色移动_第1张图片

你可能感兴趣的:(大数据与算法计算)