0

I have looked at many YouTube videos and information online but is seems I can't find the answer. Every time I run the code the text appears with a zero but when clicking the enemy to earn points the number 1 overlaps with the zero and it doesn't change. This problem repeats over and over again. What do/can I do? I am new to the python code and there might be some unnecessary code so please bare that in mind if you see a rookie mistake thank you.

import pygame
import time
import random 
pygame.init()
from pygame.locals import *

white = (255,255,255)
window = pygame.display.set_mode((800,600))
pygame.display.set_caption("Extended Essay game")
BADGUY = pygame.image.load('enemy.png')
pygame.font.init()


#Enemy char
class Enemy():
   def __init__(self, x, y):
     self.x = x
     self.y = y


def main():
  run = True
  FPS = 60
  x = 0
  enemies = 0
  clock = pygame.time.Clock()
  font = pygame.font.Font('freesansbold.ttf', 32)
  window.fill((255,255,255))
  window.blit(BADGUY,(0,0))

  def update():
     text = font.render("Enemies: " + str(enemies), True, (0,0,0))
     window.blit(text, (200, 300))
     pygame.display.update() 

    
  while run:
     clock.tick(FPS)
        
     for event in pygame.event.get():
         if event.type == pygame.QUIT:
             run = False
         if event.type == pygame.MOUSEBUTTONDOWN:
             if event.button == 1:
                 window.blit(BADGUY, (random.randint(0, 700), random.randint(0 , 700)))
                 enemies += 1
                    
    update() 

main()
KING
  • 3
  • 1

1 Answers1

0

You need to clear the screen before putting new stuff on it. Put this at the beginning of update() or right before it is called in the loop:

window.fill((255, 255, 255))
Seth
  • 2,214
  • 1
  • 7
  • 21