2011-12-09 176 views
1

所以我想简单地在pygame中移动一个物体。我一直在寻找教程,但我能找到的是如何让它看起来像下雪,哈哈。我一直在试图实现这个方法来移动一个对象,但我没有运气。我想要做的就是在屏幕上移动一个对象,当它到达屏幕的末尾时,它会重置并重新开始。所以我试图将我放在屏幕上的代码(两个多边形,线条和圆)放在水平或垂直方向上的对象并不重要。Pygame移动物体

import pygame, sys, time, random 
from pygame.locals import * 

pygame.init() 

windowSurface = pygame.display.set_mode((500, 400), 0, 32) 
pygame.display.set_caption("Paint") 

BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
GREEN = (0, 255, 0) 
BLUE = (0, 0, 255) 

windowSurface.fill(WHITE) 

pygame.draw.polygon(windowSurface,BLUE,((146, 0), (250, 100), (230, 265), (44, 250), (0,110))) 
pygame.draw.polygon(windowSurface,RED,((70, 0), (150, 200), (0, 50))) 
pygame.draw.line(windowSurface,BLACK,(60, 60), (120, 60), 8) 
pygame.draw.circle(windowSurface, GREEN , (150,150), 15, 0) 


pygame.display.update() 

while True: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
+0

也许这可能有所帮助:http://stackoverflow.com/questions/8252860/pygame-move-object-position-from-one-point-to-another-at-constant-speed/8253040#8253040 – sinan

回答

3

用你的方法,你不能。使用pygame的想法是绘制你想绘制每一帧的所有对象。您必须先在while True循环内移动图纸。 然后,因为你的一切绘制每一帧,您可以:

  • 用于存储对象
  • 检查的位置和方向创建对象/变量如果对象到达屏幕的一个边界
  • 使用新的位置在最后绘制多边形的

所以,你可以有类似的东西(这是你的任务是改变一个对象)

# ... pygame and app initialization 

# get screen size 
info = pygame.display.Info() 
sw = info.current_w 
sh = info.current_h 

# initial position 
x = y = 0 
# initial direction 
dx = 5 
dy = 2 

while True: 

    # update position with direction 
    x += dx 
    y += dy 

    # check bounds 
    if x - dx < 0 or x + dx > sw: 
     dx = -dx 
    if y - dy < 0 or y + dy > sh: 
     dy = -dy 

    # then draw and use x/y in your drawing instructions! 
    # ... pygame events ... 
1

为了做你正在尝试做一个开始,你将不得不随着“while True:”循环而改变。这里是一个代码,将做你正在做的事情的一个例子:

import pygame, sys, pygame.locals#1 
pygame.init()#2 
window=pygame.display.set_mode((500, 400), 0, 32)#3 
pygame.display.set_caption("Paint")#4 
BLACK = (0, 0, 0)#5 
WHITE = (255, 255, 255)#6 
RED = (255, 0, 0)#7 
GREEN = (0, 255, 0)#8 
BLUE = (0, 0, 255)#9 
pentagon=pygame.Surface((250, 265))#10 
pentagon.fill((0, 0, 0))#11 
pygame.draw.polygon(pentagon, BLUE, ((146, 0), (250, 100), (230, 265), (44, 250), (0,110)))#12 
pentagon.set_colorkey((0, 0, 0))#13 
triangle=pygame.Surface((150, 200))#14 
triangle.fill((0, 0, 0))#15 
pygame.draw.polygon(triangle, RED, ((70, 0), (150, 200), (0, 50)))#16 
triangle.set_colorkey((0, 0, 0))#17 
line=pygame.Surface((60, 8))#18 
line.fill(BLACK)#19 
circle=pygame.Surface((30, 30))#20 
circle.fill((0, 0, 0))#21 
pygame.draw.circle(circle, GREEN , (15, 15), 15, 0)#22 
circle.set_colorkey((0, 0, 0))#23 
rects={'pentagon': pentagon.get_rect(), 'triangle': triangle.get_rect(), 'line': line.get_rect(), 'circle': circle.get_rect()}#24 
rects['line'].centery=60#25 
rects['line'].left=60#26 
rects['circle'].centerx=150#27 
rects['circle'].centery=150#28 
while True:#29 
    for event in pygame.event.get():#30 
     if event.type==pygame.locals.QUIT:#31 
      pygame.quit()#32 
      sys.exit()#33 
    for rect in rects:#34 
     rects[rect].right+=1#35 
     if rects[rect].right>500:#36 
      if rect=='line':#37 
       rects['line'].centery=60#38 
       rects['line'].left=60#39 
      elif rect=='circle':#40 
       rects['circle'].centerx=150#41 
       rects['circle'].centery=150#42 
      else:#43 
       rects[rect].topleft=(0, 0)#44 
    window.fill(WHITE)#45 
    window.blit(pentagon, rects['pentagon'])#46 
    window.blit(triangle, rects['triangle'])#47 
    window.blit(line, rects['line'])#48 
    window.blit(circle, rects['circle'])#49 
    pygame.time.Clock().tick(40)#50 
    pygame.display.update()#51 

