曹冲称象小游戏pygame实现
2024-08-25 07:26:27
#!/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()
最新文章
- python中list作为全局变量无需global声明的原因
- 一个叫vtysh的命令行shell
- primefaces p:tableData 显示 List<;List>;
- stamp-po的作用
- 跟大牛之间关于hibernate的一些探讨记录
- 将UTF8编码的3字节中英文转成2字节中英文.
- Mysql SQL优化&;执行计划
- android studio还不错
- 开发问题记录——ArcEngine问题记录
- JavaScript小笔记の经典算法等....
- python-gdb
- HttpServletResponse HttpServletRequest RequestDispatcher
- 【转】Vim学习资料
- SQLsever存储过程分页查询
- qt+opencv LNK4272:library machine type &#39;x64&#39; conflicts with target mathine type &#39;x86&#39;
- shell之磁盘容量检查,配合crontab可以定时清理磁盘
- Nginx配置中的log_format用法梳理(设置详细的日志格式)
- 深入理解$watch ,$apply 和 $digest --- 理解数据绑定过程——续
- HDU 2767 Proving Equivalences (Tarjan)
- 安装office2016和激活。