135-1821-9792

flip函数python flip函数怎么用

Python3.4.1异常: 'float' object cannot be interpreted as an integer

'float' object cannot be interpreted as an integer的意思是:float类型不能解释为int类型 。

创新互联凭借在网站建设、网站推广领域领先的技术能力和多年的行业经验,为客户提供超值的营销型网站建设服务,我们始终认为:好的营销型网站就是好的业务员。我们已成功为企业单位、个人等客户提供了成都做网站、网站制作服务,以良好的商业信誉,完善的服务及深厚的技术力量处于同行领先地位。

代码错误处应该发生在图中红框内的代码语句中。

因为使用的是Python3所以在所框语句中应该使用//去代替/。

扩展资料:

Python贪吃蛇代码:

import pygame,sys,random,time

from pygame.locals import *   #从pygame模块导入常用的函数和常量

#定义颜色变量

black_colour = pygame.Color(0,0,0)

white_colour = pygame.Color(255,255,255)

red_colour = pygame.Color(255,0,0)

grey_colour = pygame.Color(150,150,150)

#定义游戏结束函数

def GameOver(gamesurface):

#设置提示字体的格式

GameOver_font = pygame.font.SysFont("MicrosoftYaHei", 16)

#设置提示字体的颜色

GameOver_colour = GameOver_font.render('Game Over',True,grey_colour)

#设置提示位置

GameOver_location = GameOver_colour.get_rect()

GameOver_location.midtop = (320,10)

#绑定以上设置到句柄

gamesurface.blit(GameOver_colour,GameOver_location)

#提示运行信息

pygame.display.flip()

#休眠5秒

time.sleep(5)

#退出游戏

pygame.quit()

#退出程序

sys.exit()

#定义主函数

def main():

#初始化pygame,为使用硬件做准备

pygame.init()

pygame.time.Clock()

ftpsClock = pygame.time.Clock()

#创建一个窗口

gamesurface = pygame.display.set_mode((640,480))

#设置窗口的标题

pygame.display.set_caption('tanchishe snake')

#初始化变量

#初始化贪吃蛇的起始位置

snakeposition = [100,100]

#初始化贪吃蛇的长度

snakelength = [[100,100],[80,100],[60,100]]

#初始化目标方块的位置

square_purpose = [300,300]

#初始化一个数来判断目标方块是否存在

square_position = 1

#初始化方向,用来使贪吃蛇移动

derection = "right"

change_derection = derection

#进行游戏主循环

while True:

#检测按键等pygame事件

for event in pygame.event.get():

if event.type==QUIT:

#接收到退出事件后,退出程序

pygame.quit()

sys.exit()

elif event.type==KEYDOWN:

#判断键盘事件,用w,s,a,d来表示上下左右

if event.key==K_RIGHT or event.key==ord('d'):

change_derection = "right"

if event.key==K_LEFT or event.key==ord('a'):

change_derection = "left"

if event.key==K_UP or event.key==ord('w'):

change_derection = "up"

if event.key==K_DOWN or event.key==ord('s'):

change_derection = "down"

if event.key==K_ESCAPE:

pygame.event.post(pygame.event.Event(QUIT))

#判断移动的方向是否相反

if change_derection =='left'and not derection =='right':

derection = change_derection

if change_derection =='right'and not derection =='left':

derection = change_derection

if change_derection == 'up' and not derection =='down':

derection = change_derection

if change_derection == 'down' and not derection == 'up':

derection = change_derection

#根据方向,改变坐标

if derection == 'left':

snakeposition[0] -= 20

if derection == 'right':

snakeposition[0] += 20

if derection == 'up':

snakeposition[1] -= 20

if derection == 'down':

snakeposition[1] += 20

#增加蛇的长度

snakelength.insert(0,list(snakeposition))

#判断是否吃掉目标方块

if snakeposition[0]==square_purpose[0] and snakeposition[1]==square_purpose[1]:

square_position = 0

else:

snakelength.pop()

#重新生成目标方块

if square_position ==0:

#随机生成x,y,扩大二十倍,在窗口范围内

x = random.randrange(1,32)

y = random.randrange(1,24)

square_purpose = [int(x*20),int(y*20)]

square_position = 1

#绘制pygame显示层

gamesurface.fill(black_colour)

for position in snakelength:

pygame.draw.rect(gamesurface,white_colour,Rect(position[0],position[1],20,20))

pygame.draw.rect(gamesurface,red_colour,Rect(square_purpose[0],square_purpose[1],20,20))

#刷新pygame显示层

pygame.display.flip()

#判断是否死亡

if snakeposition[0]0 or snakeposition[0]620:

GameOver(gamesurface)

if snakeposition[1]0 or snakeposition[1]460:

GameOver(gamesurface)

for snakebody in snakelength[1:]:

if snakeposition[0]==snakebody[0] and snakeposition[1]==snakebody[1]:

