通过pygame实现小游戏

 

记录一下uml课的课程设计,要求实现曹冲称象主题的小游戏:拖过拖拽砝码,实现对不同重量的大象进行称重

因为是游戏,所以使用了python的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()

 

posted @ 2018-11-12 15:00  一瓶怡宝  阅读(622)  评论(0编辑  收藏  举报