Чёрный экран в pygame при запуске функции
Код-
import pygame
from pygame.locals import *
import random
from time import sleep as s
pygame.mixer.pre_init(44100, -16, 2, 512)
pygame.init()
pygame.font.init()
#-utf-8-
# ДИсплей
display_width = 800
display_height = 600
# КАРТИНКИ КатусОВ
cactus_img = [pygame.image.load('D:/PyGame/DATA/кактусы/Cactus0.png'),pygame.image.load('D:/PyGame/DATA/кактусы/Cactus1.png'),pygame.image.load('D:/PyGame/DATA/кактусы/Cactus2.png')]
# ОПЦИИ КАКТУСА
cactus_options = [69,445,37,410,40,420]
#
land = pygame.image.load('D:/PyGame/Data/ландшафт/Land.jpg')
dino_img = [pygame.image.load('D:/PyGame/Data/динозавр/Dino0.png'),pygame.image.load('D:/PyGame/Data/динозавр/Dino1.png'),pygame.image.load('D:/PyGame/Data/динозавр/Dino2.png')
,pygame.image.load('D:/PyGame/Data/динозавр/Dino3.png'),pygame.image.load('D:/PyGame/Data/динозавр/Dino4.png')]
stone_img = [pygame.image.load('D:/PyGame/DATA/объекты/Stone0.png'),pygame.image.load('D:/PyGame/DATA/объекты/Stone1.png')]
cloud_img = [pygame.image.load('D:/PyGame/DATA/объекты/Cloud0.png'),pygame.image.load('D:/PyGame/DATA/объекты/Cloud1.png')]
health_img = pygame.image.load('D:/PyGame/Data/объекты/heart.png')
health_img = pygame.transform.scale(health_img,(30,30) )
pygame.mixer.music.load('D:/PyGame/Data/audio/фон.mp3')
pygame.mixer.music.set_volume(0.4)
jump_sound = pygame.mixer.Sound('D:/PyGame/Data/audio/рык.wav')
fall_sound = pygame.mixer.Sound('D:/PyGame/Data/audio/падение.wav')
hp_ = pygame.mixer.Sound('D:/PyGame/Data/audio/hp-.wav')
loss = pygame.mixer.Sound('D:/PyGame/Data/audio/loss.wav')
hearts_plus_sound = pygame.mixer.Sound('D:/PyGame/Data/audio/hp+.wav')
button_sound = pygame.mixer.Sound('D:/PyGame/Data/audio/button.wav')
img_counter = 0
health = 2
def show_menu():
menu_bg = pygame.image.load('D:/PyGame/Data/ландшафт/Menu.jpg')
show = True
start_btn = Button(300,70)
while show:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
display.blit(menu_bg,(0,0))
start_btn.draw(300,200,'Начать игру.')
pygame.display.update()
clock.tick(60)
class Button:
def __init__(self,width, heigth):
self.width = width
self.heigth = heigth
self.inactive_clr = (13,162, 58 )
self.active_clr = (23,204,58)
def draw(self, x , y , message, action=None , font_size=30):
mouse= pygame.mouse.get_pos()
click= pygame.mouse.get_pressed()
print(mouse)
if x< mouse[0] < x + self.width and y< mouse[1] < y + self.heigth:
pygame.draw.rect(display,self.active_clr , (x,y,self.width, self.heigth))
if click[0] == 1:
pygame.mixer.Sound.play(button_sound)
pygame.time.delay(300)
if action is not None:
action()
else:
pygame.draw.rect(display, self.inactive_clr , (x,y,self.width, self.heigth))
print_text(message=message,x=x+10,y=y+10,font_size= font_size)
class object():
def __init__(self, x, y, width,image,speed):
self.x = x
self.y = y
self.widht = width
self.image = image
self.speed = speed
def move(self):
if self.x >= -self.widht:
display.blit(self.image,(self.x,self.y))
# pygame.draw.rect(display,(224,91,22),(self.x, self.y, self.widht, self.height))
self.x -= self.speed
return True
else:
return False
def return_self(self,radius,y,widht,image):
self.x = radius
self.y = y
self.widht = widht
self.image = image
display.blit(self.image,(self.x,self.y))
# ИГРОК
user_width = 60
user_height = 100
user_x = display_width // 3
user_y = display_height - user_height - 100
# Кактус
cactus_widht = 20
cactus_height = 70
cactus_x = display_width - 50
cactus_y = display_height - cactus_height - 100
# НаЗВАНИЕ ИГРЫ
display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Dyno")
# ИКОНКА ИГРЫ
icon = pygame.image.load("D:/PyGame/DATA/иконка/icon.png")
pygame.display.set_icon(icon)
# КЛОКИ
clock = pygame.time.Clock()
# РАЗРЕШЕНИЕ ПРЫЖКА
make_jump = bool(False)
# КОЛИЧЕСТВО ПРЫЖКОВ
jump_counter = 30
# СТАРТ ИГРЫ
img_counter = 5
#
scores = int(0)
max_scores = int(open('D:/PyGame/Data/bin/max_score.txt','r').read())
max_above = 0
# above_cactus = False
def game_cycle():
cactus_arr = []
global make_jump, button
pygame.mixer.music.play(-1)
stone, cloud = open_random_objects()
create_cactus_arr(cactus_arr)
game = bool(True)
heart = object(display_width,280,30,health_img,4)
button = Button(150,50)
while game == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
pause()
move_objects(stone,cloud)
hearts_plus(heart)
if keys[pygame.K_SPACE]:
make_jump = True
if make_jump:
jump()
display.blit(land,(0,0))
count_scores(cactus_arr)
print_text(f'Очки: {str(scores)}',600,10)
button.draw(20,100,'Ти дурак!')
heart.move()
drow_array(cactus_arr)
draw_dino()
if check_collision(cactus_arr):
game= False
chow_health()
pygame.display.update()
clock.tick(70)
return game_over()
#Create cactus
def create_cactus_arr(array):
choice1 = random.randrange(0,3)
img = cactus_img[choice1]
widht = cactus_options[choice1 * 2]
height = cactus_options[choice1 * 2 + 1]
array.append(object(display_width + 20, height, widht, img, 4))
choice1 = random.randrange(0,3)
img = cactus_img[choice1]
widht = cactus_options[choice1 * 2]
height = cactus_options[choice1 * 2 + 1]
array.append(object(display_width + 300, height, widht, img, 4))
choice = random.randrange(0,3)
img = cactus_img[choice1]
widht = cactus_options[choice1 * 2]
height = cactus_options[choice1 * 2 + 1]
array.append(object(display_width + 600, height, widht, img, 4))
#Draw array
def drow_array(array):
for cactus in array:
check = cactus.move()
if not check:
object_return(array,cactus)
def find_radius(array):
maximum = max(array[0].x, array[1].x, array[2].x)
if maximum < display_width:
radius = display_width
if radius - maximum < 50:
radius += 280
else:
radius = maximum
choice = random.randrange(0,5)
if choice == 0:
radius += random.randrange(10,20)
else:
radius += random.randrange(200,350)
return radius
#jump
def jump():
global user_y, make_jump, jump_counter
if jump_counter >= -30:
user_y -= jump_counter / 2.5
jump_counter -= 1
if jump_counter == 30:
pygame.mixer.Sound.play(jump_sound)
if jump_counter == -30:
pygame.mixer.Sound.play(fall_sound)
else:
jump_counter = 30
make_jump = False
def open_random_objects():
choice = random.randrange(0,2)
img_of_stone = stone_img[choice]
choice = random.randrange(0,2)
img_of_cloud = cloud_img[choice]
stone = object(display_width, display_height-80, 10, img_of_stone, 4)
cloud = object(display_width, 80, 70, img_of_cloud, 1)
return stone, cloud
def move_objects(stone,cloud):
check = stone.move()
if not check:
choice = random.randrange(0,2)
img_of_stone = stone_img[choice]
stone.return_self(display_width, 500 + random.randrange(10,80), stone.widht, img_of_stone )
check = cloud.move()
if not check:
choice = random.randrange(0,2)
img_of_cloud = cloud_img[choice]
cloud.return_self(display_width, random.randrange(10,200), cloud.widht, img_of_cloud )
def draw_dino():
global img_counter
if img_counter == 25:
img_counter = 0
display.blit(dino_img[img_counter//5], (user_x,user_y))
img_counter += 1
def print_text(message,x,y,font_color=(0,0,0),font_type='D:/PyGame/DATA/шрифты/font.ttf',font_size=30):
font_type = pygame.font.Font(font_type,font_size)
text= font_type.render(message, True, font_color)
display.blit(text, (x,y))
def object_return(objects,obj):
radius = find_radius(objects)
choice = random.randrange(0,3)
img = cactus_img[choice]
width = cactus_options[choice * 2]
height = cactus_options[choice * 2 +1]
obj.return_self(radius,height,width,img)
def check_collision(barriers):
for barrier in barriers:
if barrier.y == 449: # Маленький кактус
if not make_jump:
if barrier.x <= user_x + user_width -35 <= barrier.x + barrier.widht:
if check_health():
object_return(barriers,barrier)
return False
else:
return True
elif jump_counter >= 0:
if user_y + user_height -5 >= barrier.y:
if barrier.x <= user_x + user_width - 27 <= barrier.x + barrier.widht:
if check_health():
object_return(barriers,barrier)
return False
else:
return True
else:
if user_y + user_height -10 >= barrier.y:
if barrier.x <= user_x <= barrier.x + barrier.widht:
if check_health():
object_return(barriers,barrier)
return False
else:
return True
else:
if not make_jump:
if barrier.x <= user_x + user_width -5 <= barrier.x + barrier.widht:
if check_health():
object_return(barriers,barrier)
return False
else:
return True
elif jump_counter == 10:
if user_y + user_height - 5 >= barrier.y:
if barrier.x <= user_x + user_width - 5 <= barrier.x + barrier.widht:
if check_health():
object_return(barriers,barrier)
return False
else:
return True
elif jump_counter >= -1:
if user_y + user_height - 5 >= barrier.y:
if barrier.x <= user_x + user_width - 35 <= barrier.x + barrier.widht:
if check_health():
object_return(barriers,barrier)
return False
else:
return True
else:
if user_y + user_height -10 >= barrier.y:
if barrier.x <= user_x + 5 <= barrier.x + barrier.widht:
if check_health():
object_return(barriers,barrier)
return False
else:
return True
return False
def pause():
pygame.mixer.music.pause()
paused = True
while paused:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
print_text('Пауза. Нажмите на Enter, чтобы продолжить...', 160,300)
keys = pygame.key.get_pressed()
if keys[pygame.K_RETURN]:
paused = False
pygame.display.update()
clock.tick(15)
pygame.mixer.music.unpause()
def game_over():
global scores, max_scores
pygame.mixer.music.stop()
if scores > max_scores:
max_scores = scores
f = open('D:/PyGame/Data/bin/max_score.txt','w')
f.write(str(max_scores))
stopped = True
while stopped:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
print_text('Game over!', 200,200,font_size=60,font_color=(247,27,27))
print_text('Нажмите на Enter, чтобы начать заново, а чтобы выйти',80,350)
print_text('нажмите Esc...',200,400)
print_text(f'Макс: {max_scores}',600,35)
keys = pygame.key.get_pressed()
if keys[pygame.K_RETURN]:
return True
if keys[pygame.K_ESCAPE]:
return False
pygame.display.update()
clock.tick(15)
def count_scores(barriers):
global scores, max_above
above_cactus = 0
if -20 <= jump_counter < 25:
for barrier in barriers:
if user_y + user_height - 5 <= barrier.y:
if barrier.x <= user_x <= barrier.x + barrier.widht:
above_cactus += 1
elif barrier.x <= user_x + user_width <= barrier.x + barrier.widht:
above_cactus += 1
max_above = max(max_above,above_cactus)
else:
if jump_counter == -30:
scores += max_above
max_above = 0
def chow_health():
global health
x= 20
show = 0
while show != health:
display.blit(health_img, (x,20) )
x = x + 40
show += 1
def check_health():
global health
health -= 1
if health < 1:
pygame.mixer.Sound.play(loss)
return False
else:
pygame.mixer.Sound.play(hp_)
return True
def hearts_plus(heart):
global health, user_x, user_y, user_width, user_height
if heart.x <= -heart.widht:
radius = display_width + random.randrange(500,3000)
heart.return_self(radius, heart.y + pribavka , heart.widht, heart.image)
if user_x <= heart.x <= user_x + user_width:
if user_y <= heart.y <= user_y + user_height:
pygame.mixer.Sound.play(hearts_plus_sound)
if health < 5:
health += 1
print_text('True',50,100)
ch = random.randrange(-1,1)
if ch < 0:
pribavka = random.randrange(0,20)
else:
pribavka = random.randrange(-20,0)
radius = display_width + random.randrange(500,3000)
heart.return_self(radius, heart.y + pribavka , heart.widht, heart.image)
show_menu()
while game_cycle():
scores = 0
make_jump = False
jump_counter = 30
user_y = display_height - user_height - 100
health = 3
x= 20
pygame.quit()
quit()
#End
Файлы