GameOver(gamesurface)

#控制游戏速度

ftpsClock.tick(5)

if __name__ == "__main__":

main()

python编程中的def sinplot(flip=1):这里的flip是什么?

def sinplot(flip=1): #函数定义。sinplot:函数名称;flip:参数名称。flip=1:默认参数是1.如果没有在调用函数时,没有带参数,参数默认值是1.

python外星人入侵游戏左移正常,右移屏幕上不显示,但实际上已经移动了?

主要看看向右移动的地方的代码有没有问题:

# 设置向右移动flag

self.move_right = False

def listening_key_down(self, event):

"""

监听key_down事件

:param event:

"""

if event.key == pygame.K_RIGHT:

self.air_ship.move_right = True

elif event.key == pygame.K_LEFT:

self.air_ship.move_left = True

def listening_key_up(self, event):

"""

监听key_up事件

:param event:

"""

if event.key == pygame.K_RIGHT:

self.air_ship.move_right = False

elif event.key == pygame.K_LEFT:

self.air_ship.move_left = False

def move(self):

"""

更新飞船横轴位置,实现左右移动

"""

if self.move_right and self.air_ship_rect.right = self.screen_rect.right:

self.center += self.speed_factor        if self.move_left and self.air_ship_rect.left = self.screen_rect.left:

self.center -= self.speed_factor

self.air_ship_rect.centerx = self.center

def listening_game_event(self):

"""

监听事件

"""

for event in pygame.event.get():            if event.type == pygame.QUIT:

pygame.quit()

sys.exit()            elif event.type == pygame.KEYDOWN:

self.listening_key_down(event)            elif event.type == pygame.KEYUP:

self.listening_key_up(event)

# 游戏主循环

while True:            self.listening_game_event()            self.air_ship.move()            self.update_game_screen()

python读取保存多帧图片数量少了

cv2.imshow("left", img_left)

filename3=str(number)+'n3'+'.jpg' #打印第number张图片+增值方式+保存类型

cv2.imwrite(savedpath + filename3, img_left)

"""

# 数据增强实现

"""

import cv2

import numpy as np

import os

# 图像平移

def img_translation(image):

# 图像平移 下、上、右、左平移

M = np.float32([[1, 0, 0], [0, 1, 100]])

img_down = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

M = np.float32([[1, 0, 0], [0, 1, -100]])

img_up = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

M = np.float32([[1, 0, 100], [0, 1, 0]])

img_right = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

M = np.float32([[1, 0, -100], [0, 1, 0]])

img_left = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

# 保存图片,需要保存上述的哪一图片,就在cv2.imwrite()中,将哪一图片名放入。

# filename='xxx' +'.jpeg'

# cv2.imwrite(savedpath + filename, img_left)

# 显示图形

cv2.imshow("down", img_down)

filename0=str(number)+'n0'+'.jpg'

cv2.imwrite(savedpath + filename0, img_down)

cv2.imshow("up", img_up)

filename1=str(number)+'n1'+'.jpg'

cv2.imwrite(savedpath + filename1, img_up)

cv2.imshow("right", img_right)

filename2=str(number)+'n2'+'.jpg'

cv2.imwrite(savedpath + filename2, img_right)

cv2.imshow("left", img_left)

filename3=str(number)+'n3'+'.jpg'

cv2.imwrite(savedpath + filename3, img_left)

# 图像缩放

def img_scale(image):

result = cv2.resize(image, (224, 224))

cv2.imshow("scale", result)

filename=str(number)+'n5'+'.jpg'

cv2.imwrite(savedpath + filename, result)

# 图像翻转

def img_flip(image):

# 0以X轴为对称轴翻转,0以Y轴为对称轴翻转, 0X轴Y轴翻转

horizontally = cv2.flip(image, 0) # 水平镜像

vertically = cv2.flip(image, 1) # 垂直镜像

hv = cv2.flip(image, -1) # 水平垂直镜像

# 显示图形

cv2.imshow("Horizontally", horizontally)

filename1=str(number)+'n6'+'.jpg'

cv2.imwrite(savedpath + filename1, horizontally)

cv2.imshow("Vertically", vertically)

filename2=str(number)+'n7'+'.jpg'

cv2.imwrite(savedpath + filename2, vertically)

cv2.imshow("Horizontally Vertically", hv)

filename3=str(number)+'n8'+'.jpg'

cv2.imwrite(savedpath + filename3, hv)

# 图像旋转

def img_rotation(image):

# 原图的高、宽 以及通道数

rows, cols, channel = image.shape

# 绕图像的中心旋转

# 参数:旋转中心 旋转度数 scale

M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 30, 1)

# 参数:原始图像 旋转参数 元素图像宽高

rotated = cv2.warpAffine(image, M, (cols, rows))

# 显示图像

cv2.imshow("rotated", rotated)

filename1=str(number)+'n9'+'.jpg'

