I am creating a game called "Survival Island" and have just created the start screen.
The pygame lags too much after doing an event (takes time to respond).
Here is my source code:
#packages
import pygame
import sys
from sys import exit
#initialization
pygame.init()
#display surf
width = 600
height = 400
surface = pygame.display.set_mode((width,height))
clock = pygame.time.Clock()#for fps
#caption
pygame.display.set_caption('Survival Island')
#variables
mousex = 0
mousey = 0
#booleans
play = True #entered playmode
canQuitOnStart = True #game can be quitted on start
drawStartScreen = True #start screen drawed
running = True # game is running
#definitions
def quitOnStart(): #quitting the game
#can be seen if rect is drawn [pygame.draw.rect(surface,(0,0,255),(550,350,40,40))]
global mousex,mousey,running
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN: #quit on pressing x on start screen
if mousex > 550 and mousey > 350 and mousex <590 and mousey <390:
print('Exit1')
running = False
def drawStart(): #drawing start menu
START_Image = pygame.image.load('START_Image.png').convert()
surface.blit(START_Image,(0,0))
pygame.display.update()
def playGame():
#play on clicking on "play"
global mousex,mousey,canQuitOnStart,drawStartScreen
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if mousex > 415 and mousey >190 and mousex <70 and mousey <30: # can be seen if rect is drawn [pygame.draw.rect(surface,(0,0,255),(415,190,70,30))]
canQuitOnStart = False
drawStartScreen = False
screen.fill((0,0,0))
pygame.display.update()
if drawStartScreen == True:
drawStart()
def main():
if play == True:
playGame()
if canQuitOnStart == True:
quitOnStart()
#main loop
while running:
#get mouse position
mousex,mousey = pygame.mouse.get_pos()
# fps is 60
clock.tick(120)
# quit button event
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# main function
if __name__ == '__main__':
main()
pygame.quit()#quits after event
After running, The pygame window displays the image. It takes several tries to close the window after running(the 'X' on the bottom right corner)
I am a newbie programmer so.. I want some pygame courses to do (please suggest some).
Thank you !!!