我现在正在pygame中为一个班级创建战舰,我正试图超越,因为我喜欢使用python并使用pygame创建它。无论如何,我已经创建了一个Button类,除了一个问题之外它工作得很好,如果我在屏幕上有多个按钮,它们都运行相同的代码:/我需要它们是唯一的并在单击它们时运行不同的代码我似乎无法做到这一点大声笑。
这是按钮类:
import pygame
red = (255, 0, 0)
darkRed = (153, 0, 0)
green = (0, 204, 0)
darkGreen = (0, 102, 0)
black = (0, 0, 0)
white = (255, 255, 255)
gray = (192, 192, 192)
darkGray = (96, 96, 96)
class Button(pygame.sprite.Sprite):
def __init__(self, color, color2, value):
pygame.sprite.Sprite.__init__(self)
self.color = color
self.color2 = color2
self.value = value
self.image = pygame.Surface([80, 50])
self.image.fill(self.color)
self.rect = self.image.get_rect()
def update(self):
pos = pygame.mouse.get_pos()
xPos = self.rect[0]
yPos = self.rect[1]
width = self.rect[2]
height = self.rect[3]
if xPos + width > pos[0] > xPos and yPos + height > pos[1] > yPos:
self.image.fill(self.color2)
else:
self.image.fill(self.color)
def click(self):
click = pygame.mouse.get_pressed()
if click[0] == True:
return self.value
else:
return False
以下是运行所有内容的主要代码:
from Button import *
from TextDisplay import *
from wallPositions import *
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
def screen0():
pygame.init()
button_list = pygame.sprite.Group()
button = Button(green, darkGreen, True)
button_list.add(button)
text = Text("Battleship", white, 115, (screen_width * .5), (screen_height * .5 - 50))
done = False
clock = pygame.time.Clock()
button.rect.x = (screen_width * .5 - 35)
button.rect.y = (screen_height * .5 + 70)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if button.click() == True:
screen1()
button_list.update()
screen.fill(black)
text.draw(screen)
button_list.draw(screen)
pygame.display.flip()
clock.tick(30)
pygame.quit()
def screen1():
pygame.init()
button_list = pygame.sprite.Group()
button = Button(gray, darkGray, 1)
button_list.add(button)
button.rect.x = x_list[4]
button.rect.y = y_list[2]
button2 = Button(gray, darkGray, 2)
button_list.add(button2)
button2.rect.x = x_list[6]
button2.rect.y = y_list[2]
button3 = Button(gray, darkGray, 3)
button_list.add(button3)
button3.rect.x = x_list[8]
button3.rect.y = y_list[2]
button4 = Button(gray, darkGray, 4)
button_list.add(button4)
button4.rect.x = x_list[10]
button4.rect.y = y_list[2]
button5 = Button(gray, darkGray, 5)
button_list.add(button5)
button5.rect.x = x_list[4]
button5.rect.y = y_list[4]
button6 = Button(gray, darkGray, 6)
button_list.add(button6)
button6.rect.x = x_list[6]
button6.rect.y = y_list[4]
button7 = Button(gray, darkGray, 7)
button_list.add(button7)
button7.rect.x = x_list[8]
button7.rect.y = y_list[4]
button8 = Button(gray, darkGray, 8)
button_list.add(button8)
button8.rect.x = x_list[10]
button8.rect.y = y_list[4]
button9 = Button(gray, darkGray, 1)
button_list.add(button9)
button9.rect.x = x_list[4]
button9.rect.y = y_list[8]
button10 = Button(gray, darkGray, 2)
button_list.add(button10)
button10.rect.x = x_list[6]
button10.rect.y = y_list[8]
button11 = Button(gray, darkGray, 3)
button_list.add(button11)
button11.rect.x = x_list[8]
button11.rect.y = y_list[8]
button12 = Button(gray, darkGray, 4)
button_list.add(button12)
button12.rect.x = x_list[4]
button12.rect.y = y_list[10]
button13 = Button(gray, darkGray, 5)
button_list.add(button13)
button13.rect.x = x_list[6]
button13.rect.y = y_list[10]
button14 = Button(gray, darkGray, 6)
button_list.add(button14)
button14.rect.x = x_list[8]
button14.rect.y = y_list[10]
button15 = Button(red, darkRed, True)
button_list.add(button15)
button15.rect.x = x_list[12]
button15.rect.y = y_list[10]
instructions = Text("Select your row and column then hit 'Fire!'", white, 30, (screen_width * .5), 20)
text = Text('1', black, 50, (button.rect.x + 40), (button.rect.y + 25))
text1 = Text('2', black, 50, (button2.rect.x + 40), (button2.rect.y + 25))
text2 = Text('3', black, 50, (button3.rect.x + 40), (button3.rect.y + 25))
text3 = Text('4', black, 50, (button4.rect.x + 40), (button4.rect.y + 25))
text4 = Text('5', black, 50, (button5.rect.x + 40), (button5.rect.y + 25))
text5 = Text('6', black, 50, (button6.rect.x + 40), (button6.rect.y + 25))
text6 = Text('7', black, 50, (button7.rect.x + 40), (button7.rect.y + 25))
text7 = Text('8', black, 50, (button8.rect.x + 40), (button8.rect.y + 25))
text8 = Text('1', black, 50, (button9.rect.x + 40), (button9.rect.y + 25))
text9 = Text('2', black, 50, (button10.rect.x + 40), (button10.rect.y + 25))
text10 = Text('3', black, 50, (button11.rect.x + 40), (button11.rect.y + 25))
text11 = Text('4', black, 50, (button12.rect.x + 40), (button12.rect.y + 25))
text12 = Text('5', black, 50, (button13.rect.x + 40), (button13.rect.y + 25))
text13 = Text('6', black, 50, (button14.rect.x + 40), (button14.rect.y + 25))
text14 = Text('Fire!', black, 40, (button15.rect.x + 40), (button15.rect.y + 25))
rows = Text('Rows', white, 45, (x_list[1] + 10), (y_list[2] + 25))
columns = Text('Columns', white, 45, (x_list[1] + 40), (y_list[8] + 25))
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# I am using this to see if the buttons are working
# But every button is running this code not just the first one
if button.click() == 1:
print("Yay!")
button_list.update()
screen.fill(black)
button_list.draw(screen)
instructions.draw(screen)
text.draw(screen)
text1.draw(screen)
text2.draw(screen)
text3.draw(screen)
text4.draw(screen)
text5.draw(screen)
text6.draw(screen)
text7.draw(screen)
text8.draw(screen)
text9.draw(screen)
text10.draw(screen)
text11.draw(screen)
text12.draw(screen)
text13.draw(screen)
text14.draw(screen)
rows.draw(screen)
columns.draw(screen)
pygame.display.flip()
clock.tick(30)
pygame.quit()
screen0()
我一直被困在这一天,我问我的老师,他说,因为我做了一些不同的事情,我必须自己弄清楚,我甚至问学校的技术人员他们不知道:/我一直在查看堆栈大约一个小时,似乎没有人和我有同样的问题...请帮助,如果我的代码似乎有任何缩进问题,那是因为我只是复制并粘贴它
答案 0 :(得分:0)
在主程序中,编写2个按钮功能(func1(),func2())。然后在按钮类中添加一个变量' self.action'和方法&#def; defaction(功能)。创建按钮时,使用正确的函数调用方法:button.setaction(func1)。然后在时钟代码上,你可以这样自我反应'调用适当的函数。