鱼C论坛

 找回密码
 立即注册
查看: 2406|回复: 1

[学习笔记] 已经连续学习两周

[复制链接]
发表于 2019-9-12 16:19:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 没有退路了 于 2019-9-18 17:14 编辑
  1. def chaxun(x=''):
  2.     if x in mydict:
  3.         print(x,mydict.get(x),sep=" : ")
  4.     else:
  5.         print('查无此人')
  6.    
  7. def charu(y='',z=''):
  8.         mydict.update({y:z}) # 另一种写法 mydict[y]=z

  9. def shanchu(q=''):
  10.     if q in mydict:
  11.         del mydict[q] # 另一种写法mydict.pop(q)
  12.         print('已删除联系人:%s' % q)
  13.     else:
  14.         print('查无此人')
  15.         
  16. print('!---欢迎进入通讯录程序---!','!---1:查询联系人资料 ---!','!---2:插入新的联系人 ---!','!---3:删除已有联系人 ---!','!---4:退出通讯录程序 ---!',' ',sep='\n')

  17. #读入通讯录
  18. mydict=dict()
  19. with open(r'C:\Users\lenovo\Desktop\练习题\通讯录.txt','r') as file:
  20.     for line in file:
  21.         print(line)
  22.         line=line.strip()
  23.         key=line.split()[0]
  24.         value=line.split()[1]
  25.         mydict[key]=value
  26.     file.close()
  27.     print(mydict)

  28. while 1:
  29.     zhiling=int(input('\n请输入相关的指令代码:'))
  30.     if zhiling in {1,2,3,4}:
  31.         if zhiling==1:
  32.             temp1=input('请输入联系人姓名: ')
  33.             chaxun(temp1)
  34.             continue
  35.         if zhiling==2:
  36.             temp2=input('请输入联系人姓名: ')
  37.             if temp2 in mydict:
  38.                 print('你输入的姓名在通讯录中已存在-->>',temp2,' : ',mydict.get(temp2))
  39.                 if input('是否需要修改用户资料(y/n):')=='y':
  40.                     temp3=input('请输入联系人电话: ')
  41.                     charu(temp2,temp3)
  42.                 else:
  43.                     continue
  44.             temp3=input('请输入联系人电话: ')
  45.             charu(temp2,temp3)
  46.             continue
  47.         if zhiling==3:
  48.             temp4=input('请输入联系人姓名: ')
  49.             shanchu(temp4)
  50.             continue
  51.         if zhiling==4:
  52.             print('!---感谢使用通讯录程序---!')
  53.             break
  54.     else:
  55.         print('指令接收错误')

  56. #写入通讯录
  57. with open(r'C:\Users\lenovo\Desktop\练习题\通讯录.txt','w') as file:
  58.     for key,value in mydict.items():
  59.         file.write(str(key)+' '+str(value)+'\n')
  60. file.close()



