Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python上移动创建的对象?_Python_String_Functional Programming_Pygame_Shapes - Fatal编程技术网

如何在Python上移动创建的对象?

如何在Python上移动创建的对象?,python,string,functional-programming,pygame,shapes,Python,String,Functional Programming,Pygame,Shapes,我使用Python创建了一个八角形: bif="hello.jpg" import pygame, sys from pygame.locals import * points=[(-60,20),(-60,-20),(-20,60),(20... colour=(0,191,255) pygame.init() screen=pygame.display.set_mode((1000,10... background=pygame.image.load(bif).conv

我使用Python创建了一个八角形:

bif="hello.jpg" 

import pygame, sys 
from pygame.locals import * 

points=[(-60,20),(-60,-20),(-20,60),(20... 
colour=(0,191,255) 

pygame.init() 

screen=pygame.display.set_mode((1000,10... 
background=pygame.image.load(bif).conve... 


x,y=0,0 
movex,movey=0,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 

        x+=movex 
        y+=movey 

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

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

    pygame.display.update() 
我是Python的新用户。我用Python创建了一个八角形,但它不会移动

我怎么移动它? 我需要什么编码? 我将如何移动我创建的这个八角形? 我做错什么了吗?我做错了什么? 我该如何改进这一点?
您已经为八角形设置了点,但是看起来它们根本没有被movex和movey变量改变

举个简单的例子,如果您有一组正方形的点,例如:

mysquare = [[10, 10], [20, 10], [20, 20], [10, 20]] #I believe you can use lists and tuples, but in case NOT, switch the nested lists for tuples
…您需要根据movex变量简要更改x值。例如

for point in mysquare:
    point[0] += movex
    point[1] += movey
如果使用元组,则必须替换整个元组,而不是在给定索引处递增值

for pt in mysquare:
    pt = (pt[0] + movex, pt[1] + movey)
-编辑-

举个例子,让我们看看这在while循环中的位置


我相信这会给你想要的效果,虽然我还没有测试过。但这个想法是合理的:找出如何移动这些点,然后再移动它们。

您的代码中有许多重要数据所在的省略号。你能填一下吗?为什么这个问题用函数式编程来标记?什么是省略号?对不起,我是一个新用户,我真的不知道很多。它真的不应该被标记在那里。上面说我至少需要5个标签,但我不知道还有什么标签。对不起,我对这个很陌生。如果你是我,你会放什么?你能复制我的代码然后把你的代码放进去吗?谢谢你。
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 

    ##at this point, once you know HOW to move the octagon,
    ##now you need to APPLY the changes to the octagon
    for pt in your_octagon: ##or whatever you end up calling it
        pt = (pt[0] + movex, pt[1] + movey)

screen.blit(background,(0,0)) 
pygame.draw.polygon(screen,colour,points... 
pygame.display.update()