在黑色画布里移动白色小方块,小游戏

import sys
import pygame

WIDTH=800
HEIGHT=600
SIZE=WIDTH,HEIGHT
pygame.init()
screen=pygame.display.set_mode(SIZE)

class Player:
	def __init__(self):
		self.surf = pygame.Surface((50,50))
		self.surf.fill("white")
		self.rect=self.surf.get_rect()
		# self.rect = self.surf.get_rect()

	def move(self):
		pressed_key = pygame.key.get_pressed()
		if pressed_key[pygame.K_DOWN]:
			self.rect.move_ip(0, 10)
		# player_rect.top +=10
		if pressed_key[pygame.K_UP]:
			self.rect.move_ip(0, -10)
		# player_rect.top -=10
		if pressed_key[pygame.K_RIGHT]:
			self.rect.move_ip(10, 0)
		# player_rect.right +=10
		if pressed_key[pygame.K_LEFT]:
			self.rect.move_ip(-10, 0)
		# player_rect.left -=10
		# 限制玩家在屏幕中移动
		if self.rect.right >= WIDTH:
			self.rect.right = WIDTH
		if self.rect.left <= 0:
			self.rect.left = 0
		if self.rect.bottom >= HEIGHT:
			self.rect.bottom = HEIGHT
		if self.rect.top <= 0:
			self.rect.top = 0

	def draw(self):
		screen.blit(self.surf,self.rect)

player=Player()
clock=pygame.time.Clock()
while True:
	for event in pygame.event.get():
		if event.type==pygame.QUIT:
			pygame.quit()
			sys.exit()
	# if event.type==pygame.KEYDOWN and event.key==pygame.K_DOWN:
	# player_rect.top +=10
	# move
	player.move()
	# draw
	screen.fill("black")
	player.draw()
	pygame.display.flip()
	clock.tick(60)

tkinter选项

#coding: utf-8
from tkinter import *
from tkinter import ttk
import tkinter.font as tkFont
root=Tk()
root.geometry('400x400')
root.title('20181201')
ft=tkFont.Font(family='宋体',size=12)
e1=StringVar()
fm1=Frame(root)
fm1.pack()
fm2=Frame(root)
fm2.pack()
Label(fm1,text='进货方式',font=ft).pack()
comb1=ttk.Combobox(fm1,font=ft,textvariable=e1,width=14,state='readonly')
comb1.pack()
comb1['values']=('厂家进货','经销商进货','自己生产')
comb1.current(0)
Label(fm2,text='文具类型',font=ft).pack()
listb1=Listbox(fm2,font=('宋体',12),selectmode=SINGLE,width=12,height=5,exportselection=False)
listb1.pack()
for item in ['铅笔','毛笔','钢笔','圆珠笔','中性笔']:
	listb1.insert(END,item)
listb1.select_set(0)#默认选第一个
mainloop()

生成随机密码

import random

def generate_password():
    chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*"
    password = ""
    for _ in range(10):
        password += random.choice(chars)
    return password

def generate_passwords():
    passwords = []
    first_chars = set()
    while len(passwords) < 10:
        password = generate_password()
        if password[0] not in first_chars:
            first_chars.add(password[0])
        passwords.append(password)
    return passwords
    
random.seed(0x1010)
passwords = generate_passwords()
for password in passwords:
    print(password)

UDP通信

客户端

from socket import *

serverName='localhost'
serverPort=7788
clientSocket=socket(AF_INET,SOCK_DGRAM)
while True:
    message=input('input lowercase sentence or "quit" to exit:\n')
    if message=='quit':
        break
    clientSocket.sendto(message.encode(),(serverName,serverPort))
    modifiedMessage,serverAddress=clientSocket.recvfrom(2048)
    print(modifiedMessage)
    print(serverAddress)

clientSocket.close()

服务端

from socket import *

serverPort=7788
serverSocket=socket(AF_INET,SOCK_DGRAM)
serverSocket.bind(('localhost',serverPort))
print('the server is ready to receive:')
while True:
	message,clientAddress=serverSocket.recvfrom(2048)
	print(clientAddress,message)
	modifiedMessage=message.upper()
	serverSocket.sendto(modifiedMessage,clientAddress)

时序数据的自相关函数(ACF)

import numpy as np
import matplotlib.pyplot as plt
# 手动输入北京过去几年的人口数据
years = np.array([2019, 2020, 2021, 2022, 2023])
population = np.array([21901000, 21893000, 21860000, 21843000, 21858000])
# 计算ACF
def acf(x, lag_param): # 使用lag_param以避免名称冲突
    n = len(x)
    mean_x = np.mean(x)
    c0 = np.sum((x - mean_x) ** 2) / n
    values = []
    for k in range(lag_param):
        if k == 0:
            c = np.sum((x - mean_x) ** 2) / n
        else:
            c = np.sum((x[k:] - mean_x) * (x[:-k] - mean_x)) / n
        values.append(c / c0)
    return np.array(values)
# 设定滞后阶数
lag = 10 # 直接在函数外部定义
# 计算ACF值
acf_values = acf(population, lag)
# 绘制ACF图
plt.bar(range(lag), acf_values)
plt.title('ACF for Beijing Population')
plt.xlabel('Lag')
plt.ylabel('ACF Value')
plt.xticks(range(lag))
plt.show()
posted @ 2024-11-16 23:39  园糯  阅读(3)  评论(0编辑  收藏  举报