python - Pygame drawing doesn't work correctly -
i have problem drawing simple rectangle on screen. i've no clue why fails draw, i've tried several things: drawing on background surface, changing order of function calls etc. doesn't seem draw anything. can see, enemy() method supposed draw red rectangle, instead draws nothing. game runs fine otherwise. i'm sure simple i've overlooked.. frustrating!
import pygame pygame.locals import * pygame.time import * pygame.font import * 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: event in pygame.event.get(): if event.type == quit: pygame.quit() sys.exit() clock.tick(50) collisionwithborder() draw() playerfunc() enemysquares() #-------------------------------------------------------------------- if __name__ == '__main__': main()
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 draw red rectangle - on new surace creates, not on "screen" surface.
just drop enemysurf = ...
line there , change pygame.draw.rect(enemysurf, red, enemy, 0)
pygame.draw.rect(screen, red, enemy, 0)
have rectangle apear on next call pygame.display.flip()
Comments
Post a Comment