#!/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()

曹冲称象小游戏pygame实现的更多相关文章

  1. python小游戏-pygame模块

    一.tkinter模块的GUI 基本上使用tkinter来开发GUI应用需要以下5个步骤: 导入tkinter模块中我们需要的东西. 创建一个顶层窗口对象并用它来承载整个GUI应用. 在顶层窗口对象上 ...

  2. Pygame:编写一个小游戏 标签: pythonpygame游戏 2017-06-20 15:06 103人阅读 评论(0)

    大学最后的考试终于结束了,迎来了暑假和大四的漫长的"自由"假期.当然要自己好好"玩玩"了. 我最近在学习Python,本意是在机器学习深度学习上使用Python ...

  3. pygame小游戏之坦克大战

    以前在学校的时候无聊的学了会pygame.看了大概一周的教学视频,做出来个坦克大战的小游戏 Python3.5  pycharm import pygame,sys,time from random ...

  4. 用python+pygame写贪吃蛇小游戏

    因为python语法简单好上手,前两天在想能不能用python写个小游戏出来,就上网搜了一下发现了pygame这个写2D游戏的库.了解了两天再参考了一些资料就开始写贪吃蛇这个小游戏. 毕竟最开始的练手 ...

  5. <pygame> 打飞机(小游戏)

    0.游戏的基本实现 ''' 游戏的基本实现 游戏的初始化:设置游戏窗口,绘制图像的初始位置,设定游戏时钟 游戏循环:设置刷新频率,检测用户交互,更新所有图像位置,更新屏幕显示 ''' 1.安装pyga ...

  6. Python小游戏——外星人入侵(保姆级教程)第一章 01创建Pygame窗口 02创建设置类Setting()

    系列文章目录 第一章:武装飞船 01:创建Pygame窗口以及响应用户输入 02:创建设置类Setting() 一.前期准备 1.语言版本 Python3.9.0 2.编译器 Pycharm2022 ...

  7. 【python游戏编程之旅】第九篇---嗷大喵快跑小游戏开发实例

    本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 前几期博客我们一起学习了,pygame中的冲突检测技术以及一些常用的数据结构. 这次我们来一起做一个简单的酷 ...

  8. Python 小游戏 Bunny

    最近在学习Python,所以上网找了一个小程序练练手. 关于这款名为[Bunny]的小游戏,详细请看下面的链接: http://www.oschina.net/translate/beginning- ...

  9. 【python游戏编程之旅】第五篇---嗷大喵爱吃鱼小游戏开发实例

    本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 我们一同在前几期的博客中已经学到了很多pygame的基本知识了,现在该做个小游戏实战一下了. 前几期博客链接 ...

随机推荐

  1. 80X86计算机组织

    计算机主要由运算器.控制器.存储器.和输入输出设备构成. 主频: 主频是指芯片所用的主时钟频率,它直接影响计算机的运行速度,由于处理器体系结构的差别,同样的主频可能产生不同的计算速度,但主频仍然是反映 ...

  2. BZOJ5105 CodePlus2017晨跑

    这个题???我WA了两发??? #include<iostream> #include<cstdio> #include<cmath> #include<cs ...

  3. SpringMVC框架并发时出现id变成另外一个用户id问题

    今天测试写的代码,出现了在用一个账户登录操作的时候,操作记录的是另外一个id. 经过查找网上的解决方案确认了问题:在controller里面定义了一个userid属性,每次都通过userid传输值.然 ...

  4. VSS2005 上传pdf 空白

    加补丁 VS80-KB943847-X86-INTL.exe

  5. Weblogic CVE-2018-3191远程代码命令执行漏洞复现

      0x00 简介 北京时间10月17日,Oracle官方发布的10月关键补充更新CPU(重要补丁更新)中修复了一个高危的WebLogic远程代码执行漏洞(CVE-2018-3191).该漏洞允许未经 ...

  6. NOIP2015D2总结

    今天居然考了一套题.NOIP2015D2. 这是当年的战绩: 360的一等奖线.好强啊! 之前做过2015的D1,但我确实不会做landlord……今天曾祥瑞学长和林可学姐都来了,他们说,朱昶宇AK, ...

  7. MySQL基本了解与使用

    MySQL的相关概念介绍 MySQL 为关系型数据库(Relational Database Management System), 这种所谓的"关系型"可以理解为"表格 ...

  8. 团体程序设计天梯赛 L1-011. A-B

    读入的是字符串,数组大小至少为字符串长度+1 #include <stdio.h> #include <stdlib.h> #include <string.h> ...

  9. 一、初识java

    理论性的东西就不在笔记中作为纪录了. 先来解释下java安装过程中的一些问题,java安装和环境配置不多做强调,可以参考http://www.cnblogs.com/JianXu/p/5158404. ...

  10. merger_by_one 处理二维数组,根据里面某字段合并, 里面有的保留,有的求和~~

    public function tt(){ $param = array( array ( 'hykno' => '2222222-CB', 'tcdk_fid' => '458B6D70 ...