2016-11-20 88 views
0
  1. 我目前遇到的问题是我的图像似乎是自己填充屏幕,我该如何解决这个问题?雪碧问题

    import pygame, os, sys 
    from pygame.locals import* 
    
    pygame.init() 
    
    sHEIGHT = 600 
    sWIDTH = 800 
    gName = "Survive" 
    
    fps = pygame.time.Clock() 
    FPS = 10 
    
    GD = pygame.display.set_mode((sWIDTH,sHEIGHT)) 
    class Main: 
    white = (255,255,255) 
    
    def display(white): 
        pygame.display.set_caption(gName) 
        GD.fill(white) 
    def player1(): 
        img ="Images/" 
        health = 100 
        global movex 
        global movexchange 
        global movey 
        global moveychange 
        global imgplayer1 
        movex = 400 
        movey = 400 
        movexchange = 0 
        moveychange = 0 
    
        imgplayer1 = pygame.image.load(os.path.join(img, "Human.png")) 
    
    
    
    
        pygame.display.update()       
    
    
    def gameLoop(): 
        fps.tick(FPS) 
    
    
        while True: 
         print(fps) 
         global movexchange 
         global moveychange 
         global movex 
         global movey 
         for event in pygame.event.get(): 
          if event.type == pygame.QUIT: 
             pygame.quit() 
             sys.exit() 
          if event.type == pygame.KEYDOWN: 
           if event.key == pygame.K_a: 
             moveychange = 0 
             movexchange -=5 
           if event.key == pygame.K_w: 
             moveychange -=5 
             movexchange =0 
           if event.key == pygame.K_s: 
             moveychange += 5 
             movexchange = 0  
           if event.key == pygame.K_d: 
             moveychange = 0 
             movexchange +=5  
          if event.type == pygame.KEYUP:   
           if event.key == pygame.K_a: 
            movexchange =0 
            moveychange =0 
           if event.key == pygame.K_w: 
            movexchange =0 
            moveychange =0 
           if event.key == pygame.K_s: 
            movexchange =0 
            moveychange =0 
           if event.key == pygame.K_d: 
            movexchange =0 
            moveychange =0   
    
    
         movex += movexchange 
         movey += moveychange 
         GD.blit(imgplayer1,(movex,movey)) 
         pygame.display.update() 
    
    
    display(white) 
    player1() 
    gameLoop() 
    

没有人有任何想法,为什么发生这种情况,我该如何解决这一问题?

回答

0

PyGame是低级库。

你必须清除屏幕,然后在新的位置blit对象。您可以使用一些图像作为背景,或者您可以使用fill(color)来填充屏幕。

# --- constants --- (UPPER_CASE names) 

WHITE = (255, 255, 255) 
FPS = 30 

# --- functions --- (lower_case names) 

def game_loop(): 
    global move_x_change 
    global move_y_change 
    global move_x 
    global move_y 

    while True: 
     # --- events --- 

     for event in pygame.event.get(): 
      # ... code ... 

     # --- updates (without draws) --- 

     move_x += move_x_change 
     move_y += move_y_change 

     # --- draws (without updates) --- 

     # clear buffer 
     screen.fill(WHITE) 

     # draw elements 
     screen.blit(img_player_1, (move_x, move_y)) 

     # send buffer on screen 
     pygame.display.update() 

     # --- FPS --- 

     # keep constant speed 
     fps.tick(FPS) 

# --- main --- 

pygame.init() 
# ... code ... 

BTW:PEP 8 -- Style Guide for Python Code

示例如何组织代码:simpleother templates

+0

非常感谢你对我的帮助。我一直在这个问题上停留了10个小时。我现在只有4天的时间没有任何指导。 再次,非常感谢您解决这个问题,并教我当我需要使用大写和小写 – zerrith

+0

顺便说一句:[编程与Python和pygame街机游戏](http://programarcadegames.com/) – furas

+0

感谢您的额外资源: ) – zerrith