0

I have a problem in my code that I want one of your guys to answer.

This is my code:

import pygame
import time

pygame.init()
pygame.mixer.init()

screen = pygame.display.set_mode((1280,720))
pygame.display.set_caption('Title Screen')

titleim = pygame.image.load(r'C:\Users\kp876\Desktop\Temorairy\Kill, Buy, Repeat title screen.png')
menuim = pygame.image.load(r'C:\Users\kp876\Desktop\Menu Screen.png')
playim = pygame.image.load(r'C:\Users\kp876\Desktop\play button.png')
stickmanim = pygame.image.load('stickmanfightpose.png')
shopim = pygame.image.load('shop.png')

rplayim = pygame.transform.scale(playim,(640,360))
rshopim = pygame.transform.scale(shopim,(35,30))

popSound = pygame.mixer.Sound('Pop sound effect.wav')
menumusic = pygame.mixer.music.load('awesomeness.mp3')

screen.blit(titleim,(0,0))
pygame.display.update()

running = True
running_title = True
mouse = False

coins = 10

def redrawTitle():
    screen.fill((0,0,0))
    screen.blit(titleim,(0,0))

def fade(w,h):
    val1 = 1
    fade = pygame.Surface((w,h))
    fade.fill((0,0,0))
    for alpha in range(0, 300, 5):
        fade.set_alpha(alpha)
        redrawTitle()
        screen.blit(fade,(0,0))
        pygame.display.update()

def mainmenuText():
    font = pygame.font.Font("C:\Windows\Fonts\Arial.ttf", 100)
    text = font.render('Press Play',True,(210,200,200))
    playRect = pygame.Rect((555,480),(150,120))
    pygame.draw.rect(screen, (0,0,0), playRect)
    screen.blit(menuim,(0,0))
    screen.blit(text,(380,340))
    screen.blit(rplayim,(310,360))
    pygame.display.update()

def shop():
    pistol = False
    rifle = False
    machinegun = False
    white = (255,255,255)
    screen.fill(white)
    global coins
    
    shopfont = pygame.font.Font("C:\Windows\Fonts\Arial.ttf", 60)
    shoptxt = shopfont.render('Shop', True,(0,0,0))
    coinstxt = shopfont.render('Coins:' + str(coins) + '!',True,(0,0,0))
    
    namefont = pygame.font.Font("C:\Windows\Fonts\Arial.ttf", 35)
    
    pistoltxt = namefont.render('Pistol', True, (0,0,0))
    rifletxt = namefont.render('Rifle', True, (0,0,0))
    machineguntxt = namefont.render('Machine Gun', True, (0,0,0))
    
    mainshopR = pygame.Rect((0,0),(1280,720))
    pistolR = pygame.Rect((50,150),(200,200))
    rifleR = pygame.Rect((450,150),(200,200))
    machinegunR = pygame.Rect((850,150),(200,200))
    
    pygame.draw.rect(screen, white, mainshopR)
    pygame.draw.rect(screen, (128,128,128),pistolR)
    pygame.draw.rect(screen, (128,128,128),rifleR)
    pygame.draw.rect(screen, (128,128,128),machinegunR)
    
    screen.blit(shoptxt,(500,30))
    screen.blit(pistoltxt,(100,220))
    screen.blit(rifletxt,(500,220))
    screen.blit(machineguntxt,(850,220))
    screen.blit(coinstxt,(50,30))
    pygame.display.update()
    if pistol:
        pygame.draw.rect(screen, (0,128,0),pistolR)
    if rifle:
        pygame.draw.rect(screen, (0,128,0),rifleR)
    if machinegun:
        pygame.draw.rect(screen, (0,128,0),machinegunR)
    
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            spot1 = pygame.mouse.get_pos()
            if spot1[0] > 50 and spot1[0] < 250 and spot1[1] > 150 and spot1[1] < 350:
                if not pistol:
                    if coins >= 3:
                        pistolR = pygame.Rect((50,150),(200,200))
                        pygame.event.get()
                        pistol = True
                        coins = coins - 3
                        screen.blit(coinstxt,(50,30))
                        pygame.draw.rect(screen, (0,128,0),pistolR)
                        pygame.display.update()
                        pygame.event.get()
            if spot1[0] > 450 and spot1[0] < 650 and spot1[1] > 150 and spot1[1] < 350:
                if not rifle:
                    if coins >= 10:
                        pygame.event.get()
                        rifle = True
                        coins = coins - 10
                        print(coins)
                        screen.blit(coinstxt,(50,30))
                        pygame.draw.rect(screen, (0,128,0),rifleR)
                        pygame.display.update()
                        pygame.event.get()
            if spot1[0] > 850 and spot1[0] < 1050 and spot1[1] > 150 and spot1[1] < 350:
                if not machinegun:
                    if coins >= 30:
                        pygame.event.get()
                        machinegun = True
                        coins = coins - 30
                        print(coins)
                        screen.blit(coinstxt,(50,30))
                        pygame.draw.rect(screen, (0,128,0),machinegunR)
                        pygame.display.update()
                        pygame.event.get()
            

