0

this is my entire code

import pygame
from rgb import *
from pygame.constants import *
import time
pygame.init()
new=True
start = False
land_pos = [0, 600]
height=700
width=1500
center=(780, 450)
ball_pos=[51,540]
sky_pos=[0,-1000]
grass1_pos=[200,490]
grass2_pos=[1900,510]
grass3_pos=[2300,500]
grass4_pos=[1000,498]
grass5_pos=[2700,532]

main_ui = pygame.image.load("venv/graphics/UI_main.png")
sky=pygame.image.load("venv/graphics/sky.jpg")
land=pygame.image.load("venv/graphics/grass_land.PNG")
grass1=pygame.image.load("venv/graphics/grass1.png")
grass2=pygame.image.load("venv/graphics/grass2.png")
grass3=pygame.image.load("venv/graphics/grass3.png")
grass4=pygame.image.load("venv/graphics/grass4.png")
grass5=pygame.image.load("venv/graphics/grass5.png")
pause_ui=pygame.image.load("venv/graphics/PAUSE_MENU.png")
land = pygame.transform.scale(land, (4000, 300))
main_ui=pygame.transform.scale(main_ui,(1800,900))
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.update()


def bliti(x,y,z):
    screen.blit(z, (x,y))
running = True
while running:
    new1=True
    run = True
    keys = pygame.key.get_pressed()
    for event in pygame.event.get():
            if keys[K_ESCAPE]:
                while run:
                    bliti(780,450,pause_ui)
                    rect1 = pygame.draw.rect(screen, white, (115, 70, 200, 100))
                    rect2 = pygame.draw.rect(screen, white, (405, 70, 200, 100))
                    pygame.display.update()

                    if event.type == pygame.MOUSEBUTTONUP:
                        pos1 = pygame.mouse.get_pos()
                        if rect1.collidepoint(pos1):
                            print("ihihh")
                            run=False
                        if rect2.collidepoint(pos1):
                            new=True
                            start=False


    if new:
        rect = pygame.draw.rect(screen, white, (115, 70, 200, 100))
        bliti(0, 0, main_ui)

        if event.type == pygame.MOUSEBUTTONUP :
            pos = pygame.mouse.get_pos()
            if rect.collidepoint(pos):
                start=True
                new = False
    pygame.display.update()

    if start:
        if keys[K_LEFT]:
            if ball_pos[0]>=0 and sky_pos[0]==0:
                if ball_pos[0] > 51:
                    ball_pos[0]-=1
        if keys[K_RIGHT]:
            if sky_pos[0] == -4000 and ball_pos[0] <= 1484:
                if ball_pos[0] > 400:
                    ball_pos[0]+=1
        times=time.localtime()
        if keys[K_LEFT]:
            if sky_pos[0] == -4000 and ball_pos[0] > 401:
                ball_pos[0]-=1
        times=time.localtime()
        screen.blit(sky, sky_pos)
        bliti(land_pos[0], land_pos[1],land)

        class Circle:
            pygame.draw.circle(screen, green, ball_pos, 60)


        bliti(grass1_pos[0], grass1_pos[1], grass1)
        bliti(grass2_pos[0], grass2_pos[1], grass2)
        bliti(grass3_pos[0], grass3_pos[1], grass3)
        bliti(grass4_pos[0], grass4_pos[1], grass4)
        bliti(grass5_pos[0], grass5_pos[1], grass5)

        if not ball_pos[0] <= 400:
            if land_pos[0] != -4000:
                if keys[K_RIGHT]:
                    land_pos[0] -= 1
                    grass1_pos[0]-=1
                    grass2_pos[0]-=1
                    grass3_pos[0] -= 1
                    grass4_pos[0] -= 1
                    grass5_pos[0] -= 1
            if land_pos[0] != 0:
                if not ball_pos[0] > 401:
                    if keys[K_LEFT]:
                        land_pos[0] += 1
                        grass1_pos[0] += 1
                        grass2_pos[0] += 1
                        grass3_pos[0] += 1
                        grass4_pos[0] += 1
                        grass5_pos[0] += 1
        if not ball_pos[0] <= 400:
            if sky_pos[0] != -4000:
                if keys[K_RIGHT]:
                    sky_pos[0] -= 1
            if sky_pos[0] != 0:
                if not ball_pos[0] > 401:
                    if keys[K_LEFT]:
                        sky_pos[0] += 1
        else:
            if keys[K_RIGHT]:
                ball_pos[0] += 1



        pygame.display.update()


pygame.quit()

the problem i am having is over here

running = True
while running:
    new1=True
    run = True
    keys = pygame.key.get_pressed()
    for event in pygame.event.get():
            if keys[K_ESCAPE]:
                while run:
                    bliti(780,450,pause_ui)
                    rect1 = pygame.draw.rect(screen, white, (115, 70, 200, 100))
                    rect2 = pygame.draw.rect(screen, white, (405, 70, 200, 100))
                    pygame.display.update()

                    if event.type == pygame.MOUSEBUTTONUP:
                        pos1 = pygame.mouse.get_pos()
                        if rect1.collidepoint(pos1):
                            print("ihihh")
                            run=False
                        if rect2.collidepoint(pos1):
                            new=True
                            start=False


    if new:
        rect = pygame.draw.rect(screen, white, (115, 70, 200, 100))
        bliti(0, 0, main_ui)

i am making a white rectangle and making it so that when i click it the game either resumes or gets us to the main menu.

but instead my code crashes when i click it

if event.type == pygame.MOUSEBUTTONUP:
        pos1 = pygame.mouse.get_pos()
    if rect1.collidepoint(pos1):
        print("hi")
        run=False
    if rect2.collidepoint(pos1):
        new=True
        start=False

here the print("hi") statement is also not running plz help. Thank You in advance!

Rabbid76
  • 177,135
  • 25
  • 101
  • 146
  • Why do you run an event loop in the event loop? Change the game design! Use 1 application loop and 1 event loop. Add variables that store the stated of the game. For instance set a state when ESC is pressed. – Rabbid76 Jan 25 '21 at 16:26
  • what do you exactly mean? i didnt understand plz explain. btw i want the pause menu to stay in the place thats why i ran a while loop to keep it on the screen – Teerth Sonawani Jan 26 '21 at 08:02

0 Answers0