猫史档案馆


python作品大集合!!

用户:Error_Block错误方块Error_Block错误方块查看:0 回复:4 评论:0 创建时间:2021-08-13T16:22:15


话说,有没有喜欢python的小伙伴吖?如果你觉得自己的作品还不错,就分享一下吧!

我先来几个:

质数计算器

库:

math

gmpy2

time

import math
import gmpy2
import time
Error = 0
prime = ["质数"]
number = 0
print('请选择范围运算或者单个运算\n范围运算为1\n单个运算为2')
try:
    choose = int(input())
except:
    print('输入错误!')
    Error = 1
    exit()
primelist = []
if (choose == 1):
    print('请输入计算范围,用英文逗号","分隔:')
    tmp = input()
    tmp = tmp.split(',')
    try:
        a=int(tmp[0])
        b=int(tmp[1])
        begin = min(a,b)
        finish = max(a,b)
    except:
        print('输入错误!')
        Error = 1
        exit()
    if a <= 0 or b <= 0:
        print('输入错误!')
        Error = 1
        exit()
    if finish - begin > 50000000:
        print("数字过于庞大,计算时间太长!请减小范围!")       
        Error = 1
        exit()
    try:
        c = int(input("打印质数为1,仅打印质数数量为2,请选择输出方式:"))
    except:
        print('输入错误!')
        Error = 1
        exit()
    if  c == 1 and finish - begin > 200000:
        print("数字过于庞大,无法显示!请减小范围或更换简洁输出方式!")       
        Error = 1
        exit()
    if c != 1 and c != 2:
        print("输入错误!")
        Error = 1
        exit()
    start = time.time()
    for x in range(begin, (finish + 1)):
        if c == 1:
            if gmpy2.is_prime(x):
                prime.append(str(x))
        if gmpy2.is_prime(x):
            number += 1
    l = number
    z = ",".join(prime)
    if c == 1:
        print(prime)
    print("有{}个素数".format(l))
elif (choose == 2):
    print('需要查询的数字是:')
    try:
        y = int(input())
    except:
        print('输入错误!')
        Error = 1
        exit()
    start = time.time()
    if (y <= 0):
        print("输入错误")
    else:
        if gmpy2.is_prime(y):
            print("{}是素数".format(y))
        else:
            print("{}是和数".format(y))
else:
    print('输入错误!')
    Error = 1
    exit()
if Error == 0:
    end = time.time()
    time = end - start
    print("程序运行了{}秒".format(time))

冰雹猜想证实程序

库:

math

import math


#冰雹猜想
n = int(input('请输入起始值:'))
x = []
while (n != 1):
    x.append(n)
    print(n)
    if (n % 2 == 0):
        n = n // 2
    else:
        n = n * 3 + 1
print('1')
MAX = x[0]
for i in x:
    if (i > MAX):
        MAX = i
K = x.index(MAX)
T = ((len(x) - K) - 1)
if K != 0:
    print("经过了{}次运算到达顶峰值{},{}次运算到达谷底值1".format(K,MAX,T))
