I've already coded my game without classes and am looking for a way to add a main function to the game in python.
just wanted to ask how to add main function in python for pygame
looking forward for the answer.. thanks!!
See the below pygame code.
Code:- [In Python]
import pygame
from pygame.locals import*
import random
from random import randint
import numpy as np
def times_table():
numbers = np.linspace(1,20,20)
x = random.randint(0,19)
y = random.randint(0,19)
answer = False
while answer == False:
for event in pygame.event.get():
print(numbers[x], numbers[y])
ans = numbers[x]*numbers[y]
user_input = input(f'What is {numbers[x]} x {numbers[y]}?: ')
if user_input == ans:
answer = True
print('correct!')
else:
answer = False
print('game over')
return answer
message_to_screen(f'What is {numbers[x]} x {numbers[y]}?: ', (0,0,0))
pygame.display.update()
def text_objects(text,color):
textSurface = font.render(text, True,color)
return textSurface, textSurface.get_rect()
def message_to_screen(msg, color):
textSurf, textRect = text_objects(msg,color)
#screen_text = font.render(msg, True, color)
#screen.blit(screen_text, [width/2,height/2])
textRect.center = ([width/2,height/2])
screen.blit(textSurf, textRect)
def pause():
paused = True
while paused:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
paused = False
elif event.key == pygame.K_q:
pygame.quit()
message_to_screen('press C to continue or Q to quit', (0,0,0))
pygame.display.update()
size = width, height = (800, 800)
road_w = int(width/1.6)
roadmark_w = int(width/80)
right_lane = width/2 + road_w/4
left_lane = width/2 - road_w/4
speed = 1
#self.hitbox = (self.x + 20, self.y, 900, 900)
pygame.init()
running = True
# set window size
screen = pygame.display.set_mode(size)
font = pygame.font.SysFont(None, 25)
# set title
pygame.display.set_caption("Fun Crazy Maths")
# set background colour
screen.fill((60, 220, 0))
# apply changes
pygame.display.update()
# load player vehicle
car = pygame.image.load("Capture3.PNG")
car_loc = car.get_rect()
car_loc.center = right_lane, height*0.8
# load enemy vehicle
car2 = pygame.image.load("Capture.PNG")
car2_loc = car2.get_rect()
car2_loc.center = left_lane, height*0.2
x = pygame.draw.rect(
screen,
(80, 80, 80),
(700, 0, road_w, height))
y = pygame.draw.rect(
screen,
(80, 80, 80),
(-400, 0, road_w, height))
counter = 0
# .game .loop
while running:
counter += 1
if counter == 5000:
speed += 0.15
counter = 0
print("level up", speed)
# animate enemy vehicle
car2_loc[1] += speed
if car2_loc[1] > height:
if random.randint(0,1) == 0:
car2_loc.center = right_lane, -200
else:
car2_loc.center = left_lane, -200
# end game
#if car_loc[0] == car2_loc[0] and car2_loc[1] > car_loc[1] - 150:
#print("GAME OVER! YOU lOST!")
#break
if car_loc.colliderect(car2_loc):
print("GAME OVER! YOU lOST!")
break
if car_loc.colliderect(x):
print("GAME OVER! YOU lOST!")
break
if car_loc.colliderect(y):
print("GAME OVER! YOU lOST!")
break
#times_table()
for event in pygame.event.get():
if event.type == quit:
RUNNING = False
if event.type == KEYDOWN:
if event.key in [K_a, K_LEFT]:
times_table()
car_loc = car_loc.move([-int(road_w/2), 0])
if event.key in [K_d, K_RIGHT]:
car_loc = car_loc.move([int(road_w/2), 0])
times_table()
if event.key == pygame.K_p:
pause()
pygame.draw.rect(
screen,
(50, 50, 50),
(width/2-road_w/2, 0, road_w, height))
pygame.draw.rect(
screen,
(255, 240, 60),
(width/2 - roadmark_w/2, 0, roadmark_w, height))
# left line
pygame.draw.rect(
screen,
(255, 255, 255),
(width/2 - road_w/2 + roadmark_w*2, 0, roadmark_w, height))
# right line
pygame.draw.rect(
screen,
(255, 255, 255),
(width/2 + road_w/2 - roadmark_w*3, 0, roadmark_w, height))
screen.blit(car, car_loc)
screen.blit(car2, car2_loc)
pygame.display.update()
pygame.quit()
I'm the newbie in stackoverflow advice is acceptable ..