曹冲称象小游戏pygame实现
#!/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实现的更多相关文章
- python小游戏-pygame模块
一.tkinter模块的GUI 基本上使用tkinter来开发GUI应用需要以下5个步骤: 导入tkinter模块中我们需要的东西. 创建一个顶层窗口对象并用它来承载整个GUI应用. 在顶层窗口对象上 ...
- Pygame:编写一个小游戏 标签: pythonpygame游戏 2017-06-20 15:06 103人阅读 评论(0)
大学最后的考试终于结束了,迎来了暑假和大四的漫长的"自由"假期.当然要自己好好"玩玩"了. 我最近在学习Python,本意是在机器学习深度学习上使用Python ...
- pygame小游戏之坦克大战
以前在学校的时候无聊的学了会pygame.看了大概一周的教学视频,做出来个坦克大战的小游戏 Python3.5 pycharm import pygame,sys,time from random ...
- 用python+pygame写贪吃蛇小游戏
因为python语法简单好上手,前两天在想能不能用python写个小游戏出来,就上网搜了一下发现了pygame这个写2D游戏的库.了解了两天再参考了一些资料就开始写贪吃蛇这个小游戏. 毕竟最开始的练手 ...
- <pygame> 打飞机(小游戏)
0.游戏的基本实现 ''' 游戏的基本实现 游戏的初始化:设置游戏窗口,绘制图像的初始位置,设定游戏时钟 游戏循环:设置刷新频率,检测用户交互,更新所有图像位置,更新屏幕显示 ''' 1.安装pyga ...
- Python小游戏——外星人入侵(保姆级教程)第一章 01创建Pygame窗口 02创建设置类Setting()
系列文章目录 第一章:武装飞船 01:创建Pygame窗口以及响应用户输入 02:创建设置类Setting() 一.前期准备 1.语言版本 Python3.9.0 2.编译器 Pycharm2022 ...
- 【python游戏编程之旅】第九篇---嗷大喵快跑小游戏开发实例
本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 前几期博客我们一起学习了,pygame中的冲突检测技术以及一些常用的数据结构. 这次我们来一起做一个简单的酷 ...
- Python 小游戏 Bunny
最近在学习Python,所以上网找了一个小程序练练手. 关于这款名为[Bunny]的小游戏,详细请看下面的链接: http://www.oschina.net/translate/beginning- ...
- 【python游戏编程之旅】第五篇---嗷大喵爱吃鱼小游戏开发实例
本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 我们一同在前几期的博客中已经学到了很多pygame的基本知识了,现在该做个小游戏实战一下了. 前几期博客链接 ...
随机推荐
- BZOJ2878 NOI2012迷失游乐园(树形dp+环套树+概率期望)
考虑树的部分分怎么做.令f[i]为i向子树内走的期望路径长度,转移比较显然.算答案时先把其父亲的答案弄好就可以统计自己的答案了. 环套树也类似.树里直接dp,对环上点暴力考虑环上的每条路径,算完后再在 ...
- Alpha 完结撒花 —— 事后诸葛亮
写在前面 林燊大哥 一路走来,好不容易,终于完结了. 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 解决的问题 用户在进店之前无法得知店铺的优劣,通过 ...
- mysqlbinlog- 处理二进制日志文件的实用工具 学习笔记
参考 MySQL 5.5官方简体中文参考手册完美版 8.6 节 调用: shell> mysqlbinlog [option] log-files... mysqlbinlog支持下面选项: ...
- java插件之Lombok
使用注释来减少Java中的重复代码 @NonNull - 或者:我怎么学会停止担心和喜欢上了NullPointerException. @Cleanup - 自动资源管理:安全地调用您的close() ...
- OS X 安装pyspider
pyspider安装的过程中,需要安装pycurl.有几个坑 一.首先遇到权限的问题 因为/Library目录是root权限,所以非root用户对该目录的读写经常会遇到权限问题,但是不宜切换成root ...
- Access to a protected network share using Win32 C++
WNetAddConnection2 DWORD WNetAddConnection2A( LPNETRESOURCEA lpNetResource, LPCSTR lpPassword, LPCST ...
- nginx访问日志出现大量的500状态信息,用postman返回 Internal Server Error,Too Many Attempts.错误的解决办法
用postman的post方法访问某个URL时,出现以下错误: { "status": "1", "message": " Int ...
- P2627 修剪草坪
P2627 修剪草坪 题目描述 在一年前赢得了小镇的最佳草坪比赛后,Farm John变得很懒,再也没有修剪过草坪.现在,新一轮的最佳草坪比赛又开始了,Farm John希望能够再次夺冠. 然而,Fa ...
- 《Spring实战》 1-2
第1章 Spring之旅 Spring容器 Spring中bean的生命周期 Spring框架中的模块 Spring Portfolio 第2章 装配Bean 自动化装配bean 通过Java代码装配 ...
- 关于ARGB_8888、ALPHA_8、ARGB_4444、RGB_565的理解
关于ARGB_8888.ALPHA_8.ARGB_4444.RGB_565的理解 A:透明度 R:红色 G:绿 B:蓝 Bitmap.Config ARGB_4444:每个像素占四位,即A=4,R=4 ...