1
votes

I have a problem with drawing a simple rectangle on the screen. I've no clue why it fails to draw, I've tried several things: drawing it on the background surface, changing the order of the function calls etc. but it just doesn't seem to draw anything. As you can see, the enemy() method is supposed to draw a red rectangle, instead it draws nothing. The game runs fine otherwise. I'm sure its something simple I've overlooked.. frustrating!

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()
1

1 Answers

2
votes
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)

The function above does draw a red rectangle - on a new surace it creates, not on the "screen" surface.

Just drop your enemySurf = ... line there and change pygame.draw.rect(enemySurf, red, enemy, 0) to pygame.draw.rect(screen, red, enemy, 0) to have the rectangle apear on the next call to pygame.display.flip()