用python的pygame,做一个按钮

把这段编码给我- -教咱做个按钮

急...满意的追加
下面这个咱看过,不成功

唔...摁钮?是指开始游戏这类的么?如果是的话:

import pygame
pygame.init() #初始化pygame
screen=pygame.display.set_mode([640,480])  #窗口大小:640*480
screen.fill([255,255,255])#用白色填充窗口
myimage=pygame.image.load(‘某个用来做摁钮的图片’) #把变量myimage赋给导入的图片
screen.blit(myimage,[100,100]) #在100,100的地方画出这个图片(100和100为左部和上部)
pygame.display.flip() 
while True:
    for event in pygame.event.get():#获得事件
        if event.type==pygame.MOUSEBUTTONDOWN and 100<=event.pos[0]<=图片宽 and \
         100<=event/pos[1]<=图片长: #判断鼠标位置以及是否摁了下去。
            #做需要做的事情,如开始游戏。
            pass

够详细得了!

温馨提示:内容为网友见解,仅供参考
第1个回答  2009-01-15
呵呵,有点长。

import pygame
from pygame.locals import *

class SimpleUI:

RED = (255,0,0)
BLUE = (0,0,255)

def __init__(self):
# Initialize PyGame
pygame.init()
pygame.display.set_caption('Paint')
self.screen = pygame.display.set_mode((640,480))
self.screen.fill((255,255,255))
self.button1 = Rect(20, 20, 100, 50)
self.button2 = Rect(20, 90, 100, 50)
self.colorArea = Rect(140, 20, 400, 400)
self.selectedColor = SimpleUI.BLUE

def run(self):
# Run the event loop
self.loop()
# Close the Pygame window
pygame.quit()

def loop(self):
exit = False
while not exit:
exit = self.handleEvents()
self.draw()

def handleEvents(self):
exit = False
for event in pygame.event.get():
if event.type == QUIT:
exit = True
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
exit = True
elif event.type == MOUSEBUTTONDOWN:
self.handleMouseDown(pygame.mouse.get_pos())
return exit

def handleMouseDown(self, (x, y)):
print x,y
if (self.button1.collidepoint(x, y)):
self.selectedColor = SimpleUI.BLUE
print "BLUE"
elif (self.button2.collidepoint(x, y)):
self.selectedColor = SimpleUI.RED
print "RED"

def draw(self):
pygame.draw.rect(
self.screen,
SimpleUI.BLUE,
self.button1,
)
pygame.draw.rect(
self.screen,
SimpleUI.RED,
self.button2,
)
pygame.draw.rect(
self.screen,
self.selectedColor,
self.colorArea,
)
pygame.display.update()

# Start the game
SimpleUI().run()
相似回答