mouse1 = False
val1 = 0
nop = True
music = True

pygame.mixer.music.play(-1)
while running_title:
    pygame.event.get()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running_title = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse1 = True
            if mouse1 and val1 == 1:
                spot = pygame.mouse.get_pos()
                if spot[0] < 705 and spot[0] > 555 and spot[1] < 600 and spot[1] > 480:
                    pygame.event.get()
                    White = (255,250,250)
                    screen.fill(White)
                    pygame.display.update()
                    pygame.mixer.music.stop()
                    popSound.play()
                    screen.blit(stickmanim,(0,0))
                    pygame.display.update()
                    running_title = False
    if val1 == 0:
        time.sleep(3)
        fade(1280,720)
        pygame.display.update()
        mouse = True
    if mouse and nop:
        screen.blit(menuim,(0,0))
        time.sleep(1)
        mainmenuText()
        val1 = 1
        nop = False
        
go = True
while running:
    if go:
        fightR = pygame.Rect((550,300),(100,60))
        fightborderR = pygame.Rect((540,294),(120,72))
        fightfont = pygame.font.Font("C:\Windows\Fonts\Arial.ttf", 25)
        fightext = fightfont.render('FIGHT!',True,(0,0,0))
        shopR = pygame.Rect((100,20),(130,50))
        shopborderR = pygame.Rect((90,15),(150,60))
        coinsfont = pygame.font.Font("C:\Windows\Fonts\Arial.ttf", 50)
        shoptext = fightfont.render('Shop',True,(0,0,0))
        pygame.draw.rect(screen,(0,0,0),fightborderR)
        pygame.draw.rect(screen,(128,128,128),fightR)
        pygame.draw.rect(screen,(0,0,0),shopborderR)
        pygame.draw.rect(screen,(128,128,128),shopR)
        screen.blit(fightext,(555,303))
        screen.blit(rshopim,(110,30))
        screen.blit(shoptext,(150,28))
        pygame.display.update()
        go = False
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running_title = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            spot = pygame.mouse.get_pos()
            if spot[0] < 660 and spot[0] > 540 and spot[1] < 366 and spot[1] > 294:
                yo = 'yo'
            if spot[0] < 240 and spot[0] > 90 and spot[1] > 15 and spot[1] < 85:
                shop()
                
            

in shop() I want it to recognize when someone clicks an item(pistol,rifle,etc) but whenever i click it nothing happens.

Hopefully this is enough details for you guys to fix my problem.

Thanks!

ps. This code is not finished. So if you are wondering why there is some weird stuff in the code, most likely it's for later on in the project.

  • Dont call `pygame.event.get()` multiple times. Call it once in main loop and store it. `events = pygame.event.get()`. The you can use `events` variables multiple times where ever you need it. Also a side note, dont call `pygame.display.update()` multiple times. Just call it once in the main loop. –  Jan 18 '21 at 15:53
  • Also `time.sleep(3)` will cause the entire game to stop for three seconds. –  Jan 18 '21 at 15:56
  • but that didn't fix the problem –  Jan 18 '21 at 15:56
  • hmm hang on I will write a proper answer. –  Jan 18 '21 at 15:57

1 Answers1

0

I am struggling to understand the logic behind your code, but the problem is caused because you are calling pygame.event.get() multiple times. Its everywhere. Sometimes you are calling it and not even storing it which again is probably not what you want. In your main loop, call it once and store it.

while running:
    events = pygame.event.get()
    for event in events:
         #do stuff with it

Then to get access to events in shop, take events as argument def shop(events) and use this events variable instead of pygame.event.get().

Then in the main loop, when you call shop, pass events as argument.

if spot[0] < 240 and spot[0] > 90 and spot[1] > 15 and spot[1] < 85:
     shop(events)

You are also calling pygame.display.update() multiple times, which might cause flickering problems.

  • either I don't understand or that didn't fix the problem –  Jan 18 '21 at 16:49
  • If you are able to provide more details on how to fix this problem, that would be greatly appreicated –  Jan 18 '21 at 16:54