马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 苏丛 于 2018-2-28 19:28 编辑
小甲鱼演示的有bug的代码是这样的import pygame
import sys
from pygame.locals import*
#初始化Pygame
pygame.init()
size = width, height = 600, 400
speed = [-2, 3]
bg = (180, 90, 200)
screen = pygame.display.set_mode(size, RESIZABLE)
person = pygame.image.load('1.png')
position = person.get_rect()
l_head = person
r_head = pygame.transform.flip(person, True, False)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
person = l_head
speed = [-1, 0]
elif event.key == K_RIGHT:
person = r_head
speed = [1, 0]
elif event.key == K_UP:
speed = [0, -1]
elif event.key == K_DOWN:
speed = [0, 1]
#用户调整窗口尺寸
if event.type == VIDEORESIZE:
size = event.size
width, height = size
print(size)
#重新画屏幕
screen = pygame.display.set_mode(size, RESIZABLE)
#移动图像
position = position.move(speed)
if position.left < 0 or position.right > width:
person = pygame.transform.flip(person, True, False)
speed[0] = -speed[0]
if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]
#填充背景
screen.fill(bg)
#更新图像
screen.blit(person, position)
#更新界面
pygame.display.flip()
#延迟10毫秒
pygame.time.delay(10)
bug就是把界面边框拖拽到图片上时图片会在边框上抽搐
小甲鱼说课后作业有修复后的代码,但貌似没有课后作业了。
我想到的解决方法是直接改位置
代码如下import pygame
import sys
from pygame.locals import*
#初始化Pygame
pygame.init()
size = width, height = 600, 400
speed = [-2, 3]
bg = (180, 90, 200)
screen = pygame.display.set_mode(size, RESIZABLE)
person = pygame.image.load('1.png')
position = person.get_rect()
l_head = person
r_head = pygame.transform.flip(person, True, False)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
person = l_head
speed = [-1, 0]
elif event.key == K_RIGHT:
person = r_head
speed = [1, 0]
elif event.key == K_UP:
speed = [0, -1]
elif event.key == K_DOWN:
speed = [0, 1]
#用户调整窗口尺寸
if event.type == VIDEORESIZE:
size = event.size
width, height = size
print(size)
#重新画屏幕
screen = pygame.display.set_mode(size, RESIZABLE)
#移动图像
position = position.move(speed)
if position.left < 0 or position.right == width + 2:
person = pygame.transform.flip(person, True, False)
speed[0] = -speed[0]
if position.top < 0 or position.bottom == height + 3:
speed[1] = -speed[1]
#直接修改位置
if position.right > width + 2:
position[0] -= position.right - size[0]
if position.bottom > height + 3:
position[1] -= position.bottom - size[1]
#填充背景
screen.fill(bg)
#更新图像
screen.blit(person, position)
#更新界面
pygame.display.flip()
#延迟10毫秒
pygame.time.delay(10)
求指点
有没有更好的解法
谢谢
你的思路直接改變位置的方法是正確的
以下是我自己的代碼給你參考
改變窗口大小後邊界顫抖解決方法有兩步驟
1. 把改變後的窗口大小(width, height)重新定義給size
if event.key == K_s and pygame.key.get_mods() & KMOD_LCTRL: #縮小
if i < len(displaylist)-1:
i += 1
size = width, height = displaylist[i]
screen = pygame.display.set_mode(size)
if event.key == K_b and pygame.key.get_mods() & KMOD_LCTRL: #放大
if i > 0:
i -= 1
size = width, height =displaylist[i]
screen = pygame.display.set_mode(size)
2.烏龜位置改變根據窗口邊界(width, height)大小,超過就是=窗口邊界
這樣就不會被卡住了
if position.left < 0:
position.left = 0
speed[0] = -speed[0]
if position.right > width:
position.right = width
speed[0] = -speed[0]
if position.top < 0:
position.top = 0
speed[1] = -speed[1]
if position.bottom > height:
position.bottom = height
speed[1] = -speed[1]
|