python简单编程代码图片100例(python简单代码图案)

http://www.itjxue.com  2023-04-01 04:33  来源:未知  点击次数: 

Python程序开发之简单小程序实例(11)小游戏-跳动的小球

Python程序开发之简单小程序实例

(11)小 游戏 -跳动的小球

一、项目功能

用户控制挡板来阻挡跳动的小球。

二、项目分析

根据项目功能自定义两个类,一个用于控制小球在窗体中的运动,一个用于接收用户按下左右键时,挡板在窗体中的运动。在控制小球的类中,我们还需要考虑当小球下降时,碰到挡板时的位置判断。

三、程序源代码

源码部分截图:

源码:

#!/usr/bin/python3.6

# -*- coding: GBK -*-

#导入相应模块

from tkinter import *

import random

import time

#自定义小球的类 Ball

class Ball:

# 初始化

def __init__(self,canvas,paddle,color):

#传递画布值

self.canvas=canvas

#传递挡板值

self.paddle=paddle

#画圆并且保存其ID

self.id=canvas.create_oval(10,10,25,25,fill=color)

self.canvas.move(self.id,245,100)

#小球的水平位置起始列表

start=[-3,-2,-1,1,2,3]

#随机化位置列表

random.shuffle(start)

self.x=start[0]

self.y=-2

self.canvas_heigh=self.canvas.winfo_height()#获取窗口高度并保存

self.canvas_width=self.canvas.winfo_width()

#根据参数值绘制小球

def draw(self):

self.canvas.move(self.id,self.x,self.y)

pos=self.canvas.coords(self.id)#返回相应ID代表的图形的当前坐标(左上角和右上角坐标)

#使得小球不会超出窗口

pad=self.canvas.coords(self.paddle.id)#获取小球挡板的坐标

