资源简介
这是使用python语言编程的小游戏,汉诺塔hanoi,欢迎大家下载
代码片段和文件信息
import copy
import threading
import pygame
from pygame.locals import *
import time
import random
import sys
def rand_color():
return [random.randrange(0230) for i in range(03)]
global MESSAGE_TO_GUI
MESSAGE_TO_GUI=[]
class Hanoi(threading.Thread):
def __init__(selfnMESSAGE_TO_GUI):
self.MESSAGE_TO_GUI=MESSAGE_TO_GUI
threading.Thread.__init__(self)
self.hanoi=[[][][]]
self.hanoi[0]=range(n0-1)
def grubu_tasi(selfkaynakhedef):
for tas in copy.copy(self.hanoi[kaynak]):
self.tasi_tasi(tas hedef)
def tasi_tasi(selftashedef):
for sopa in self.hanoi:
if sopa.count(tas):
kaynak=self.hanoi.index(sopa)
if tas==1:
self.hanoi[kaynak].pop()
self.hanoi[hedef].append(1)
else:
hedefler=[012]
hedefler.remove(kaynak)
hedefler.remove(hedef)
yeni_hedef=hedefler[0]
for yeni_tas in copy.copy(self.hanoi[kaynak][self.hanoi[kaynak].index(tas)+1:]):
self.tasi_tasi(yeni_tasyeni_hedef)
self.hanoi[kaynak].pop()
self.hanoi[hedef].append(tas)
time.sleep(1)
while self.MESSAGE_TO_GUI:
pass
self.MESSAGE_TO_GUI.append({“from“:kaynak“to“:hedef})
def run(self):
time.sleep(1)
self.grubu_tasi(01)
class Sprite(object):
def __init__(self):
self.image=None
self.rect=None
self.xleftover=0
self.yleftover=0
self.placexleftover=0
self.placeyleftover=0
# self.added=False
def update(self*args):
pass
def move(selfxy):
x+=self.xleftover
y+=self.yleftover
self.xleftover=x-int(x)
self.yleftover=y-int(y)
self.rect=self.rect.move(xy)
def move_to(self **cor):
cor[cor.keys()[0]]+=self.placexleftover
cor[cor.keys()[1]]+=self.placeyleftover
x=cor[cor.keys()[0]]
y=cor[cor.keys()[1]]
self.placexleftover=x-int(x)
self.placeyleftover=y-int(y)
self.rect=self.image.get_rect(**cor)
def draw(selfsurfacechange):
surface.blit(self.imageself.rect.move(change))
class Text(Sprite):
def __init__(selffonttextcor):
self.image=font.render(text1(000))
self.rect=self.image.get_rect(**cor)
DISK_H=20
class Disk(Sprite):
def __init__(selfpegn):
self.image=pygame.Surface((50+(n*10)DISK_H))
pygame.draw.rect(self.image rand_color() (0 0 50+(n*10)DISK_H))
self.rect=self.image.get_rect()
self.place(peg)
def place(selfpeg):
self.peg=peg
self.rect.centerx=self.peg.rect.centerx
try:
self.rect.centery=self.peg.disks[-1].rect.centery-DISK_H
except:
self.rect.centery=self.peg.rect.bottom-DISK_H
class Peg(Sprite):
def __init__(selfncormax):
self.image=pygame.Surface((10(max*DISK_H)+50))
pygame.draw.rect(self.image (000) (0 010(max*DISK_H)+50 ))
self.rect=self.image.get_rect(**cor)
self.disks=[]
for disk in range(n+11-1):
self.disks.app
相关资源
- Python 简单的登录界面源码
- 小甲鱼零基础入门学习Python+全套源码
- pygraphviz python3.4 轮子
- 用Python编写潮流计算极坐标
- Python-图像操作
- python建模
- windows将Python添加到注册表
- 位图转g代码轮廓加工源码
- 基于神经网络控制一阶倒立摆小车
- 基于Python爬虫的股票信息爬取保存到
- 基于NMF和PCA实现人脸图像特征提取与
- 基于PCA实现鸢尾花数据集降维
- 基于KNN实现“手写识别”
- 基于sklearn模块的神经网络实现“手写
- 2018最新BAT-python面试题内附答案
- BM3D去噪python代码
- insert_test_data.py
- 栅格投影转换.py
- IDW插值批处理
- 神经网络模型python模板
- python3程序设计习题答案第3版
- python核心基础.txt
- 尚硅谷2018年Python视频
- 基于用户最近邻模型的协同过滤算法
- python自写的车牌识别小程序,完全自
- python面试笔记培训就业必回
- Django+Python搭建的购物网站
- python利用scipy的optimize实现非线性最小
- Python简单网页爬虫
- 自动发布mxd服务
评论
共有 条评论