#!/usr/bin/env python
# -*- coding: UTF-8 -*- import pygame
from pygame.locals import *
from sys import exit
import sys
from random import *
from math import pi
import random pygame.init() caoChongimageName = "cheng.jpg"
# elephantimageName = "if_lemon_2003191.png"
# basketimageName = "if_lemon_2003191.png"
# chengTuoimageName = "if_lemon_2003191.png"
# chengimageName = "if_lemon_2003191.png"
# tieQiaoimageName = "if_lemon_2003191.png"
# chiimageName = "2.jpg"
# shuiChiimageName = "3.jpg"
background_image_filename = 'background.jpg' def display_init():
global screen
screen = pygame.display.set_mode((863, 603), 0, 32)
pygame.display.set_caption("曹冲称象")
global background
background = pygame.image.load(background_image_filename).convert() class Button(object):
def __init__(self, name, col,position, size):
self.name = name
self.col = col
self.size = size
self.position = position
def isOver(self):
point_x,point_y = pygame.mouse.get_pos()
x, y = self. position
w, h = self.size
in_x = x - w < point_x < x
in_y = y - h < point_y < y
return in_x and in_y
def render(self, fontSize = 20):
w, h = self.size
x, y = self.position
pygame.draw.rect(screen, self.col, ((x - w, y - h), (w, h)), 0)
my_font = pygame.font.Font('chinese.ttf',fontSize)
font_test = my_font.render(self.name, True, (255, 255, 255))
fsetting = font_test.get_rect()
fsetting.center = (x - w / 2, y - h / 2)
screen.blit(font_test, fsetting)
def setText(self, text):
self.name = text caoChong = Button("曹冲", (187, 173, 160), (200, 87), (85, 25))
elephant = Button("象", (187, 173, 160), (400, 87), (85, 25))
basket = Button("水", (187, 173, 160), (600, 87), (85, 25))
chengTuo = Button("秤砣", (187, 173, 160), (800, 87), (85, 25))
tieQiao = Button("铁锹", (187, 173, 160), (1000, 87), (85, 25))
cheng = Button("秤", (187, 173, 160), (1200,700), (85, 25))
text = Button("游戏开始", (187, 173, 160), (780, 580), (200, 400))
chengTuonumber = 1 resultText = ""
weightText = "" def map_init():
screen.fill((250, 248, 239))
screen.blit(background, (14,25)) caoSurface = pygame.Surface((107, 104))
caoImage = pygame.image.load(caoChongimageName).convert_alpha()
caoSurface.blit(caoImage, (0, 0))
screen.blits(blit_sequence=((caoSurface, (100, 150)), (caoSurface, (300, 150)), (caoSurface, (600, 150)))) caoChong.render()
elephant.render()
basket.render()
cheng.render()
chengTuo.render()
tieQiao.render()
text.render() def cal(chengTuo):
global weight, testWeight, chengTuonumber, weightText
while weight>=chengTuoweight[chengTuo]:
weightText = weightText+str(chengTuoweight[chengTuo])+'\n'
chengTuonumber = chengTuonumber + 1
weight = weight-chengTuoweight[chengTuo]
testWeight = testWeight+chengTuoweight[chengTuo] def chengXiang():
global weight, resultText
weight= random.randint(100, 10000)
global chengTuoweight
chengTuoweight= [1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
global testWeight
testWeight= 0
i = 0
while True:
cal(i)
if weight==0:
break
i = i+1
print("该象的体重是%d" %testWeight)
# print(weightText) def waShuichi():
chiSurface = pygame.Surface((256, 256))
chiImage = pygame.image.load(chiimageName).convert_alpha()
chiSurface.blit(chiImage, (0, 0))
screen.blit(chiSurface, (1200, 400)) display_init() while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if (caoChong.isOver() == True):
text.setText("需要挖一个水池")
elif (basket.isOver() == True):
text.setText("水池已经注满水,可以开始称象了")
elif (tieQiao.isOver() == True):
text.setText("挖好了一个水池,现在倒水")
elif (elephant.isOver() == True):
text.setText("选中了一头体重未知的象")
elif (chengTuo.isOver() == True):
text.setText("选择秤砣放上秤")
elif (cheng.isOver() == True):
text.setText("开始称象")
weightText = ""
chengXiang()
text.setText(weightText+("该象的体重是%d" %testWeight)) map_init()
map_init()
pygame.display.update()

最新文章

  1. python中list作为全局变量无需global声明的原因
  2. 一个叫vtysh的命令行shell
  3. primefaces p:tableData 显示 List&lt;List&gt;
  4. stamp-po的作用
  5. 跟大牛之间关于hibernate的一些探讨记录
  6. 将UTF8编码的3字节中英文转成2字节中英文.
  7. Mysql SQL优化&amp;执行计划
  8. android studio还不错
  9. 开发问题记录——ArcEngine问题记录
  10. JavaScript小笔记の经典算法等....
  11. python-gdb
  12. HttpServletResponse HttpServletRequest RequestDispatcher
  13. 【转】Vim学习资料
  14. SQLsever存储过程分页查询
  15. qt+opencv LNK4272:library machine type &#39;x64&#39; conflicts with target mathine type &#39;x86&#39;
  16. shell之磁盘容量检查,配合crontab可以定时清理磁盘
  17. Nginx配置中的log_format用法梳理(设置详细的日志格式)
  18. 深入理解$watch ,$apply 和 $digest --- 理解数据绑定过程——续
  19. HDU 2767 Proving Equivalences (Tarjan)
  20. 安装office2016和激活。

热门文章

  1. c++11 委托构造
  2. Hihocoder之conv2d()
  3. BZOJ 2938: [Poi2000]病毒
  4. Mac上安装mariadb
  5. Java之高级IO,Properties
  6. Android学习笔记——关于onConfigurationChanged(转)
  7. 【费用流】【网络流24题】【P4013】 数字梯形问题
  8. opencv imread值为空
  9. GO_00:Mac之Item2的配置安装
  10. sys模块python