#!/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. Nginx在Linux上的安装和配置

    链接:http://www.cnblogs.com/wbyp/p/7737224.html

  2. vyos 基础配置

    vyos 基础配置 http://www.lowefamily.com.au/2015/11/29/using-a-vyos-router-with-hyper-v/1/http://thomasvo ...

  3. 【刷题】BZOJ 4998 星球联盟

    Description 在遥远的S星系中一共有N个星球,编号为1-N.其中的一些星球决定组成联盟,以方便相互间的交流.但是,组成联盟的首要条件就是交通条件.初始时,在这N个星球间有M条太空隧道.每条太 ...

  4. 一步步创建第一个Docker App —— 3. 创建一个集群Swarm

    原文:https://docs.docker.com/engine/getstarted-voting-app/create-swarm/ 初始化集群 Swarm 1. 使用 ssh 命令登录 man ...

  5. Ansible基础概述

    一.Ansible简介 Ansible基于Python语言实现,由paramiko和PyYAML两个关键模块构建.Ansible的编排引擎可以出色地完成配置管理,流程控制,资源部署等多方面工作.Ans ...

  6. WinForm查询大数据界面假死,使用异步调用解决

    用DataGridView无分页绑定一个几千条数据的查询,查询的时候界面直接卡死十几秒,用户体验非常不好,因此用异步操作解决界面卡死的问题原本场景:点击[查询]后,界面直接卡死优化场景:点击[查询]后 ...

  7. Windows + Ubuntu下JDK与adb/android环境变量配置完整教程

    假设JDK和android sdk路径分别如下: D:\Program Files\Java\jdkD:\android-sdk 1.JDK环境变量配置JAVA_HOME=D:\Program Fil ...

  8. bzoj 4521: [Cqoi2016]手机号码

    感觉get到了一种数位dp的新姿势,加一位表示当前要填的数有没有限制(感觉以前的写法都太蠢了). 这么写有两个地方要注意: 1.每dp到一位时需要f[i][初始状态]++,相当于这位前都是前导零(这道 ...

  9. linux中awk工具的使用

    awk是一个非常好用的数据处理工具.相较于sed常常一整行处理,awk则比较倾向于一行当中分成数个“字段”处理,awk处理方式如下: $ awk '条件类型1{动作1} 条件类型2{动作2} ...' ...

  10. 特征选择实践---python

    作者:城东链接:https://www.zhihu.com/question/28641663/answer/110165221来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...