萌新求解:一个pygame的问题
import pygameimport sys
import random
#from pygame.locals import *(这个模块包含了 Pygame 定义的各种常量(常量由大写和下划线组成的))
#初始化pygame
pygame.init()
size = width,height = 600,400
speed = [-3,1]
bg = (255,255,255)
#创建窗口指定的大小
screen = pygame.display.set_mode(size)
#标题
pygame.display.set_caption("案例")
#加载图片
picture = pygame.image.load(r"C:\Users\admin\Pictures\picture.png")
#获取图像位置矩形
position = picture.get_rect()
fullscreen = False
right_head = picture
left_head = pygame.transform.flip(picture,True,False)
up_head = pygame.transform.flip(picture,270,False)
down_head = pygame.transform.flip(picture,False,270)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
picture = left_head
speed =
if event.key == pygame.K_LEFT:
picture = right_head
speed = [-5,0]
if event.key == pygame.K_UP:
picture = up_head
speed =
if event.key == pygame.K_DOWN:
picture = down_head
speed =
#全屏
if event.key == pygame.K_F11:
fullscreen = not fullscreen
if fullscreen:
size = width,height =1920,1080
screen = pygame.display.set_mode((1920,1080),pygame.FULLSCREEN|pygame.HWSURFACE)
if position.bottom > height:
positon.bottom = height
if position.right > width:
position.right = width
else:
#full screen = pygame.display.list_modes()
size = width,height = 600,400
screen = pygame.display.set_mode(size)
screen.blit(picture,(random.randint(0,width),(random.randint(0,height))))
#移动图像
position = position.move(speed)
if position.left < 0 or position.right > width:
#翻转图像
picture = pygame.transform.flip(picture,True,False)#水平翻转(垂直翻转为False)
speed = -speed
if position.top < 0 or position.bottom > height:
speed = -speed
#填充背景
screen.fill(bg)
#更新图像
screen.blit(picture,position)
#更新界面
pygame.display.flip()
#延时10毫秒
pygame.time.delay(10)
else 中当重新按下F11时 怎么把图像重置在退出全屏后的原屏幕里 求大佬求大佬求大佬{:10_266:} 嗯 ,,在第50行加上#更新图像
screen.blit(picture,position)试试{:10_245:} 小伤口 发表于 2021-4-22 09:43
嗯 ,,在第50行加上#更新图像
screen.blit(picture,position)试试
不行诶{:10_255:} 小伤口 发表于 2021-4-22 09:43
嗯 ,,在第50行加上#更新图像
screen.blit(picture,position)试试
import pygame
import sys
import random
#from pygame.locals import *(这个模块包含了 Pygame 定义的各种常量(常量由大写和下划线组成的))
#初始化pygame
pygame.init()
size = width,height = 600,400
speed = [-3,1]
bg = (255,255,255)
#创建窗口指定的大小
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
#标题
pygame.display.set_caption("案例")
#加载图片
picture = pygame.image.load(r"C:\Users\admin\Pictures\picture.png")
#获取图像位置矩形
position = picture.get_rect()
fullscreen = False
right_head = picture
left_head = pygame.transform.flip(picture,True,False)
up_head = pygame.transform.flip(picture,270,False)
down_head = pygame.transform.flip(picture,False,270)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
picture = left_head
speed =
if event.key == pygame.K_LEFT:
picture = right_head
speed = [-5,0]
if event.key == pygame.K_UP:
picture = up_head
speed =
if event.key == pygame.K_DOWN:
picture = down_head
speed =
#全屏
if event.key == pygame.K_F11:
fullscreen = not fullscreen
if fullscreen:
size = width,height =1920,1080
screen = pygame.display.set_mode((1920,1080),pygame.FULLSCREEN|pygame.HWSURFACE)
if position.bottom > height:
positon.bottom = height
if position.right > width:
position.right = width
else:
if position.right > width:
picture = pygame.transform.flip(picture,True,False)
speed = -speed
if position.bottom > height:
speed = -speed
else:
size = width,height = 600,400
screen = pygame.display.set_mode(size)
if picture.get_width() < width and picture.get_height() < height:
picture = picture
#如果图片的超过了原屏幕执行
elif position not in size:
x = random.randint(0,width)
y = random.randint(0,height)
screen.blit(picture,(x,y))
#用户调整窗口尺寸
if event.type == pygame.VIDEORESIZE:
size = event.size
width,height = size
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
#移动图像
position = position.move(speed)
if position.left < 0 or position.right > width:
#翻转图像
picture = pygame.transform.flip(picture,True,False)#水平翻转(垂直翻转为False)
speed = -speed
if position.top < 0 or position.bottom > height:
speed = -speed
#填充背景
screen.fill(bg)
#更新图像
screen.blit(picture,position)
#更新界面
pygame.display.flip()
#延时10毫秒
pygame.time.delay(10)
不知道为什么 图片超过原屏幕那总是执行不了 else和elif、if也是 import pygame
import sys
import random
#from pygame.locals import *(这个模块包含了 Pygame 定义的各种常量(常量由大写和下划线组成的))
#初始化pygame
pygame.init()
size = width,height = 600,400
speed = [-3,1]
bg = (255,255,255)
#创建窗口指定的大小
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
#标题
pygame.display.set_caption("案例")
#加载图片
picture = pygame.image.load('C:/Users/ASUS/Desktop/1-14031F05541a7.png')
#获取图像位置矩形
position = picture.get_rect()
fullscreen = False
right_head = picture
left_head = pygame.transform.flip(picture,True,False)
up_head = pygame.transform.flip(picture,270,False)
down_head = pygame.transform.flip(picture,False,270)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
picture = left_head
speed =
if event.key == pygame.K_LEFT:
picture = right_head
speed = [-5,0]
if event.key == pygame.K_UP:
picture = up_head
speed =
if event.key == pygame.K_DOWN:
picture = down_head
speed =
#全屏
if event.key == pygame.K_F11:
fullscreen = not fullscreen
if fullscreen:
size = width,height =1920,1080
screen = pygame.display.set_mode((1920,1080),pygame.FULLSCREEN|pygame.HWSURFACE)
if position.bottom > height:
positon.bottom = height
if position.right > width:
position.right = width
else:
size = width,height = 600,400
screen = pygame.display.set_mode(size)
if picture.get_width() < width and picture.get_height() < height:
picture = picture
position=picture.get_rect()
#填充背景
screen.fill(bg)
screen.blit(picture,position)#将一个图形画在另一个图形上(只是修改另一个图像的像素并不是真的画在上面)
#更新界面
pygame.display.flip()
pygame.time.delay(10)
#用户调整窗口尺寸
if event.type == pygame.VIDEORESIZE:
size = event.size
width,height = size
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
#移动图像
position = position.move(speed)
if position.left < 0 or position.right > width:
#翻转图像
picture = pygame.transform.flip(picture,True,False)#水平翻转(垂直翻转为False)
speed = -speed
if position.top < 0 or position.bottom > height:
speed = -speed
#填充背景
screen.fill(bg)
#更新图像
screen.blit(picture,position)
#更新界面
pygame.display.flip()
#延时10毫秒
pygame.time.delay(10)
试试这样
记得把路径改下哦~ 小伤口 发表于 2021-4-22 23:10
试试这样
记得把路径改下哦~
哦哦 可以了 {:10_323:}
能再问一下嘛 在拖延窗口/屏幕大小的时候,在窗口/屏幕重叠图像时,图像的循环颤抖怎么解决呀,哇!!这个bug真的不会解决呀,这个bug时检测两者之间的碰撞,可我知道的碰撞的只有rect和精灵呀,没有surface的{:10_292:}
蓝受{:10_263:}
wuyanzulqq 发表于 2021-4-24 22:37
哦哦 可以了
能再问一下嘛 在拖延窗口/屏幕大小的时候,在窗口/屏幕重叠图像时,图像的循环 ...
我一般写游戏都不让用户改大小,就是为了防止这样的问题{:10_250:},所以俺也没解决掉这个{:10_266:} 小伤口 发表于 2021-4-25 00:15
我一般写游戏都不让用户改大小,就是为了防止这样的问题,所以俺也没解决掉这个
看弹幕说可以用 “每次改变都按比例重置乌龟的位置” 不知道行不行,{:10_257:} wuyanzulqq 发表于 2021-4-25 11:40
看弹幕说可以用 “每次改变都按比例重置乌龟的位置” 不知道行不行,
这个可以解决鬼畜问题,但俺觉得运用到实际就不是很可行,不可能角色走了一段路 用户改一下窗口大小,角色又回到原地吧{:10_254:} wuyanzulqq 发表于 2021-4-25 22:10
????? 小伤口 发表于 2021-4-25 23:22
?????
import pygame
import sys
import random
from pygame.locals import *#(这个模块包含了 Pygame 定义的各种常量(常量由大写和下划线组成的))
#初始化pygame
pygame.init()
size = width,height = 600,400
speed = [-3,1]
bg = (255,255,255)
#创建窗口指定的大小
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
#标题
pygame.display.set_caption("案例")
#加载图片
opicture = pygame.image.load(r"C:\Users\admin\Pictures\picture.png")
picture = opicture
#获取图像位置矩形
opicture_rect = opicture.get_rect()
position = picture_rect = opicture_rect
#设置放大缩小的比率
ratio = 1.0
fullscreen = False
right_head = picture
left_head = pygame.transform.flip(picture,True,False)
up_head = pygame.transform.flip(picture,True,False)
down_head = pygame.transform.flip(picture,False,True)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
picture = left_head
speed =
if event.key == pygame.K_LEFT:
picture = right_head
speed = [-5,0]
if event.key == pygame.K_UP:
picture = up_head
speed =
if event.key == pygame.K_DOWN:
picture = down_head
speed =
#全屏
if event.key == pygame.K_F11:
fullscreen = not fullscreen
if fullscreen:
size = width,height =1920,1080
screen = pygame.display.set_mode((1920,1080),pygame.FULLSCREEN|pygame.HWSURFACE)
if position.bottom > height:
positon.bottom = height
if position.right > width:
position.right = width
else:
if position.right > width:
picture = pygame.transform.flip(picture,True,False)
speed = -speed
if position.bottom > height:
speed = -speed
else:
size = width,height = 600,400
screen = pygame.display.set_mode(size)
if picture.get_width() < width and picture.get_height() < height:
picture = picture
position=picture.get_rect()
#填充背景
screen.fill(bg)
screen.blit(picture,position)#将一个图形画在另一个图形上(只是修改另一个图像的像素并不是真的画在上面)
#更新界面
pygame.display.flip()
pygame.time.delay(10)
#放大、缩小图像(+、-),空格恢复原始尺寸
if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
#最大放大一倍、缩小一倍
if event.key == K_EQUALS and ratio < 2:
ratio += 0.1
if event.key == K_MINUS and ratio > 0.5:
ratio -= 0.1
if event.key == K_SPACE:
ratio = 1.0
picture = pygame.transform.smoothscale(opicture,(int(opicture_rect.width * ratio),\
int(opicture_rect.height * ratio)))
#用户调整窗口尺寸
if event.type == VIDEORESIZE and not fullscreen:
size = event.size
width,height = size
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
rect = pygame.Rect(position)
if rect.top > height or rect.left > width:
position=picture.get_rect()
#填充背景
screen.fill(bg)
screen.blit(picture,position)
pygame.display.flip()
pygame.time.delay(10)
#移动图像
position = position.move(speed)
if position.left < 0 or position.right > width:
#翻转图像
picture = pygame.transform.flip(picture,True,False)#水平翻转(垂直翻转为False)
speed = -speed
if position.top < 0 or position.bottom > height:
speed = -speed
#填充背景
screen.fill(bg)
#更新图像
screen.blit(picture,position)
#更新界面
pygame.display.flip()
#延时10毫秒
pygame.time.delay(10)
多加了放大和缩小的键,又有新bug了{:10_266:},在放大图像的时候:图像一边移动一边放大,放大的图像会移出右下,我知道放大的是像素,position还是原来的position,然后我用放大后的尺寸,放在blit的第一个参数里,再赋给opicture,可是为什么有循环颤抖呀(我想法错了嘛{:9_229:}) 小伤口 发表于 2021-4-25 23:22
?????
我好low啊{:10_266:} wuyanzulqq 发表于 2021-4-25 23:43
多加了放大和缩小的键,又有新bug了,在放大图像的时候:图像一边移动一边放大,放大的图像 ...
对不起哈,电脑没在身边{:10_245:},俺也对pygame放大缩小之类的不清楚。实在不行,就先跳过吧{:10_256:} wuyanzulqq 发表于 2021-4-25 23:48
我好low啊
不low,不low,期待你的游戏哦 小伤口 发表于 2021-4-26 09:24
对不起哈,电脑没在身边,俺也对pygame放大缩小之类的不清楚。实在不行,就先跳过吧{:10_256 ...
import pygame
import sys
import random
from pygame.locals import *#(这个模块包含了 Pygame 定义的各种常量(常量由大写和下划线组成的))
#初始化pygame
pygame.init()
size = width,height = 600,400
speed = [-3,1]
bg = (255,255,255)
#创建窗口指定的大小
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
#标题
pygame.display.set_caption("案例")
#加载图片
opicture = pygame.image.load(r"C:\Users\admin\Pictures\picture.png")
picture = opicture
#获取图像位置矩形
opicture_rect = opicture.get_rect()
position = picture_rect = opicture_rect
#设置放大缩小的比率
ratio = 1.0
fullscreen = False
# 方向, 1右0左
direction=1
right_head = picture
left_head = pygame.transform.flip(picture,True,False)
up_head = pygame.transform.flip(picture,True,False)
down_head = pygame.transform.flip(picture,False,True)
while True:
for event in pygame.event.get():
#纠正放大、缩小后图片的位置
t = pygame.display.get_surface().get_rect()
limit = t.width, t.height
p = position
if p.left < 0:
p.left = 0
speed *= -1
picture= pygame.transform.flip(picture ,True,False)
direction = 0
elif p.right > limit:
p.right = limit
speed *= -1
picture= pygame.transform.flip(picture ,True, False)
direction = 1
elif p.top < 0:
p.top = 0
speed *= -1
picture= pygame.transform.flip(picture ,False, True)
elif p.bottom > limit:
p.bottom = limit
speed *= -1
picture= pygame.transform.flip(picture ,False, True)
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
picture = left_head
speed =
if event.key == pygame.K_LEFT:
picture = right_head
speed = [-5,0]
if event.key == pygame.K_UP:
picture = up_head
speed =
if event.key == pygame.K_DOWN:
picture = down_head
speed =
#全屏
if event.key == pygame.K_F11:
fullscreen = not fullscreen
if fullscreen:
size = width,height =1920,1080
screen = pygame.display.set_mode((1920,1080),pygame.FULLSCREEN|pygame.HWSURFACE)
if position.bottom > height:
positon.bottom = height
if position.right > width:
position.right = width
else:
if position.right > width:
picture = pygame.transform.flip(picture,True,False)
speed = -speed
if position.bottom > height:
speed = -speed
else:
size = width,height = 600,400
screen = pygame.display.set_mode(size)
if picture.get_width() < width and picture.get_height() < height:
picture = picture
position=picture.get_rect()
#填充背景
screen.fill(bg)
screen.blit(picture,position)#将一个图形画在另一个图形上(只是修改另一个图像的像素并不是真的画在上面)
#更新界面
pygame.display.flip()
pygame.time.delay(10)
if event.key in (K_EQUALS,K_MINUS,K_SPACE):
if event.key == K_EQUALS and ratio < 2:
ratio += 0.1
if event.key == K_MINUS and ratio > 0.5:
ratio -= 0.1
if event.key == K_SPACE:
ratio = 1.0
op = opicture_rect
picture = pygame.transform.smoothscale(opicture,(int(op.width*ratio),int(op.height*ratio)))
if direction == 0:
picture = pygame.transform.flip(picture,True,False)
p = picture.get_rect()
position.width,position.height = p.width,p.height
#用户调整窗口尺寸
if event.type == VIDEORESIZE and not fullscreen:
size = event.size
width,height = size
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
rect = pygame.Rect(position)
if rect.top > height or rect.left > width:
position=picture.get_rect()
#填充背景
screen.fill(bg)
screen.blit(picture,position)
pygame.display.flip()
pygame.time.delay(10)
#移动图像
position = position.move(speed)
if position.left < 0 or position.right > width:
#翻转图像
picture = pygame.transform.flip(picture,True,False)#水平翻转(垂直翻转为False)
speed = -speed
if position.top < 0 or position.bottom > height:
speed = -speed
#填充背景
screen.fill(bg)
#更新图像
screen.blit(picture,position)
#更新界面
pygame.display.flip()
#延时10毫秒
pygame.time.delay(10)
图像会移出界面的bug改好了,新的bug又来了{:10_266:} {:10_293:}
每次放大、缩小 图像一点也不“老实” 小伤口 发表于 2021-4-26 09:26
不low,不low,期待你的游戏哦
{:10_287:}
页:
[1]