我会尽我所能,以尽可能解释这个代码。

1-9行,你已经知道了。

第10行从您可能不知道的事情开始。表面物体是一种矩形图片,可以附加到任何其他表面,或者使用pygame.draw绘制。相信与否,窗户实际上是一个表面。

第10行创建一个表面,第11行用黑色填充,第12行在其上绘制五边形。

第13行基本上使所有黑色透明的像素。

14-17行,你现在应该明白了。

第18行为该线条创建了一个新的曲面对象,但不是在其上绘制线条,而是用黑色填充并单独放置。这是因为,如果你看看你的旧程序中的行,你可以看到它实际上只是一个矩形,没有别的。

20-23行应该理解。

24行制作矩形字典。矩形显示表面对象的位置,并且为了将它们绘制到任何东西上都是必需的。

25-28行更改线条和圆的位置。我这样做的原因是因为我认为你可能会喜欢它,因为你的程序中没有左上角的圆和线。

29-33行你应该明白。

第34行启动了一个for循环,它将遍历所有rects。

35行将每个矩形的右侧向右移动一个像素。

第36行检查右边的右边是否已经到达窗口的右边缘。

第37行检查是否碰到一边的线是线,如果是,则第38-39行将其移动到您拥有它的位置。

第40行检查是否碰到一边的是圆形,如果是,则第41-42行将其移动到您拥有它的位置。

第43-44行将矩形放在左上角,其他两个形状开始。

你应该知道第45行。

第46-49行使用了surface.blit()函数。这个函数的第一个参数是一个曲面,第二个参数是一个曲面。

50行不允许超过40帧通过秒。

51行更新屏幕。

我真的希望这会有所帮助,如果您投了票或接受了我的回答,考虑我写了多长时间,我将不胜感激。如果您有任何疑问,请询问。谢谢!

0

这并不难。首先,我们将在可以解决你的问题移动你的对象(S):

import pygame, sys, time, random 
from pygame.locals import * 

pygame.init() 

windowSurface = pygame.display.set_mode((500, 400), 0, 32) 
pygame.display.set_caption("Paint") 

BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
GREEN = (0, 255, 0) 
BLUE = (0, 0, 255) 

windowSurface.fill(WHITE) 

x, y = 250, 200 

while True: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 

    pressed = pygame.key.get_pressed() 
    if pressed[K_UP]: y - 5 
    if pressed[K_DOWN]: y + 5 
    if pressed[K_LEFT]: x - 5 
    if pressed[K_RIGHT]: x + 5 

    pygame.display.update() 

pygame.draw.polygon(windowSurface,BLUE,((x - 104, y - 200), (x, y - 100), (x - 20, y + 65), (x - 206, y + 50), (x - 250, y - 90))) 
pygame.draw.polygon(windowSurface,RED,((x - 180, y - 200), (x - 100, y), (x - 250, y - 150))) 
pygame.draw.line(windowSurface,BLACK,(x - 190, y - 140), (x - 130, y - 140), 8) 
pygame.draw.circle(windowSurface, GREEN , (x - 100, y - 50), 15, 0) 

在上面的代码行,所有的坐标转换为X,Y变量的形式。当按下箭头键时,变量会发生变化,从而改变形状的坐标和位置。