1. 程式人生 > >曹衝稱象小遊戲pygame實現

曹衝稱象小遊戲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()