оценка пигмеев не отображается на экране - PullRequest
0 голосов
/ 21 января 2019
[![score: next to it is empty which is where the score is supposed to go][1]][1] 
import pygame
import os 
import random 
from pygame.locals import * # Constants
import math
import sys
import random

pygame.init()  

screen=pygame.display.set_mode((1280,700)) #(length,height)
screen_rect=screen.get_rect()
background = pygame.Surface(screen.get_size())
background = pygame.image.load('stage.png').convert()
Black=(0,0,0)

class Player(pygame.sprite.Sprite):
def __init__(self):
    super().__init__()  
    self.image = pygame.Surface((50,25)) 
    self.image.fill((0,0,128))    
    self.image =  pygame.image.load('sprite4.png')
    self.rect = self.image.get_rect(topleft =(20,615))  

    self.dist = 10

def update(self): # code to make the character move when the arrow keys are pressed
    if self.rect.right > 100:   # These are to make the player so move constantly
         self.rect.y += 1
         self.rect.x += 2
    if self.rect.bottom == 700:
         pygame.quit() 

    keys = pygame.key.get_pressed()
    if keys[K_LEFT]:
        self.rect.move_ip(-1,0)
    elif keys[K_RIGHT]:
        self.rect.move_ip(0.5,0)
    elif keys[K_UP]:
        self.rect.move_ip(0,-0.5)
    elif keys[K_DOWN]:
        self.rect.move_ip(0,1)
    self.rect.clamp_ip(screen_rect)
    #while self.rect == (20,615):
    if keys [K_SPACE]:
        self.rect = self.image.get_rect(topleft =(100,100))

player = Player()



class Enemy(pygame.sprite.Sprite): 
def __init__(self):
    super().__init__()
    self.image = pygame.Surface((50,25))  # This creates the surafce with the width and length given
    self.image.fill((128,0,0))   # This fills the surface with colour
    self.rect = self.image.get_rect(topleft=(300, 50))  # This is to make the image into a rectangle
    self.direction = 0

def update(self):
    self.rect.y += 1 if self.direction == 0 else -1
    if self.rect.bottom >= 600:
        self.direction = 1
    if self.rect.top <= 50:
        self.direction = 0

class Coin(pygame.sprite.Sprite): 
    def __init__(self):
    super().__init__()
    self.image =  pygame.image.load('coin.png')
    self.rect = self.image.get_rect(topleft=(250, 200))  

myfont = pygame.font.SysFont('OpenSans', 30)        
textsurface = myfont.render('Level ONE:   Greenland', False, (0, 0, 0))        
background.blit(textsurface,(500,10))

textsurface = myfont.render('Score:', False, (0, 0, 0))        
background.blit(textsurface,(10,10))

это код для добавления текста на экран, который работает, однако, когда счет добавляется после того, как «Счет» не работает, обновление счета находится внизу кода.

number_of_enemies = 10
min_enemy_x = 200 
max_enemy_x = 1200
enemy_x_range = max_enemy_x - min_enemy_x   
enemy_zone_width = enemy_x_range / number_of_enemies  
pixel_buffer = 40 

for i in range (number_of_enemies):   
     enemy = Enemy()    # calls the enemy class
     min_x = min_enemy_x + enemy_zone_width * i + pixel_buffer / 2 
     max_x = min_enemy_x + enemy_zone_width * (i + 1) - pixel_buffer / 2  
     enemy.rect.x = random.randrange(min_x, max_x) 

enemy.rect.y = random.randrange(50, 600)                          

enemy_list.add(enemy) # adds the enemy to the group
sprites.add(enemy)

for i in range (10):   # creates 5 enemy spirtes

    coin = Coin()    # calls the coin class
    coin.rect.x = random.randrange(200, 1200, 70) # makes the coin spawn random 
    coin.rect.y = random.randrange(300, 600)

coin_list.add(coin) 
sprites.add(coin)   


def main():  #my main loop 
running = True
clock = pygame.time.Clock()  
score = 0
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    sprites.update()
    screen.blit(background, (0, 0))
    sprites.draw(screen)
    clock.tick(100)  # Limit the frame rate to 60 FPS.
    pygame.display.flip()   #updates the whole screen



    player_hit_list = pygame.sprite.spritecollide(player, enemy_list, True)
    for enemy in player_hit_list:
        pygame.quit()
    player_hit_list = pygame.sprite.spritecollide(player, coin_list, True)


    for coin in player_hit_list:
        score = (score)+ 1
        print (score)

это место, где происходит обновление при касании каждой монеты, но оно не отображается на экране. Счет обновляется, так как он печатает счет при касании монет.

if __name__ == '__main__': 
main()
...