else:
    print("起始值即为顶峰值,{}次运算到达谷底值1".format(T) 

你们还有哪些精彩作品吗?

分享格式

1.作品名称

2.自己导入的库

3.作品介绍(选填)

4.代码(点击上面评论工具栏最右边的代码图标,复制代码输入进去)

相信你一定是最好的!!!


回复

上一页1 页 / 共 1下一页
小熊与雪小熊与雪

#初始化游戏
import pygame
import sys
import random
import time
pygame.init() 
canvas = pygame.display.set_mode((480, 852))
pygame.display.set_caption("飞机大战")
canvas.fill((255, 255, 255))
#导入图片
bg = pygame.image.load("images/bg1.png")
enemy1 = pygame.image.load("images/enemy1.png")
enemy2 = pygame.image.load("images/enemy2.png")
enemy3 = pygame.image.load("images/enemy3.png")
h = pygame.image.load("images/hero.png")
b = pygame.image.load("images/bullet1.png")
stop = pygame.image.load("images/game_pause_nor.png")
LOGO = pygame.image.load("images/LOGO.png")
startGame = pygame.image.load("images/startGame.png")
#鼠标退出函数
def isMouseOut(x,y):
    if x>479 or x<=0 or y>=喵9 or y<=0:
        return True
    else:
        return False
def isMouseOver(x, y):
    if x > 1 or x <= 479 or y > 1 or y < 喵9:
        return True
    else:
        return False
#退出函数
def handleEvent():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEMOTION:
            GameVar.hero.x = event.pos[0]-GameVar.hero.width/2
            GameVar.hero.y = event.pos[1]-GameVar.hero.height/2
            if isMouseOut(event.pos[0], event.pos[1]):
                if GameVar.state == GameVar.STATES['RUNNING']:
                    GameVar.state = GameVar.STATES['PAUSE']
            if isMouseOver(event.pos[0], event.pos[1]):
                if GameVar.state == GameVar.STATES['PAUSE']:
                    GameVar.state = GameVar.STATES['RUNNING']
        if event.type==pygame.MOUSEBUTTONDOWN and event.button==1:
            if GameVar.state == 1:
                GameVar.state = GameVar.STATES['RUNNING']
#时间间隔函数
def isActionTime(lastTime,insert):
    if lastTime == 0:
        return True
    newTime = time.time()
    return newTime - lastTime >= insert
#碰撞函数
def bong(a,b):
    if a.x > b.x-a.width and a.x < b.x+a.width+b.width and a.y > b.y-a.height and a.y < b.y+a.height+b.height:
        return True
    else:
        return False
#背景类
class Bg():
    width = 480
    height = 852
    def __init__(self):
        self.img = bg
        self.x1 = 0
        self.x2 = 0
        self.y1 = 0
        self.y2 = -Bg.height
    def step(self):
        self.y1 = self.y1+1
        self.y2 = self.y2+1
        if self.y1 > Bg.height:
            self.y1 = -Bg.height
        if self.y2 > Bg.height:
            self.y2 = -Bg.height
    def paint(self):
        canvas.blit(self.img, (self.x1, self.y1))
        canvas.blit(self.img, (self.x2, self.y2))
#敌机类
class Enemy():
    def __init__(self,x,y,width,height,life,score,img):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.life = life
        self.score = score
        self.img = img
    def paint(self):
        canvas.blit(self.img,(self.x,self.y))
    def step(self):
        self.y = self.y + 2
#英雄类
class Hero():
    def __init__(self,x,y,width,height,life,img):
        self.width = width
        self.height = height
        self.x = (Bg.width+self.width)/2
        self.y = (Bg.height+self.height)/2
        self.life = life
        self.img = img
        self.shootLastTime = 0
        self.shootInsert = 0.3
    def paint(self):
        canvas.blit(self.img, (self.x, self.y))
    def shoot(self):
        if isActionTime(self.shootLastTime, self.shootInsert):
            self.shootLastTime = time.time()
            GameVar.bullets.append(Bullet(self.x+(self.width-9)/2, self.y-21, 9, 21, 1, b))
#子弹类
class Bullet():
    def __init__(self, x, y, width, height, life, img):
        self.width = width
        self.height = height
        self.x = x
        self.y = y
        self.life = life
        self.img = img
    def paint(self):
        canvas.blit(self.img, (self.x, self.y))
    def step(self):
        self.y = self.y - 2
#游戏数据类
class GameVar():
    #创建背景角色
    sky = Bg()
    #创建敌机列表
    enemies = []
    #敌机创建时间
    enemyLastTime = 0
    enemyInsert = 1
    #飞行物重绘时间
    allLastTime = 0
    allInsert = 0.04
    #英雄机
    hero = Hero(0,0,60,75,1,h)
    #创建子弹列表
    bullets = []
    #分数生命
    score = 0
    life = 1
    #游戏状态
    STATES ={'STATE':1,'RUNNING':2,'PAUSE':3,'GAME_OVER':4}
    state = STATES['STATE']
#添加所有图片函数
def allEnter():
    #添加飞机
    if isActionTime(GameVar.enemyLastTime, GameVar.enemyInsert):
        GameVar.enemyLastTime = time.time()
        a = random.randint(1,100)
        x1 = random.randint(0, Bg.width-57)
        y1 = -45
        x2 = random.randint(0, Bg.width-50)
        y2 = -68
        x3 = random.randint(0, Bg.width-100)
        y3 = -153
        if a >= 95:
            GameVar.enemies.append(Enemy(x3, y3, 100, 153, 10, 5, enemy3))
        elif a >= 80:
            GameVar.enemies.append(Enemy(x2, y2, 50, 68, 5, 3, enemy2))
        else:
            GameVar.enemies.append(Enemy(x1, y1, 57, 45, 3, 1, enemy1))
    #添加子弹
    GameVar.hero.shoot()
#写文字方法
def text(text,position,big):
    my_font = pygame.font.SysFont("微软雅黑",big)
    newText = my_font.render(text,True,(255,255,255))
    canvas.blit(newText,position)
#画所有图片函数
def allPaint():
    GameVar.sky.paint()
    for enemy in GameVar.enemies:
        enemy.paint()
    GameVar.hero.paint()
    for b in GameVar.bullets:
        b.paint()
    text('score:'+str(GameVar.score),(0,0),40)
    text('life:'+str(GameVar.life), (380, 0),40)

#移动所有图片函数
def allStep():
    GameVar.sky.step()
    for enemy in GameVar.enemies:
        enemy.step()
        if enemy.y > Bg.height:
            GameVar.enemies.remove(enemy)
        for b in GameVar.bullets:
            if bong(enemy,b):
                GameVar.bullets.remove(b)
                enemy.life -= 1
                if enemy.life <= 0:
                    GameVar.score += enemy.score
                    GameVar.enemies.remove(enemy)
        if bong(enemy,GameVar.hero):
            GameVar.enemies.remove(enemy)
            GameVar.life-=1
            if GameVar.life <= 0:
                GameVar.state = GameVar.STATES['GAME_OVER']
    for b in GameVar.bullets:
        b.step()
        if b.y < 0:
            GameVar.bullets.remove(b)
#运行函数
def gameUp():
    if GameVar.state == 1:
        GameVar.sky.paint()
        GameVar.sky.step()
        canvas.blit(LOGO,(-40,200))
        canvas.blit(startGame,(150,400))
    elif GameVar.state == 2:
        allStep()
        allPaint()
        allEnter()
    elif GameVar.state == 3:
        allPaint()
        canvas.blit(stop,(Bg.width/2,Bg.height/2))
    else:
        GameVar.sky.paint()
        GameVar.sky.step()
        text('GameOver',(60,320),100)

while True:
    gameUp()
    pygame.display.update()
    handleEvent()

点赞0


评论


忠厚的乌力HYdz忠厚的乌力HYdz

#导入模块
import pygame
from pygame.locals import *
import sys,random,time,math
class GameWindow(object):

def __init__(self,*args,*kw):
self.window_length = 600
self.window_wide = 500
#绘制游戏窗口,设置窗口尺寸
self.game_window = pygame.display.set_mode((self.window_length,self.window_wide))
#设置游戏窗口标题
pygame.display.set_caption("CatchBallGame")
#定义游戏窗口背景颜色参数
self.window_color = (135,206,250)
def backgroud(self):
#绘制游戏窗口背景颜色
self.game_window.fill(self.window_color)
class Ball(object):
'''创建球类'''
def __init__(self,*args,**kw):
#设置球的半径、颜色、移动速度参数
self.ball_color = (255,215,0)
self.move_x = 1
self.move_y = 1
self.radius = 10
def ballready(self):
#设置球的初始位置、
self.ball_x = self.mouse_x
self.ball_y = self.window_wide-self.rect_wide-self.radius
#绘制球,设置反弹触发条件
pygame.draw.circle(self.game_window,self.ball_color,(self.ball_x,self.ball_y),self.radius)
def ballmove(self):
#绘制球,设置反弹触发条件
pygame.draw.circle(self.game_window,self.ball_color,(self.ball_x,self.ball_y),self.radius)
self.ball_x += self.move_x
self.ball_y -= self.move_y
#调用碰撞检测函数
self.ball_window()
self.ball_rect()
#每接5次球球速增加一倍
if self.distance < self.radius:
self.frequency += 1
if self.frequency == 5:
self.frequency = 0
self.move_x += self.move_x
self.move_y += self.move_y
self.point += self.point
#设置游戏失败条件
if self.ball_y > 520:
self.gameover = self.over_font.render("Game Over",False,(0,0,0))
self.game_window.blit(self.gameover,(100,130))
self.over_sign = 1
class Rect(object):
'''创建球拍类'''
def __init__(self,*args,**kw):
#设置球拍颜色参数
self.rect_color = (255,0,0)
self.rect_length = 100
self.rect_wide = 10
def rectmove(self):
#获取鼠标位置参数
self.mouse_x,self.mouse_y = pygame.mouse.get_pos()
#绘制球拍,限定横向边界
if self.mouse_x >= self.window_length-self.rect_length//2:
self.mouse_x = self.window_length-self.rect_length//2
if self.mouse_x <= self.rect_length//2:
self.mouse_x = self.rect_length//2
pygame.draw.rect(self.game_window,self.rect_color,((self.mouse_x-self.rect_length//2),(self.window_wide-self.rect_wide),self.rect_length,self.rect_wide))
class Brick(object):
def __init__(self,*args,**kw):
#设置砖块颜色参数
self.brick_color = (139,126,102)
self.brick_list = [[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1]]
self.brick_length = 80
self.brick_wide = 20
def brickarrange(self):
for i in range(5):
for j in range(6):
self.brick_x = j*(self.brick_length+24)
self.brick_y = i*(self.brick_wide+20)+40
if self.brick_list[i][j] == 1:
#绘制砖块
pygame.draw.rect(self.game_window,self.brick_color,(self.brick_x,self.brick_y,self.brick_length,self.brick_wide))
#调用碰撞检测函数
self.ball_brick()
if self.distanceb < self.radius:
self.brick_list[i][j] = 0
self.score += self.point
#设置游戏胜利条件
if self.brick_list == [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]]:
self.win = self.win_font.render("You Win",False,(0,0,0))
self.game_window.blit(self.win,(100,130))
self.win_sign = 1
class Score(object):
'''创建分数类'''
def __init__(self,*args,**kw):
#设置初始分数
self.score = 0
#设置分数字体
self.score_font = pygame.font.SysFont('arial',20)
#设置初始加分点数
self.point = 1
#设置初始接球次数
self.frequency = 0
def countscore(self):

点赞0


评论


Error_Block错误方块Error_Block错误方块

emmmmmm......代码岛的也可以吧,我是viscode。

点赞0


评论


滑稽的麦霸菇_点点滑稽的麦霸菇_点点

import random


print('你领养了一只新的小金鱼')
name = input('小金鱼的名字是:')
HP = 1000
eggs = 0
old = 0.1
print((('小金鱼' + name) + '陪你玩耍'))
while True:
message = input((('要小金鱼' + name) + '做什么 (eat our lay): '))
if (HP > 2000):
print('小金鱼撑死了')
break
if (HP < 0):
print('小金鱼感觉身体被掏空')
break
if (message == 'eat'):
HP = (HP + random.randint(300, 400))
old = (old + 0.1)
print('什么东西,真香')
elif (message == 'lay'):
lay = random.randint(10, 20)
HP = (HP + random.randint((-450), (-350)))
old = (old + 0.1)
print((('产下了' + str(lay)) + '颗卵'))
eggs = (eggs + lay)
else:
print('哎?你在干哈?')
print(('产卵总数:' + str(eggs)))
print((((('小金鱼' + str(name)) + '活了') + str(old)) + '岁'))

点赞0


评论