Вы должны сделать рисунок в основном приложении l oop. Основное приложение l oop непрерывно краснеть aws всей сцены. Основное приложение l oop должно выполнять следующие действия:
- обрабатывать события и менять игровое состояние в зависимости от событий и времени
- очищать дисплей
- Нарисуйте сцену
- Обновите отображение
Создайте начальную партитуру поверхности перед основным применением l oop. Создать новый счет Поверхность, когда счет изменился. Нарисуйте болячку в основном приложении l oop:
def main():
global width, rows, s, snack
title()
width = 500
rows = 20
win = pygame.display.set_mode((width, width)) # Creates the screen, sets resolution
s = snake((0,255,0), (10,10)) # Sets snake colour to green and starting position
snack = cube(randomSnack(rows, s), colour=(255,0,0))
score = 0
flag = True
white = (255, 255, 255)
# create score surface
message = score
font = pygame.font.Font('Fonts/PressStart2P-vaV7.ttf', 30)
text = font.render(str(message), True, (255, 255, 255))
clock = pygame.time.Clock() # Creates clock object that can change refresh rate
while flag:
pygame.time.delay(50) # Delays by 50 ms, makes it so snake cannot move 10 blocks a second
clock.tick(10) # Sets max refresh rate
# handle the events
for event in pygame.event.get():
if event.type == pygame.QUIT:
flag = False
s.move()
if s.body[0].position == snack.position:
s.addCube()
score += 1
# update score surface
message = score
text = font.render(str(message), True, (255, 255, 255))
print(score)
snack = cube(randomSnack(rows, s), colour=(255,0,0)) # Randomizes snack position and sets colour as red
for x in range(len(s.body)):
if s.body[x].position in list(map(lambda z:z.position,s.body[x+1:])): # This checks if you die
print('Score: ', len(s.body))
s.reset((10,10))
break
# clear the display
# [...]
# draw the score
win.blit(text, (120,30))
# draw the snake etc.
# [...]
pygame.display.flip()