复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-9-18 17:12:55 | 显示全部楼层
  1. import random
  2. import pygame as g
  3. import sys
  4. from pygame.locals import *

  5. g.init()   # 初始化导入的所有pygame模块
  6. screen=g.display.set_mode([300,300]) #初始化一个准备显示的界面
  7. g.display.set_caption('copyright by 没有退路了') #定义窗口标题


  8. background = g.image.load(r"dahai.png").convert()
  9. fishImg = g.image.load(r"xiaoyu.png").convert_alpha()#左
  10. wuguiImg = g.image.load(r"wugui.png").convert_alpha()#左
  11. fishImg1 = g.image.load(r"xiaoyu1.png").convert_alpha()#右


  12. w_width = wuguiImg.get_width()-5 #得到乌龟图片的宽度,后面留着吃鱼的时候用
  13. w_height = wuguiImg.get_height()-5 #得到乌龟图片的高度
  14. y_width = fishImg.get_width()-5 #得到鱼图片的宽度
  15. y_height = fishImg.get_height()-5 #得到鱼图片的高度


  16. #创建一个时钟
  17. fpsClock=g.time.Clock()

  18. boundary_x=[0,300]
  19. boundary_y=[0,300]

  20. class Turtle:
  21.     def __init__(self):
  22.         self.strength=1000
  23.         self.x=random.randint(boundary_x[0],boundary_x[1])
  24.         self.y=random.randint(boundary_y[0],boundary_y[1])

  25.     def move(self):
  26.         new_x=self.x+random.choice([-20,20])
  27.         new_y=self.y+random.choice([-20,20])
  28.             
  29.         if new_x<boundary_x[0]:
  30.             self.x=2*boundary_x[0]-new_x
  31.         elif new_x>boundary_x[1]:
  32.             self.x=2*boundary_x[1]-new_x
  33.         else:
  34.             self.x=new_x
  35.             
  36.         if new_y<boundary_y[0]:
  37.             self.y=2*boundary_y[0]-new_y
  38.         elif new_y>boundary_y[1]:
  39.             self.y=2*boundary_y[1]-new_y
  40.         else:
  41.             self.y=new_y
  42.         self.strength-=1
  43.         return (self.x,self.y)

  44.     def eat(self):
  45.         self.strength+=20
  46.         if self.strength>1000:
  47.             self.strength=1000

  48. class Fish:
  49.     def __init__(self):
  50.         self.x=random.randint(boundary_x[0],boundary_x[1])
  51.         self.y=random.randint(boundary_y[0],boundary_y[1])
  52.         self.speed=5
  53.         self.Img=fishImg1

  54.     def move(self):
  55.         new_x=self.x+self.speed
  56.         new_y=self.y+random.choice([-1,0,1])
  57.         
  58.         if new_x<boundary_x[0]:
  59.             self.x=2*boundary_x[0]-new_x
  60.             self.speed=-self.speed
  61.             if self.speed>0:
  62.                 self.Img=fishImg1
  63.             else:
  64.                 self.Img=fishImg
  65.         elif new_x>boundary_x[1]:
  66.             self.x=2*boundary_x[1]-new_x
  67.             self.speed=-self.speed
  68.             if self.speed>0:
  69.                 self.Img=fishImg1
  70.             else:
  71.                 self.Img=fishImg
  72.         else:
  73.             self.x=new_x
  74.             
  75.         if new_y<boundary_y[0]:
  76.             self.y=2*boundary_y[0]-new_y
  77.         elif new_y>boundary_y[1]:
  78.             self.y=2*boundary_y[1]-new_y
  79.         else:
  80.             self.y=new_y
  81.         
  82.         return (self.x,self.y)

  83. turtle=Turtle()
  84. fishlist=[]
  85. for i in range(10):
  86.     newfish=Fish()
  87.     fishlist.append(newfish)
  88.    
  89. while 1:
  90.     for event in g.event.get():
  91.         if event.type==g.QUIT:
  92.             print('QUIT')
  93.             g.quit()
  94.             sys.exit()

  95.     screen.blit(background, (0, 0)) #绘制背景图片

  96.     for each_f in fishlist:
  97.         screen.blit(each_f.Img,(each_f.x,each_f.y))
  98.         each_f.move()
  99.    
  100.     zuobiao=turtle.move()
  101.     screen.blit(wuguiImg,(zuobiao))

  102.     if not len(fishlist):
  103.         break
  104.     if not turtle.strength:
  105.         print('die')
  106.         break
  107.    
  108.     for each_fish in fishlist[:]:
  109.         if (zuobiao[0]<each_fish.x+y_width) and (zuobiao[0]+w_width)>each_fish.x and (zuobiao[1]<each_fish.y+y_height) and (zuobiao[1]+y_height>each_fish.y) :
  110.             turtle.eat()
  111.             print('eat one fish')
  112.             fishlist.remove(each_fish)

  113.     g.display.update()
  114.     fpsClock.tick(11)
  115.    
  116. g.quit()
  117. sys.exit()      
复制代码
xiaoyu1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-25 04:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表