• 大小: 6KB
    文件类型: .py
    金币: 1
    下载: 0 次
    发布日期: 2021-05-12
  • 语言: Python
  • 标签: python  

资源简介

这是使用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

评论

共有 条评论