2013-05-19 79 views
1

我在屏幕上繪製一個簡單的矩形有問題。我不知道爲什麼它不能繪製,我嘗試了幾件事:在背景表面繪製它,改變函數調用的順序等,但它似乎沒有畫任何東西。正如你所看到的,enemy()方法應該繪製一個紅色的矩形,而不是繪製任何東西。遊戲運行良好,否則。我敢肯定它的一些簡單的我忽略了......令人沮喪!Pygame繪圖不能正常工作

import pygame 
from pygame.locals import * 
from pygame.time import * 
from pygame.font import * 
from pygame.draw import * 
import sys 
pygame.font.init() 
pygame.init() 


screen = pygame.display.set_mode((700,300)) 
pygame.display.set_caption('something') 

#-------------------------------------------------------------------- 

velX = 0 
velY = 0 
playerx = 20 
player_filename = 'player.png' 
player = pygame.image.load(player_filename) 
playery = 150 
clock = pygame.time.Clock() 

#-------------------------------------------------------------------- 

def draw():   
    global velX 
    global velY 
    global playerx 
    global playery 

    red = pygame.Color(255,0,0) 
    black = pygame.Color(0,0,0) 

    level = "1" 
    background = pygame.Surface(screen.get_size()) 
    background = background.convert() 
    background.fill((255,255,255)) 
    screen.blit(background, (0,0)) 

    playerx = playerx + velX 
    playery = playery + velY 
    screen.blit(player, (playerx,playery)) 
    font = pygame.font.Font(None, 36) 
    text = font.render(level, 1, black) 
    screen.blit(text, (670,10)) 
    pygame.display.flip() 

#-------------------------------------------------------------------- 

def enemySquares(): 
    enemySurf = pygame.Surface(screen.get_size()) 
    red = pygame.Color(255,0,0) 
    enemy = pygame.Rect(200,50,20,20) 
    pygame.draw.rect(enemySurf, red, enemy, 0) 

#-------------------------------------------------------------------- 

def collisionWithBorder(): 
    if playerx > 680 or playerx < 0: 
     pygame.quit() 
     sys.exit() 
    if playery > 280 or playery < 0: 
     pygame.quit() 
     sys.exit() 

#-------------------------------------------------------------------- 

def playerFunc(): 
    keys_down = pygame.key.get_pressed()  
    pygame.key.set_repeat(1,50) 
    time = 50/1000 
    global velX 
    global velY 
    direction = -1 

    if keys_down[K_d]: 
     direction = 0 
    if keys_down[K_a]: 
     direction = 3 
    if keys_down[K_w]: 
     direction = 1 
    if keys_down[K_s]: 
     direction = 2 

    if direction == 0: 
     velX = 50*time 
     velY = 0 
    if direction == 1: 
     velY = -50*time 
     velX = 0 
    if direction == 2: 
     velY = 50*time 
     velX = 0 
    if direction == 3: 
     velX = -50*time 
     velY = 0 

#-------------------------------------------------------------------- 

def main(): 
    while True: 

     for event in pygame.event.get(): 
      if event.type == QUIT: 
       pygame.quit() 
       sys.exit() 

     clock.tick(50)   
     collisionWithBorder() 
     draw() 
     playerFunc() 
     enemySquares() 

#-------------------------------------------------------------------- 

if __name__ == '__main__': 
    main() 

回答

2
def enemySquares(): 
    enemySurf = pygame.Surface(screen.get_size()) 
    red = pygame.Color(255,0,0) 
    enemy = pygame.Rect(200,50,20,20) 
    pygame.draw.rect(enemySurf, red, enemy, 0) 

上面的函數做畫一個紅色矩形 - 它創造,而不是 「屏幕」表面的新surace。

那裏只要放下你的enemySurf = ...線和改變pygame.draw.rect(enemySurf, red, enemy, 0)pygame.draw.rect(screen, red, enemy, 0)有下次調用矩形apear到pygame.display.flip()

+0

沒有工作,很遺憾。 did'nt似乎沒有任何效果:( – Arcticcu

+1

它沒有工作,因爲在你的主循環中,你調用'draw()',然後你調用'enemySquares()'。 )函數正在繪製背景並更新屏幕,所以在繪製背景並翻轉屏幕後繪製敵人矩形,然後在下一個draw()的循環中再次繪製該矩形。 ,在你重新更新屏幕之前,在每個幀上繪製矩形,如果你在'draw()'函數內部調用'enemySquares()',並且在調用翻轉屏幕之前它應該工作。 – Haz

+0

確實得到它與最終jsbueno說的一樣,我不好誤解他說的第一個 – Arcticcu