if pos[1]=self.canvas_heigh or(pos[3]=pad[1] and pos[2]=pad[0] and pos[2]

python编程例子有哪些?

python编程经典例子:

1、画爱心表白、图形都是由一系列的点(X,Y)构成的曲线,由于X,Y满足一定的关系,所以就可以建立模型,建立表达式expression,当满足时,两个for循环(for X in range;for Y in range)就会每行每列的打印。

2、快递查询工具、此Python小项目需要用到json与requests两个库,还需调用API。

3、用python编程完成图像识别、鼠标模拟,需要调用OpenCV框架。

扩展资料:

Python的设计目标之一是让代码具备高度的可阅读性。它设计时尽量使用其它语言经常使用的标点符号和英文单字,让代码看起来整洁美观。它不像其他的静态语言如C、Pascal那样需要重复书写声明语句,也不像它们的语法那样经常有特殊情况和意外。

Python开发者有意让违反了缩进规则的程序不能通过编译,以此来强制程序员养成良好的编程习惯。并且Python语言利用缩进表示语句块的开始和退出,而非使用花括号或者某种关键字。增加缩进表示语句块的开始,而减少缩进则表示语句块的退出,缩进成为了语法的一部分。

使用Python 制作对比图片相似度的程序

import media

def red_average(pic):

'''Return an integer that represents the average red of the picture.

'''

total=0

for pixel in pic:

total = total + media.get_red(pixel)

red_average = total / (media.get_width(pic)*media.get_height(pic))

return red_average

def green_average(pic):

'''Return an integer that represents the average green of the picture

'''

total = 0

for pixel in pic:

total = total + media.get_green(pixel)

green_average = total / (media.get_width(pic)*media.get_height(pic))

return green_average

def blue_average(pic):

'''Return an integer that represents the average blue of the picture

'''

total = 0

for pixel in pic:

total = total + media.get_blue(pixel)

blue_average = total / (media.get_width(pic)*media.get_height(pic))

return blue_average

def scale_red(pic, value):

'''Return the picture that the average of the red is value which has been set.

'''

averaged = red_average(pic)

factor = float(value) / averaged

for pixel in pic:

new_red = min(255, int(factor * media.get_red(pixel)))

media.set_red(pixel,new_red)

return pic

def scale_green(pic, value):

'''Return the picture that the average of the green is value which has been set.

'''

averaged = green_average(pic)

factor = float(value) / averaged

for pixel in pic:

new_green = min(255, int(factor * media.get_green(pixel)))

media.set_green(pixel,new_green)

return pic

def scale_blue(pic, value):

'''Return the picture that the average of the blue is value which has been set.

'''

averaged = blue_average(pic)

factor = float(value) / averaged

for pixel in pic:

new_blue = min(255, int(factor * media.get_blue(pixel)))

media.set_blue(pixel,new_blue)

return pic

def expand_height(pic, factor):

'''Return a newpicture that has been vertically stretched by the factor which has been set.

'''

new_width = pic.get_width()

new_height = pic.get_height()*factor

newpic = media.create_pic(new_width, new_height, media.black)

for pixel in pic:

x = media.get_x(pixel)

y = media.get_y(pixel)

newpixel = media.get_pixel(newpic, x, y*factor)

for newpixel in newpic:

new_red = media.get_red(pixel)

new_green = media.get_green(pixel)

new_blue = media.get_blue(pixel)

media.set_red(newpixel,new_red)

media.set_green(newpixel,new_green)

media.set_blue(newpixel,new_blue)

return newpic

def expand_width(pic,factor):

'''Return a newpicture that has been horizontally stretched by the factor which has been set.

'''

new_width = pic.get_width() * factor

new_height = pic.get_height()

newpic = media.create_pic(new_width,new_height,media.black)

for newpixel in newpic:

x = media.get_x(newpixel)

y = media.get_y(newpixel)

pixel = media.get_pixel(pic,x / factor, y)

new_red = media.get_red(pixel)

new_green = media.get_green(pixel)

new_blue = media.get_blue(pixel)

media.set_red(newpixel,new_red)

media.set_green(newpixel,new_green)

media.set_blue(newpixel,new_blue)

return newpic

def reduce_height(pic, factor):

'''return a new pic that has been compressed vertically by the factor which has been set

'''

# Create a new, all-black pic with the appropriate new height and

# old width; (all colour components are zero).

new_width = pic.get_width

new_height = (pic.get_height() - 1) / factor + 1

newpic = media.create_pic(new_width, new_height, media.black)

# Iterate through all the pixels in the original (large) image, and copy

# a portion of each pixel's colour components into the correct

# pixel position in the smaller image.

for pixel in pic:

# Find the corresponding pixel in the new pic.

x = media.get_x(pixel)

y = media.get_y(pixel)

newpixel = media.get_pixel(newpic, x, y / factor)

# Add the appropriate fraction of this pixel's colour components

# to the components of the corresponding pixel in the new pic.

new_red = newpixel.get_red()+pixel.get_red()/factor

new_green = newpixel.get_green()+pixel.get_green()/factor

new_blue = newpixel.get_blue()+pixel.get_blue()/fctor

media.set_red(newpixel,new_red)

media.set_green(newpixel,new_green)

media.set_blue(newpixel,new_blue)

return newpic

def reduce_width(pic,factor):

'''Return a newpic that has been horizontally compressed by the factor which has been set.

'''

new_width = (media.get_width() - 1) / factor + 1

new_height = media.get_height()

newpic = media.create_pic(new_width, new_height, media.black)

for pixel in pic:

x = media.get_x(pixel)

y = media.get_y(pixel)

new_pixel = media.get_pixel(newpic, x / factor, y)

new_red = newpixel.get_red() + pixel.get_red() / factor

new_green = newpixel.get_green() + pixel.get() / factor

new_blue = newpixel.get_blue() + pixel.get()/factor

media.set_red(newpixel, new_red)

media.set_green(newpixel, new_green)

media.set_blue(newpixel, new_blue)

return newpic

def distance(pixel1, pixel2):

red1 = media.get_red(pixel1)

green1 = media.get_green(pixel1)

blue1 = media.get_blue(pixel1)

red2 = media.get_red(pixel2)

green2 = media.get_green(pixel2)

blue2 = media.get_blue(pixel2)

sum = abs(red1 -red2) + abs(green1 - green2) + abs(blue1 - blu2)

return sum

def simple_difference(pic1, pic2):

for pixel in pic1:

x = media.get_x(pixel)

y = media.get_y(pixel)

pixel2 = media.get_pixel(pic2, x, y)

sum = media.distance(pixel, pixel2)

return sum

def smart_difference(pic1,pic2):

height1 = media.get_height(pic1)

height2 = media.get_height(pic2)

factorh = float(height1 / height2)

if factorh = 1:

height1 = media.reduce_height(pic1, factorh)

else:

height2 = media.reduce_height(pic2, 1 / factorh)

width1 = media.get_width(pic1)

width2 = media.get_width(pic2)

factorw = float(width1 / width2)

if factorw = 1:

width1 = reduce_width(pic1, factorw)

else:

width2 = reduce_width(pic2, 1 / factorw)

red1 = red_average(pic1)

green1 = green_average(pic1)

blue1 = blue_average(pic1)

red2 = media.scale_red(pic2, red1)

green2 = media.scale_green(pic2, green1)

blue2 = media.scale_blue(pic2, blue1)

#if __name__ == '__main__':

#media.show(newpic)

写一个20行以上的python简单代码

class?Price:

????ticket_d?=?100

????ticket_w?=?ticket_d*1.2

????def?rq(self):

????????self.a?=?int(input('请输入是平日还是周末(平日:1/周末:0):'))

????????if?self.a?==?0:

????????????self.p?=?self.ticket_w

????????if?self.a?==?1:

????????????self.p?=?self.ticket_d

????def?pj(self):

????????input('请输入是人数'?+?'\n')

????????self.ad?=?int(input('大人数量:'))

????????self.ch?=?int(input('儿童数量:'))

????????self.money?=?self.p?*self.ad?+?self.p?*self.ch/2

????????print('%f'?%?self.money)

class?Ticket():

????????def?__init__(self,?weekend=False,?child=False):

????????????????self.exp?=?100

????????????????if?weekend:

????????????????????????self.inc?=?1.2

????????????????else:

????????????????????????self.inc?=?1

????????????????if?child:

????????????????????????self.discount?=?0.5

????????????????else:

????????????????????????self.discount?=?1

????????def?calcPrice(self,?num):

????????????????return?self.exp?*?self.inc?*?self.discount?*?num

adult?=?Ticket()

child?=?Ticket(child=True)

print("2个成人?+?1个小孩平日票价为:%.2f"?%?(adult.calcPrice(2)?+?child.calcPrice(1)))

python编程问题?

可以使用 Python 的 itertools 模块来生成所有可能的组合,然后再计算每个组合的和。以下是示例代码:

在此代码中,我们首先使用 itertools.combinations() 函数生成了所有可能的组合,并将它们存储在一个名为 combinations 的列表中。然后,我们对列表中的每个组合求和,将它们存储在另一个名为 sums 的列表中。最后,我们通过将 sums 转换为一个集合来确定唯一的和,并打印出我们找到的所有唯一和的数量以及它们各自的值。

请注意,如果数字非常多或者复杂度高,则可能需要考虑优化该代码,以便更有效地生成和计算组合的和。

python有趣的编程代码

class?Point:

??row=0

??col=0

??def?__init__(self,?row,?col):

????self.row=row

????self.col=col

??def?copy(self):

????return?Point(row=self.row,?col=self.col)

#初始框架

import?pygame

import?random

#初始化

pygame.init()

W=800

H=600

ROW=30

COL=40

size=(W,H)

window=pygame.display.set_mode(size)

pygame.display.set_caption('贪吃蛇')

bg_color=(255,255,255)

snake_color=(200,200,200)

head=Point(row=int(ROW/2),?col=int(COL/2))

head_color=(0,128,128)

snakes=[

??Point(row=head.row,?col=head.col+1),

??Point(row=head.row,?col=head.col+2),

??Point(row=head.row,?col=head.col+3)

]

#生成食物

def?gen_food():

??while?1:

????pos=Point(row=random.randint(0,ROW-1),?col=random.randint(0,COL-1))

????#

????is_coll=False

????#是否跟蛇碰上了

????if?head.row==pos.row?and?head.col==pos.col:

??????is_coll=True

????#蛇身子

????for?snake?in?snakes:

??????if?snake.row==pos.row?and?snake.col==pos.col:

????????is_coll=True

????????break

????if?not?is_coll:

??????break

??return?pos

#定义坐标

food=gen_food()

food_color=(255,255,0)

direct='left'???????#left,right,up,down

#

def?rect(point,?color):

??cell_width=W/COL

??cell_height=H/ROW

??left=point.col*cell_width

??top=point.row*cell_height

??pygame.draw.rect(

????window,?color,

????(left,?top,?cell_width,?cell_height)

??)

??pass

#游戏循环

quit=True

clock=pygame.time.Clock()

while?quit:

??#处理事件

??for?event?in?pygame.event.get():

????if?event.type==pygame.QUIT:

??????quit=False

????elif?event.type==pygame.KEYDOWN:

??????if?event.key==273?or?event.key==119:

????????if?direct=='left'?or?direct=='right':

??????????direct='up'

??????elif?event.key==274?or?event.key==115:

????????if?direct?==?'left'?or?direct?==?'right':

??????????direct='down'

??????elif?event.key==276?or?event.key==97:

????????if?direct?==?'up'?or?direct?==?'down':

??????????direct='left'

??????elif?event.key==275?or?event.key==100:

????????if?direct?==?'up'?or?direct?==?'down':

??????????direct='right'

??#吃东西

??eat=(head.row==food.row?and?head.col==food.col)

??#重新产生食物

??if?eat:

????food?=?gen_food()

??#处理身子

??#1.把原来的头,插入到snakes的头上

??snakes.insert(0,?head.copy())

??#2.把snakes的最后一个删掉

??if?not?eat:

????snakes.pop()

??#移动

??if?direct=='left':

????head.col-=1

??elif?direct=='right':

????head.col+=1

??elif?direct=='up':

????head.row-=1

??elif?direct=='down':

????head.row+=1

??#检测

??dead=False

??#1.撞墙

??if?head.col0?or?head.row0?or?head.col=COL?or?head.row=ROW:

????dead=True

??#2.撞自己

??for?snake?in?snakes:

????if?head.col==snake.col?and?head.row==snake.row:

??????dead=True

??????break

??if?dead:

????print('死了')

????quit=False

??#渲染——画出来

??#背景

??pygame.draw.rect(window,?bg_color,?(0,0,W,H))

??#蛇头

??for?snake?in?snakes:

????rect(snake,?snake_color)

??rect(head,?head_color)

??rect(food,?food_color)

??#

??pygame.display.flip()

??#设置帧频(速度)

??clock.tick(8)

#收尾工作

这是一个简易版贪吃蛇的代码,虽然结构简单,但是该有的功能都是完整的,可玩性也不错

(责任编辑:IT教学网)

更多

推荐安全技术文章