已经连续学习两周
本帖最后由 没有退路了 于 2019-9-18 17:14 编辑def chaxun(x=''):
if x in mydict:
print(x,mydict.get(x),sep=" : ")
else:
print('查无此人')
def charu(y='',z=''):
mydict.update({y:z}) # 另一种写法 mydict=z
def shanchu(q=''):
if q in mydict:
del mydict # 另一种写法mydict.pop(q)
print('已删除联系人:%s' % q)
else:
print('查无此人')
print('!---欢迎进入通讯录程序---!','!---1:查询联系人资料 ---!','!---2:插入新的联系人 ---!','!---3:删除已有联系人 ---!','!---4:退出通讯录程序 ---!',' ',sep='\n')
#读入通讯录
mydict=dict()
with open(r'C:\Users\lenovo\Desktop\练习题\通讯录.txt','r') as file:
for line in file:
print(line)
line=line.strip()
key=line.split()
value=line.split()
mydict=value
file.close()
print(mydict)
while 1:
zhiling=int(input('\n请输入相关的指令代码:'))
if zhiling in {1,2,3,4}:
if zhiling==1:
temp1=input('请输入联系人姓名: ')
chaxun(temp1)
continue
if zhiling==2:
temp2=input('请输入联系人姓名: ')
if temp2 in mydict:
print('你输入的姓名在通讯录中已存在-->>',temp2,' : ',mydict.get(temp2))
if input('是否需要修改用户资料(y/n):')=='y':
temp3=input('请输入联系人电话: ')
charu(temp2,temp3)
else:
continue
temp3=input('请输入联系人电话: ')
charu(temp2,temp3)
continue
if zhiling==3:
temp4=input('请输入联系人姓名: ')
shanchu(temp4)
continue
if zhiling==4:
print('!---感谢使用通讯录程序---!')
break
else:
print('指令接收错误')
#写入通讯录
with open(r'C:\Users\lenovo\Desktop\练习题\通讯录.txt','w') as file:
for key,value in mydict.items():
file.write(str(key)+' '+str(value)+'\n')
file.close()
import random
import pygame as g
import sys
from pygame.locals import *
g.init() # 初始化导入的所有pygame模块
screen=g.display.set_mode() #初始化一个准备显示的界面
g.display.set_caption('copyright by 没有退路了') #定义窗口标题
background = g.image.load(r"dahai.png").convert()
fishImg = g.image.load(r"xiaoyu.png").convert_alpha()#左
wuguiImg = g.image.load(r"wugui.png").convert_alpha()#左
fishImg1 = g.image.load(r"xiaoyu1.png").convert_alpha()#右
w_width = wuguiImg.get_width()-5 #得到乌龟图片的宽度,后面留着吃鱼的时候用
w_height = wuguiImg.get_height()-5 #得到乌龟图片的高度
y_width = fishImg.get_width()-5 #得到鱼图片的宽度
y_height = fishImg.get_height()-5 #得到鱼图片的高度
#创建一个时钟
fpsClock=g.time.Clock()
boundary_x=
boundary_y=
class Turtle:
def __init__(self):
self.strength=1000
self.x=random.randint(boundary_x,boundary_x)
self.y=random.randint(boundary_y,boundary_y)
def move(self):
new_x=self.x+random.choice([-20,20])
new_y=self.y+random.choice([-20,20])
if new_x<boundary_x:
self.x=2*boundary_x-new_x
elif new_x>boundary_x:
self.x=2*boundary_x-new_x
else:
self.x=new_x
if new_y<boundary_y:
self.y=2*boundary_y-new_y
elif new_y>boundary_y:
self.y=2*boundary_y-new_y
else:
self.y=new_y
self.strength-=1
return (self.x,self.y)
def eat(self):
self.strength+=20
if self.strength>1000:
self.strength=1000
class Fish:
def __init__(self):
self.x=random.randint(boundary_x,boundary_x)
self.y=random.randint(boundary_y,boundary_y)
self.speed=5
self.Img=fishImg1
def move(self):
new_x=self.x+self.speed
new_y=self.y+random.choice([-1,0,1])
if new_x<boundary_x:
self.x=2*boundary_x-new_x
self.speed=-self.speed
if self.speed>0:
self.Img=fishImg1
else:
self.Img=fishImg
elif new_x>boundary_x:
self.x=2*boundary_x-new_x
self.speed=-self.speed
if self.speed>0:
self.Img=fishImg1
else:
self.Img=fishImg
else:
self.x=new_x
if new_y<boundary_y:
self.y=2*boundary_y-new_y
elif new_y>boundary_y:
self.y=2*boundary_y-new_y
else:
self.y=new_y
return (self.x,self.y)
turtle=Turtle()
fishlist=[]
for i in range(10):
newfish=Fish()
fishlist.append(newfish)
while 1:
for event in g.event.get():
if event.type==g.QUIT:
print('QUIT')
g.quit()
sys.exit()
screen.blit(background, (0, 0)) #绘制背景图片
for each_f in fishlist:
screen.blit(each_f.Img,(each_f.x,each_f.y))
each_f.move()
zuobiao=turtle.move()
screen.blit(wuguiImg,(zuobiao))
if not len(fishlist):
break
if not turtle.strength:
print('die')
break
for each_fish in fishlist[:]:
if (zuobiao<each_fish.x+y_width) and (zuobiao+w_width)>each_fish.x and (zuobiao<each_fish.y+y_height) and (zuobiao+y_height>each_fish.y) :
turtle.eat()
print('eat one fish')
fishlist.remove(each_fish)
g.display.update()
fpsClock.tick(11)
g.quit()
sys.exit()
页:
[1]