球球大作战
import pygame
import color
from random import randint
"""
游戏功能:点击鼠标生成一个球,球不能出边界,大球吃小球
第一步:搭建游戏窗口
第二不:点击屏幕产生球
第三步:让球动起来(需要用列表来保存球,字典保存每个球的信息)
第四部:
"""
# 全局变量
Window_WIDTH = 800 # 屏幕宽度
Window_HEIGHT = 800 # 屏幕高度
key_ball_color = 'ball_color'
key_ball_center = 'ball_center'
key_ball_radius = 'ball_radius'
key_ball_xspeed = 'ball_xspeed'
key_ball_yspeed = 'ball_yspeed'
key_ball_alive = 'ball_alive'
all_ball = [] # 保存所有的球
def ball_move():
"""
球动起来
:return:
"""
for ball in all_ball:
ball_x,ball_y = ball[key_ball_center]
new_x = ball_x + ball[key_ball_xspeed]
new_y = ball_y + ball[key_ball_yspeed]
if new_x <= ball[key_ball_radius]:
ball[key_ball_xspeed] *= -1
new_x = ball[key_ball_radius]
if new_x >= Window_WIDTH - ball[key_ball_radius]:
ball[key_ball_xspeed] *= -1
new_x = Window_WIDTH - ball[key_ball_radius]
if new_y <= ball[key_ball_radius]:
ball[key_ball_yspeed] *= -1
new_y = ball[key_ball_radius]
if new_y >= Window_HEIGHT - ball[key_ball_radius]:
ball[key_ball_yspeed] *= -1
new_y = Window_HEIGHT - ball[key_ball_radius]
ball[key_ball_center] = new_x,new_y
def draw_all_ball(window):
for ball in all_ball[:]:
if ball[key_ball_alive]:
pygame.draw.circle(window,ball[key_ball_color],ball[key_ball_center],ball[key_ball_radius])
else:
all_ball.remove(ball)
pygame.display.update()
def create_ball(window,pos):
"""
# 在指定的位置产生一个随机的球
:param window:游戏界面
:param pos:球的产生位置
:return:
"""
ball_color = color.rand_color()
ball_center = pos
ball_radius = randint(10,30)
# 创建每个球对应的字典
ball = {
key_ball_color:ball_color,
key_ball_center:ball_center,
key_ball_radius:ball_radius,
key_ball_xspeed:randint(-5,5),
key_ball_yspeed:randint(-5,5),
key_ball_alive:True
}
# 保存球的信息
all_ball.append(ball)
pygame.draw.circle(window,ball_color,ball_center,ball_radius)
pygame.display.update()
def ball_crash():
"""
检查碰撞,看屏幕上的每个球好其他球的圆心距小于等于半径
:return:
"""
# 拿第一个球
for ball in all_ball:
# 拿另一个球
for other in all_ball:
if ball == other or not ball[key_ball_alive] or not other[key_ball_alive]:
continue
x1,y1 = ball[key_ball_center]
x2,y2 = other[key_ball_center]
distance = ((x1-x2)**2 + (y1-y2)**2)**0.5
if distance <= ball[key_ball_radius] + other[key_ball_radius]:
if randint(0,1):
ball[key_ball_alive] = False
ball[key_ball_radius] += other[key_ball_radius]//4
else:
other[key_ball_alive] = False
other[key_ball_radius] += ball[key_ball_radius] // 4
def main_game():
pygame.init()
window = pygame.display.set_mode((Window_WIDTH,Window_HEIGHT))
window.fill(color.white)
pygame.display.flip()
# 游戏循环
while True:
pygame.time.delay(20)
window.fill(color.white)
ball_move()
draw_all_ball(window)
ball_crash()
for event in pygame.event.get():
# 用type判断事件的类型
if event.type == pygame.QUIT:
exit() # 退出程序
# 鼠标点击事件
if event.type == pygame.MOUSEBUTTONDOWN:
create_ball(window,event.pos)
if __name__ == '__main__':
main_game()
作业系统
字典.get(键值,默认值):
当字典没有这个值时,会返回默认值,get()不支持关键字输入,不要用关键字
```python
import file_manager
import student_system
#全局变量
all_user_file = 'user_info'
key_user_name = 'username'
key_user_password = 'password'
def get_all_user():
all_user = file_manager.read_json_file(all_user_file)
if all_user:
return all_user
return []
def is_register(user_name):
all_user = file_manager.read_json_file(all_user_file)
if not all_user:
return False
for user in all_user:
if user[key_user_name] == user_name:
return True
return False
# ==================注册==========================
"""
为了下次打开系统是能够正常登录,注册成功的信息需要保存。保存用户名和密码
[
{'user_name':'用户名','password':'密码'}
]
保存到all_user_info.json
"""
def register():
while True:
user_name = input('请输入一个用户名(3-10位):')
if not 3 <= len(user_name) <= 10:
print('输入有误,请重新输入!')
continue
if is_register(user_name):
print('{}已经注册过了,请重新输入!'.format(user_name))
continue
print('用户名可用!')
break
while True:
password = input('请输入密码!(6-16位)')
if not 6 <= len(password) <= 16:
print('输入有误,请重新输入!')
continue
re_password = input('确认密码:')
if password != re_password:
print('和第一次输入的密码不一样,请重新输入!')
continue
break
all_user = get_all_user()
all_user.append({key_user_name:user_name,key_user_password:password})
re = file_manager.write_json_file(all_user_file,all_user)
if re:
print('注册成功!')
else:
print('注册失败!')
# ==============登录=============================
def login():
user_name = input('请输入用户名:')
password = input('请输入密码')
all_user = get_all_user()
for user in all_user:
if user[key_user_name] == user_name:
if user[key_user_password] == password:
print('登录成功!')
return user_name
else:
print('密码错误')
return None
print('登录失败!')
return None
# ==================主页==========================
def show_main_page():
while True:
print(file_manager.read_text_file('login'))
value = input('请选择:')
if value == '1':
register()
elif value == '2':
user_name = login()
if user_name:
student_system.user_name = user_name
student_system.main_page()
print('进入到学生管理系统。')
elif value == '3':
print('退出成功。')
break
else:
print('输入有误,请重新输入!')
if __name__ == '__main__':
show_main_page()