如何在按下按钮时显示新图像 Pygame
How to make a new image appear when a button is pressed Pygame
我想做一个游戏,如果你按下一个按钮,就会弹出一个图像,但我不知道该怎么做。也许我把它放在一个加号缩进中,或者搞砸了一些东西,我不知道我在这里做错了什么。任何帮助,将不胜感激!无论如何,这是代码:(重要的部分是 number_one
函数)
import pygame
import os
import random
WIDTH, HEIGHT = (1024, 768)
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Joe mama")
number = [0, 1, 2]
# Button class
class Button():
def __init__(self, x, y, image):
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
def draw(self):
#Draw button on screen
WIN.blit(self.image, (self.rect.x, self.rect.y))
def number_one():
# Picture Variables
kerdes1 = pygame.image.load("Zene/1/Kérdés-1.png")
A_select = pygame.image.load("Zene/1/A megjelölve.png")
B_select = pygame.image.load("Zene/1/B megjelölve.png")
C_select = pygame.image.load("Zene/1/C megjelölve.png")
D_select = pygame.image.load("Zene/1/D megjelölve.png")
WIN.blit(kerdes1, (0, 0))
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
WIN.blit(A_select, (0, 0))
print("pressed up")
def number_two():
kerdes2 = pygame.image.load("Zene/2/Kérdés-2.png")
WIN.blit(kerdes2, (0, 0))
while len(number) > 0:
if len(number) == 0:
break
result = random.choice(number)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if result == 1:
print("1")
number.remove(1)
number_one()
print(number)
pygame.display.update()
希望我正确理解了您的问题。现在可以用了,鼠标点击图片,索引就上去了。
根据索引,他们从列表“图像”中绘制了一张图片
这是我的代码示例:
import pygame
from sys import exit
WIDTH, HEIGHT = (1024, 768)
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Joe mama")
clock = pygame.time.Clock()
class Button():
def __init__(self, x, y, image):
self.image = image
self.rect = self.image.get_rect(topleft=(x, y))
def draw(self):
WIN.blit(self.image, self.rect)
button_img = pygame.image.load("button_image.png")
A_select = pygame.image.load("image1.png")
B_select = pygame.image.load("image2.png")
images = [A_select, B_select]
def number_one(index):
WIN.blit(images[index], (WIDTH/2, HEIGHT/2))
button = Button(100, 100, button_img)
index = -1
while True:
WIN.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if button.rect.collidepoint(event.pos):
index += 1
button.draw()
if index >= 0:
number_one(index % len(images))
pygame.display.update()
clock.tick(60)
如果有什么不符合您的想法,请发表评论并编辑作业。
希望对你有所帮助,亚当
我想做一个游戏,如果你按下一个按钮,就会弹出一个图像,但我不知道该怎么做。也许我把它放在一个加号缩进中,或者搞砸了一些东西,我不知道我在这里做错了什么。任何帮助,将不胜感激!无论如何,这是代码:(重要的部分是 number_one
函数)
import pygame
import os
import random
WIDTH, HEIGHT = (1024, 768)
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Joe mama")
number = [0, 1, 2]
# Button class
class Button():
def __init__(self, x, y, image):
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
def draw(self):
#Draw button on screen
WIN.blit(self.image, (self.rect.x, self.rect.y))
def number_one():
# Picture Variables
kerdes1 = pygame.image.load("Zene/1/Kérdés-1.png")
A_select = pygame.image.load("Zene/1/A megjelölve.png")
B_select = pygame.image.load("Zene/1/B megjelölve.png")
C_select = pygame.image.load("Zene/1/C megjelölve.png")
D_select = pygame.image.load("Zene/1/D megjelölve.png")
WIN.blit(kerdes1, (0, 0))
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
WIN.blit(A_select, (0, 0))
print("pressed up")
def number_two():
kerdes2 = pygame.image.load("Zene/2/Kérdés-2.png")
WIN.blit(kerdes2, (0, 0))
while len(number) > 0:
if len(number) == 0:
break
result = random.choice(number)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if result == 1:
print("1")
number.remove(1)
number_one()
print(number)
pygame.display.update()
希望我正确理解了您的问题。现在可以用了,鼠标点击图片,索引就上去了。
根据索引,他们从列表“图像”中绘制了一张图片
这是我的代码示例:
import pygame
from sys import exit
WIDTH, HEIGHT = (1024, 768)
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Joe mama")
clock = pygame.time.Clock()
class Button():
def __init__(self, x, y, image):
self.image = image
self.rect = self.image.get_rect(topleft=(x, y))
def draw(self):
WIN.blit(self.image, self.rect)
button_img = pygame.image.load("button_image.png")
A_select = pygame.image.load("image1.png")
B_select = pygame.image.load("image2.png")
images = [A_select, B_select]
def number_one(index):
WIN.blit(images[index], (WIDTH/2, HEIGHT/2))
button = Button(100, 100, button_img)
index = -1
while True:
WIN.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if button.rect.collidepoint(event.pos):
index += 1
button.draw()
if index >= 0:
number_one(index % len(images))
pygame.display.update()
clock.tick(60)
如果有什么不符合您的想法,请发表评论并编辑作业。
希望对你有所帮助,亚当