cv2.imwrite(savedpath + filename1, rotated)

#选装60度

W = cv2.getRotationMatrix2D((cols / 2, rows / 2), 60, 1)

# 参数:原始图像 旋转参数 元素图像宽高

rotated1 = cv2.warpAffine(image, W, (cols, rows))

cv2.imshow("rotated", rotated)

filename2=str(number)+'n12'+'.jpg'

cv2.imwrite(savedpath + filename2, rotated1)

#选装145度

W = cv2.getRotationMatrix2D((cols / 2, rows / 2), 60, 1)

# 参数:原始图像 旋转参数 元素图像宽高

rotated2 = cv2.warpAffine(image, W, (cols, rows))

cv2.imshow("rotated", rotated)

filename3=str(number)+'n13'+'.jpg'

cv2.imwrite(savedpath + filename3, rotated2)

# 图像加噪

def img_noise(image, mean=0, var=0.001):

'''

添加高斯噪声

mean : 均值

var : 方差

'''

image = np.array(image / 255, dtype=float)

noise = np.random.normal(mean, var ** 0.5, image.shape)

out = image + noise

if out.min() 0:

low_clip = -1.

else:

low_clip = 0.

out = np.clip(out, low_clip, 1.0)

out = np.uint8(out * 255)

cv2.imshow("noise", out)

filename3=str(number)+'n10'+'.jpg'

cv2.imwrite(savedpath + filename3, out)

# 图像亮度调节

def img_brightness(image):

contrast = 1 # 对比度

brightness = 100 # 亮度

pic_turn = cv2.addWeighted(image, contrast, image, 0, brightness)

# cv2.addWeighted(对象,对比度,对象,对比度)

'''cv2.addWeighted()实现的是图像透明度的改变与图像的叠加'''

cv2.imshow('bright', pic_turn) # 显示图片

filename3=str(number)+'n11'+'.jpg'

cv2.imwrite(savedpath + filename3, pic_turn)

if __name__ == '__main__':

i = 0

path = '../Data/'

print(path)

savedpath = './result_new/'

filelist = os.listdir(path)

total_num = len(filelist)

for item in filelist:

number = i + 1

i = number

print("######")

print("打印到第",i,"张图片")

src = cv2.imread(path + item)

img_translation(src)

img_scale(src)

img_flip(src)

img_rotation(src)

img_noise(src)

img_brightness(src)

cv2.waitKey(0)

cv2.destroyAllWindows()

代码较为繁琐,有空之后进行优化

输出结果

用python的pygame,但每次程序一运行,pygame window窗口能出现,但是下图这种状态。

event英文翻译是事件 get 获取  type是样式,类型的意思,所以他这个都是有规律的,但是有顺序事件在前,后面跟属性

同时也有父子(层级)关系

event.get() 获取到的事件

event.type() 事件类型

event.key() 按键事件

一般都是写在while里面,因为这是个不断循环的过程,不然,只执行一便达不到实际要求,这是Pygame里面设定好的

这是贪吃蛇游戏的一段代码(一部分):

txt = font.render('GAME OVER', True, (255, 0, 0))

screen.blit(txt, (size[0]/6, size[1]*2/5-20))   # (0, 0)   (size[0]/6, size[1]*2/5)100号字中心位置

direction = 'right'    # 初始方向,向右

changeDirection = direction    # 定义一个改变方向的变量,按键

running = True

while Trunning:   

for event in pygame.event.get():    # 从队列中获取事件        

if event.type == QUIT:    # 判断事件类型是否为退出事件

# pygame.quit()

sys.exit()    

elif event.type == KEYDOWN:    # 如果按键被按下(事件)

if event.key == K_RIGHT or event.key == K_d: # 如果是右键头或者是d,蛇向右移动

changeDirection = 'right'            

if event.key == K_LEFT or event.key == K_a:  # 如果是左键头或者是a,蛇向左移动

changeDirection = 'left'

if event.key == K_UP or event.key == K_w:

changeDirection = 'up'

if event.key == K_DOWN or event.key == K_s:

changeDirection = 'down'

#这下面一行可以忽略,

if event.key == K_ESCAPE:    # 对应键盘上的Esc键,表示退出

pygame.event.post(pygame.event.Event(QUIT))

# 绘制文本,刷新显示

screen.blit(txt, (20, 10))

screen.fill(blackColor)  #放在for语句一列,属于while的(子)层级,循环不断的刷新

关于Python的问题,这段代码出了什么问题,我是个菜鸟。各位高手们帮我一下,谢谢

sekf.speed=speed

这一行的sekf应该是self

ball = Myballclass(img_file,location)

应该是 ball = Myballclass(img_file,location,speed)

下次提问时请不要只是把代码贴出来就完了,请把错误提示也贴出来。


新闻标题:flip函数python flip函数怎么用
文章源于:http://kswsj.com/article/dosopio.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号