python中如何移动图形_如何移动形状python

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-28 17:44   18   0

你的代码有几个问题。在

1)为了能够更改数组点,需要使用元组列表以外的其他数据结构,因为元组是不可变的。所以你可以有一个这样的列表:points=[[-60,20],[-60,-20],[-20,60],[20,60],[60,20],[60,-20],[20,-60],[-20,60]]

2)在这个循环中:

^{pr2}$

你会立即把它们的第20个元素和第60个元素的值抛出,因为它们都是第20个元素。我从上面的代码中了解到,你想用movex和movey移动每个点。为此,应将上述for循环更改为:for point in points:

point[0] += movex

point[1] += movey

您还需要将上面的for循环移到主循环中的关键事件下面,才能使其正常工作。在

3)在尝试访问movex和{}之前,您需要将它们初始化为某个东西,这样在主循环之外,您可以简单地如下初始化它们:movex = 0

movey = 0

while True:

#code

4)在这种状态下,您根本不需要x和y变量来移动八角形,因为您直接通过movex和movey变量递增每个点。在

经过以上所有更改,您的代码现在应该如下所示:bif="alien.png"

import pygame, sys

from pygame.locals import *

points=[[-60,20],[-60,-20],[-20,60],[20,60],[60,20],[60,-20],[20,-60],[-20,60]]

colour=(0,191,255)

pygame.init()

screen=pygame.display.set_mode((1280,800),0,32)

background=pygame.image.load(bif).convert()

movex = 0

movey = 0

while True:

for event in pygame.event.get():

if event.type == QUIT:

pygame.quit()

sys.exit()

if event.type==KEYDOWN:

if event.key==K_LEFT:

movex=-1

elif event.key==K_RIGHT:

movex=+1

elif event.key==K_UP:

movey=-1

elif event.key==K_DOWN:

movey=+1

if event.type==KEYUP:

if event.key==K_LEFT:

movex=0

elif event.key==K_RIGHT:

movex=0

elif event.key==K_UP:

movey=0

elif event.key==K_DOWN:

movey=0

for point in points:

point[0] += movex

point[1] += movey

screen.blit(background,(0,0))

pygame.draw.polygon(screen,colour,points)

pygame.display.update()

这应该很顺利:)

希望我的回答对你有帮助!

干杯

亚历克斯

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP