Reputation: 33
I'm new to pygame. I have made a code about the game similar to dinosaur game when the internet is interupt. But when I convert from .py to .exe it just open in a moment and close immediately although main script runs fine. The command I've been using was pyinstaller file.py --onefile which make a executable that keeps closing immediately when i run it. I have checked wheather there is a error but it doesn't( i dont you any image.png outside)
import pygame
import time
import random
import sys
pygame.init()
white = (255,255,255)
yellow = (255,255,102)
black = (0,0,0)
red = (235, 64, 52)
width = 1000
height = 600
clock = pygame.time.Clock()
font_style = pygame.font.SysFont(None, 30)
score_font = pygame.font.SysFont('monospace', 35)
screen = pygame.display.set_mode((width,height))
def message(msg, color):
mesg = font_style.render(msg, True, color)
screen.blit(mesg, (round(width/3), round(height/3)))
bush_speed = 1
def your_score(score1):
value = score_font.render("Your Score: " + str(score1), True, yellow)
screen.blit(value, [0, 0])
game_over = False
def collision(player_pos,bush_pos):
p_x = player_pos[0]
p_y = player_pos[1]
b_x = bush_pos[0]
b_y = bush_pos[1]
if (b_x >= p_x and b_x < p_x + 50 ) or (b_x == p_x) or (p_x >= b_x and p_x < b_x + 50) :
if (b_y >= p_y and b_y < p_y + 100) or (b_y == p_y) :
return True
return False
def bush(bush_height,bush_pos):
pygame.draw.rect(screen,red, [bush_pos[0],bush_pos[1],50,bush_height])
def speed(bush_speed,score1):
if score1 < 3:
bush_speed = 50
elif score1 < 20:
bush_speed = 4
elif score1 < 30:
bush_speed = 5
else:
bush_speed = 15
def game_loop():
game_close = False
player_pos = [200,500]
player_jump =[0,0]
player_height = 100
player_size = 50
score1 = 0
game_over = False
bush_size = 50
bush_height = (round(random.randint(50,100)/10) *10)
bush_pos = [width - bush_size, height - bush_height]
while not game_over:
while game_close:
screen.fill(white)
message("You Lost! Press C-Play Again or Q-Quit", red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
game_loop()
if event.key == pygame.K_q:
game_over = True
game_close = False
screen.fill(black)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if player_pos[0] > 0:
if event.key == pygame.K_LEFT:
player_pos[0] -= 50
if player_pos[0] < width - player_size:
if event.key == pygame.K_RIGHT:
player_pos[0] +=50
if player_pos[1] == height -100:
if event.key == pygame.K_SPACE:
player_pos[1] -=200
player_jump[0] = 1
if player_pos[1] < height - player_height :
player_pos[1] += player_jump[0]
bush_pos[0] -= bush_speed*2
clock.tick(200)
if bush_pos[0] < 0 :
bush_height = (round(random.randint(50,100)/10) *10)
bush_pos = [width - bush_size, height - bush_height]
score1 +=1
speed(bush_speed,score1)
if collision(player_pos,bush_pos):
game_close = True
bush(bush_height,bush_pos)
your_score(score1)
pygame.draw.rect(screen, red, (player_pos[0],player_pos[1],player_size,player_height))
pygame.display.update()
pygame.display.flip()
game_loop()
sorry if my english is bad and my knowledge of code is too limited
Upvotes: 3
Views: 195
Reputation: 986
Fonts are often the issue here, same goes for audio files. Try replacing:
font_style = pygame.font.SysFont(None, 30)
with
font_style = pygame.font.SysFont("Arial", 30)
Arial is pretty much always a "safe bet" in these cases. Also before converting with pyinstaller change your file name to main.py
. Also do not forget to update pygame
to 2.0.0.dev6
or newer.
It should work now.
Upvotes: 2