问题描述
这是我的代码:
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))
screen.blit(titleim,0))
def fade(w,h):
val1 = 1
fade = pygame.Surface((w,h))
fade.fill((0,0))
for alpha in range(0,300,5):
fade.set_alpha(alpha)
redrawTitle()
screen.blit(fade,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),playRect)
screen.blit(menuim,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',0))
coinstxt = shopfont.render('Coins:' + str(coins) + '!',0))
namefont = pygame.font.Font("C:\Windows\Fonts\Arial.ttf",35)
pistoltxt = namefont.render('Pistol',0))
rifletxt = namefont.render('Rifle',0))
machineguntxt = namefont.render('Machine Gun',0))
mainshopR = pygame.Rect((0,(1280,720))
pistolR = pygame.Rect((50,150),(200,200))
rifleR = pygame.Rect((450,200))
machinegunR = pygame.Rect((850,200))
pygame.draw.rect(screen,white,mainshopR)
pygame.draw.rect(screen,(128,128,128),pistolR)
pygame.draw.rect(screen,rifleR)
pygame.draw.rect(screen,machinegunR)
screen.blit(shoptxt,(500,30))
screen.blit(pistoltxt,(100,220))
screen.blit(rifletxt,220))
screen.blit(machineguntxt,(850,220))
screen.blit(coinstxt,(50,30))
pygame.display.update()
if pistol:
pygame.draw.rect(screen,pistolR)
if rifle:
pygame.draw.rect(screen,rifleR)
if machinegun:
pygame.draw.rect(screen,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,200))
pygame.event.get()
pistol = True
coins = coins - 3
screen.blit(coinstxt,30))
pygame.draw.rect(screen,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,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,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))
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))
time.sleep(1)
mainmenuText()
val1 = 1
nop = False
go = True
while running:
if go:
fightR = pygame.Rect((550,300),60))
fightborderR = pygame.Rect((540,294),(120,72))
fightfont = pygame.font.Font("C:\Windows\Fonts\Arial.ttf",25)
fightext = fightfont.render('figHT!',0))
shopR = pygame.Rect((100,20),(130,50))
shopborderR = pygame.Rect((90,15),60))
coinsfont = pygame.font.Font("C:\Windows\Fonts\Arial.ttf",50)
shoptext = fightfont.render('Shop',0))
pygame.draw.rect(screen,fightborderR)
pygame.draw.rect(screen,fightR)
pygame.draw.rect(screen,shopborderR)
pygame.draw.rect(screen,shopR)
screen.blit(fightext,(555,303))
screen.blit(rshopim,(110,30))
screen.blit(shoptext,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()
在 shop()
中,我希望它能够识别何时有人点击了某个物品(手枪、步枪等),但每当我点击它时,什么都没有发生。
希望这些细节足以让你们解决我的问题。
谢谢!
ps。此代码未完成。因此,如果您想知道为什么代码中有一些奇怪的东西,很可能是为了稍后在项目中使用。
解决方法
我正在努力理解您的代码背后的逻辑,但问题是由于您多次调用 pygame.event.get()
而引起的。它无处不在。有时您正在调用它,甚至不存储它,这又可能不是您想要的。在您的主循环中,调用一次并存储它。
while running:
events = pygame.event.get()
for event in events:
#do stuff with it
然后要访问 shop
中的事件,将事件作为参数 def shop(events)
并使用此 events
变量而不是 pygame.event.get()
。
然后在主循环中,当您调用 shop
时,将事件作为参数传递。
if spot[0] < 240 and spot[0] > 90 and spot[1] > 15 and spot[1] < 85:
shop(events)
您还多次调用 pygame.display.update()
,这可能会导致闪烁问题。