私は、行のボタンをクリックすると、画面上の画像をブリットしようとしています
screen.blit(buttonPlusImage,
[random.randrange(560, 680),
random.randrange(330, 450)])
しかし、画像は長くないので、私は(:私はディスプレイを更新するために、これらの3行を使用していました画面が更新の後に消えpygame.display.flip()
、FPS.tick(144)
そしてscreen.fill(white)
)。どのように私はそれが映像画面上の滞在していないが、screen.fill(白)と離れて行くような方法で行うことができますか?(私はそれを引き起こしているものだと思います)。
私は、内のコードを貼り付けhttps://dpaste.org/qGdi
import pygame
import random
pygame.init()
# variables
mainLoop = True
font = pygame.font.SysFont('comicsansms', 25)
white = [255, 255, 255]
green = [0, 150, 0]
gray = [140, 140, 140]
black = [0, 0, 0]
clickNum = 0
clickAmount = 1
FPS = pygame.time.Clock()
screen = pygame.display.set_mode((1300, 700))
# functions
def switchButton01():
global button02
button02 = pygame.transform.scale(button02, (100, 100))
screen.blit(button02, [580, 350])
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
global clickNum
clickNum += 1
screen.blit(buttonPlusImage, [random.randrange(560, 680), random.randrange(330, 450)])
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
# load images
button01 = pygame.image.load('button_100.png')
button02 = pygame.image.load('button_100hovered.png')
icon = pygame.image.load('icon_128.png')
buttonPlusImage = pygame.image.load('buttonPlus_32.png')
# window itself and icon
pygame.display.set_caption("incremental adventures")
pygame.display.set_icon(icon)
while mainLoop:
pygame.display.flip()
FPS.tick(144)
screen.fill(white)
# actual content in the game
button01 = pygame.transform.scale(button01, (100, 100))
screen.blit(button01, [580, 350])
click_counter_text = font.render("Click counter: ", True, black, white)
screen.blit(click_counter_text, [525, 50])
button01rect = button01.get_rect(center=(630, 400))
button01collidepoint = button01rect.collidepoint(pygame.mouse.get_pos())
click_counter = font.render((str(clickNum)), True, black, white)
screen.blit(click_counter, [700, 50])
if button01collidepoint:
switchButton01()
# quits
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
私はすべてを変えませんでした。
アプリケーションループで複数のイベントループを避けてください。注、pygame.event.get()
キューからすべての電子のベントを削除します。だから、最初または2番目のイベントループはイベントになるだろう。しかし、両方がループしていません。あなたは、複数回のループに一度のイベントを取得し、イベントのリストを使用することによって、そのことについて取り除くことができます:
while mainLoop:
events = pygame.event.get()
# [...]
if button01collidepoint:
switchButton01(events)
# [...]
for event in events:
# [...]
あなたがしたい場合は、画面上の画像の滞在は、あなたはすべてのフレームでそれを描画する必要があること。ボタンの位置を格納する追加buttonPlusPos
とメインアプリケーションループ内の位置を描きます。この関数はswitchButton01
位置を返しTOIがあります。
buttonPlusPos = None
while mainLoop:
# [...]
if button01collidepoint:
buttonPlusPos = switchButton01(events, buttonPlusPos)
if buttonPlusPos:
screen.blit(buttonPlusImage, buttonPlusPos)
機能はswitchButton01
しないblit
ボタンは、それだけで位置を返します。さらに、イベントのリストは、関数や機能プロセスリスト内のイベントではなく、その「自分」のリストを取得するに渡されます。任意のCASに画像の位置が返されます。(ことを許可され、新しい位置または現在位置のいずれかNone
):
def switchButton01(events, buttonPlusPos):
global button02
button02 = pygame.transform.scale(button02, (100, 100))
screen.blit(button02, [580, 350])
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
global clickNum
clickNum += 1
return (random.randrange(560, 680), random.randrange(330, 450))
return buttonPlusPos
完全なコード:
import pygame
import random
pygame.init()
# variables
mainLoop = True
font = pygame.font.SysFont('comicsansms', 25)
white = [255, 255, 255]
green = [0, 150, 0]
gray = [140, 140, 140]
black = [0, 0, 0]
clickNum = 0
clickAmount = 1
FPS = pygame.time.Clock()
screen = pygame.display.set_mode((1300, 700))
# functions
def switchButton01(events, buttonPlusPos):
global button02
button02 = pygame.transform.scale(button02, (100, 100))
screen.blit(button02, [580, 350])
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
global clickNum
clickNum += 1
return (random.randrange(560, 680), random.randrange(330, 450))
return buttonPlusPos
# load images
button01 = pygame.image.load('button_100.png')
button02 = pygame.image.load('button_100hovered.png')
icon = pygame.image.load('icon_128.png')
buttonPlusImage = pygame.image.load('buttonPlus_32.png')
# window itself and icon
pygame.display.set_caption("incremental adventures")
pygame.display.set_icon(icon)
buttonPlusPos = None
while mainLoop:
pygame.display.flip()
FPS.tick(144)
screen.fill(white)
events = pygame.event.get()
# actual content in the game
button01 = pygame.transform.scale(button01, (100, 100))
screen.blit(button01, [580, 350])
click_counter_text = font.render("Click counter: ", True, black, white)
screen.blit(click_counter_text, [525, 50])
button01rect = button01.get_rect(center=(630, 400))
button01collidepoint = button01rect.collidepoint(pygame.mouse.get_pos())
click_counter = font.render((str(clickNum)), True, black, white)
screen.blit(click_counter, [700, 50])
if button01collidepoint:
buttonPlusPos = switchButton01(events, buttonPlusPos)
if buttonPlusPos:
screen.blit(buttonPlusImage, buttonPlusPos)
# quits
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
quit()