Py学习  »  Python

检测是否在python zelle图形中的框内单击

Ryan M • 5 年前 • 1255 次点击  

我创造了一个骰子赌博游戏。当我的代码运行时,有一个“点击滚动”按钮。目前,如果你点击屏幕上的任何地方,骰子将滚动。如果你点击盒子外面的任何地方,并且只有在盒子里面点击骰子,我怎样才能使程序关闭?

import graphics
from graphics import *
from random import randrange
max_x = 500
max_y = 300
win = GraphWin("Dice Rolls", max_x,max_y)
#drives the program
def main():
   class dots():
       #Drawn dots
     def __init__(self,p_x=0,p_y=0,p_s=50):
       self.m_x  = p_x
       self.m_y  = p_y
       self.m_dice_size = p_s
       self.m_items = []
       dot_size = 4
       x = self.m_x
       y = self.m_y
       s = self.m_dice_size
       d = s/4
       self.m_items.append(Circle(Point(x+2*d, y+2*d), dot_size))
       self.m_items.append(Circle(Point(x+1*d, y+1*d), dot_size))
       self.m_items.append(Circle(Point(x+1*d, y+2*d), dot_size))
       self.m_items.append(Circle(Point(x+1*d, y+3*d), dot_size))
       self.m_items.append(Circle(Point(x+3*d, y+1*d), dot_size))
       self.m_items.append(Circle(Point(x+3*d, y+2*d), dot_size))
       self.m_items.append(Circle(Point(x+3*d, y+3*d), dot_size))
       for dot in self.m_items:
         dot.setFill('black')
        #Displays three dice images based on the random value
     def display_dice(self):
       return (self.m_x,self.m_y,self.m_s)
     def undraw(self):
       for dot in self.m_items:
         dot.undraw()
               #Drawn Dice
     def draw(self, p_win, p_num):
       for dot in self.m_items:
         dot.undraw()
       if (p_num==1):
         self.m_items[0].draw(p_win)
       elif (p_num==2):
         self.m_items[3].draw(p_win)
         self.m_items[4].draw(p_win)
       elif (p_num==3):
         self.m_items[0].draw(p_win)
         self.m_items[3].draw(p_win)
         self.m_items[4].draw(p_win)
       elif (p_num==4):
         self.m_items[1].draw(p_win)
         self.m_items[3].draw(p_win)
         self.m_items[4].draw(p_win)
         self.m_items[6].draw(p_win)
       elif (p_num==5):
         self.m_items[0].draw(p_win)
         self.m_items[1].draw(p_win)
         self.m_items[3].draw(p_win)
         self.m_items[4].draw(p_win)
         self.m_items[6].draw(p_win)
       elif (p_num==6):
         self.m_items[1].draw(p_win)
         self.m_items[2].draw(p_win)
         self.m_items[3].draw(p_win)
         self.m_items[4].draw(p_win)
         self.m_items[5].draw(p_win)
         self.m_items[6].draw(p_win)

   #Prepares for next roll
   class dice_t:
     def __init__(self,x=0,y=0):
       self.m_x = x
       self.m_y = y
       self.m_s = 50
       self.m_item = Rectangle(Point(self.m_x,self.m_y),Point(self.m_x+self.m_s,self.m_y+self.m_s))
       self.m_item.setFill('white')
       self.m_dots = dots(self.m_x,self.m_y,self.m_s)
     def display_dice(self):
       return (self.m_x,self.m_y,self.m_s)
     def draw_die(self, p_win):
       self.m_item.undraw()
       self.m_item.draw(p_win)
     def draw(self, p_win, p_num):
       self.draw_die(p_win)
       self.m_dots.draw(p_win,p_num)
     def undraw(self):
       self.m_item.undraw()
       self.m_dots.undraw()
   #Winnings and losing calculation
   def check_winner(p_rolls=[]):
      last = None
      total = 0
      triple = True
      for r in p_rolls:
        if (last!=None) and (last!=r):
          triple = False
        last = r
        total += r
      if (total==3) or (total==18):
        return 10
      elif (total==4) or (total==17):
        return 5
      elif (triple ==True):
        return 2
      return -1
   #Text and instructions/rules
   def get_bet(p_win,p_balance,p_def_bet):
      inst = []
      inst.append(Text(Point(max_x/2,20), "MAKE YOUR BET: "))
      inst.append(Text(Point(max_x/2,40), "BALANCE:"+str(p_balance)))
      inst.append(Text(Point(max_x/2,70), "Rules: "))
      inst.append(Text(Point(max_x/2,90), "If you roll a 3 or 18 in total your bet winnings will be 10x your bet."))
      inst.append(Text(Point(max_x/2,110), "If you roll a 4 or 17 in total your bet winnings will be 5x your bet."))
      inst.append(Text(Point(max_x/2,130), "If you roll triples besides a 3 and 18 your bet winnings will be 2x your bet."))
      inst.append(Text(Point(max_x/2,150), 'If you roll anything else, you lose your bet.'))
      inst.append(Rectangle(Point(max_x/2-59,190), Point(max_x/2+59, 210)))
      inst.append(Text(Point(max_x/2, 200), 'CLICK TO ROLL'))
      for item in inst:
        item.draw(p_win)

      bet_text = str(p_def_bet)
      bet_input = Entry(Point (max_x/2+100, 20),5)
      bet_input.setText(bet_text)
      bet_input.draw(p_win)
      p_win.getMouse()
      bet_text = bet_input.getText()
      bet = int(bet_text)
      bet_input.undraw()
      for item in inst:
        item.undraw()
      return bet
   #Shows winnings, checks for winner, updates total, and returns the updated total
   def show_winnings(p_win, p_winnings):
      inst = []
      inst.append(Text(Point(max_x/2,90), "Your WINNINGS:"+str(winnings)))
      inst.append(Rectangle(Point(max_x/2-50,190), Point(max_x/2+50, 210)))
      inst.append(Text(Point(max_x/2, 200), 'PLAY AGAIN'))
      for item in inst:
        item.draw(p_win)
      p_win.getMouse()
      for item in inst:
        item.undraw()
   #Shows bet
   def show_bet_invalid(p_win):
      inst = []
      inst.append(Text(Point(max_x/2,90), "YOUR BET WAS INVALID"))
      inst.append(Rectangle(Point(max_x/2-50,190), Point(max_x/2+50, 210)))
      inst.append(Text(Point(max_x/2, 200), 'TRY AGAIN'))
      for item in inst:
        item.draw(p_win)
      p_win.getMouse()
      for item in inst:
        item.undraw()
   #Shows game over
   def show_game_over(p_win):
      inst = []
      inst.append(Text(Point(max_x/2,90), "YOU ARE OUT OF MONEY"))
      inst.append(Rectangle(Point(max_x/2-50,190), Point(max_x/2+50, 210)))
      inst.append(Text(Point(max_x/2, 200), 'QUIT'))
      for item in inst:
        item.draw(p_win)
      p_win.getMouse()
      for item in inst:
        item.undraw()


   # M A I N and balances along with random outputs
   #################################################

   dice = []
   for d in range(0,3):
     dice.append(dice_t(max_x/2-90+d*60,5))

   balance = 100
   def_bet = 10
   while ( balance > 0 ):
     bet_invalid = True
     while (bet_invalid):
       bet = get_bet(win,balance,def_bet)
       if (bet>=1) and (bet<=balance):
         bet_invalid = False
       else:
         show_bet_invalid(win)
     def_bet = bet
     rolls = []
     for r in range(0,3):
       roll = randrange(1, 7)
       dice[r].draw(win,roll)
       rolls.append(roll)
     winnings = check_winner(rolls) * bet
     balance += winnings
     show_winnings(win, winnings)
     for r in range(0,3):
       dice[r].undraw()
   show_game_over(win)

main()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40516
 
1255 次点击  
文章 [ 2 ]  |  最新文章 5 年前