I'm very new to python and Pygame and am trying to create a game. I would like a "powerup" to appear after a set time, then repeat every time the timer hits so many seconds.
In the code, the "powerup" appears and drops down the screen after 5 seconds, the timer resets, but the next time the timer reaches 5 seconds, it continues to count and no "powerup" is produced.
I've spent hours trying to work out why this isn't working.
Any help very gladly received, thank you.
import pygame
import random
WIDTH = 480
HEIGHT = 600
FPS = 60
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
time_difference = 0
font_name = pygame.font.match_font('arial')
def draw_text(surf, text, size, x, y):
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, WHITE)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
class HealthPack(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 40))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.radius = 50
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-150, -100)
self.speedy = 6
self.speedx = 0
def update(self):
self.rect.y += self.speedy
if self.rect.top > HEIGHT:
self.kill()
all_sprites = pygame.sprite.Group()
healthpack = HealthPack()
running = True
while running:
clock.tick(FPS)
time_difference = pygame.time.get_ticks()
if time_difference >= 5000:
all_sprites.add(healthpack)
time_difference -= 5000
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
all_sprites.update()
screen.fill(BLACK)
all_sprites.draw(screen)
draw_text(screen, str(time_difference), 18, WIDTH / 2, 55)
pygame.display.flip()
pygame.quit()
Aucun commentaire:
Enregistrer un commentaire