使用pygame以方形方式移动图像

时间:2014-07-01 07:53:42

标签: python pygame pygame-surface

嘿伙计们我一直试图用pygame在屏幕上以方形方式移动球形图像。我已经尝试了很多次但它不起作用。请参阅下面的代码。

import time                                              
import pygame, sys
from pygame.locals import *
pygame.init()
clocks = pygame.time.Clock()  
surfaceObject = pygame.display.set_mode((640, 480))  
pygame.display.set_caption('Bounce')   

mousey,mousex = 0,0  
imgx = 10 
imgy = 10 
pixmove = 60
tiger = [2,2]
movement = 'down' 
background = pygame.image.load('bg.jpg').convert()
ball = pygame.image.load('ball.jpg').convert_alpha()
pygame.mixer.music.load('yeah.mp3')
while True: 
  time.sleep(1)                                        
if movement == 'down':                              
    imgx += pixmove                                  
if imgx < 640:                                      
    tiger[0] - tiger[1]                              

elif movement  == 'right':                          
    imgx += pixmove                                  
if imgx < 0:                                      
    movement = 'up'                                  


elif movement == 'up':                              
    imgy -= pixmove                                  
if imgy < 0:                                        
    movement = 'left'                                


elif movement == 'left':                            
    imgx -= pixmove                                  
if imgx < 0:                                        
    movement = 'down'                                


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

surfaceObject.blit(background,(mousex,mousey))      
surfaceObject.blit(ball,(imgx,imgy))                
pygame.mixer.music.play()
pygame.display.update()                              
clocks.tick(50)

当我运行此代码时,球会以直线方式移动,并且球在接触末端时不会反弹。

我的目标是在屏幕上以正方形旋转球。我试过更改pixmove变量,但它没有解决我的问题。

希望你们能帮助我..提前提前

3 个答案:

答案 0 :(得分:0)

你的缩进完全被打破了,所以我不知道会发生什么。

然而,以方形方式移动图像很容易。只需创建一个Rect对象应该在其中移动,一旦对象离开Rect,就改变方向。

看看下面的例子:

import pygame
from itertools import cycle

pygame.init() 

screen = pygame.display.set_mode((300, 300)) 
# move inside the screen (or any other Rect)
s_r = screen.get_rect()
player = pygame.Rect((100, 100, 50, 50))
timer = pygame.time.Clock()

speed = 5
up, down, left, right = (0, -speed), (0, speed), (-speed, 0), (speed, 0)
dirs = cycle([up, right, down, left])
dir = next(dirs)

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            raise

    # move player
    player.move_ip(dir)
    # if it's outside the screen
    if not s_r.contains(player):
        # put it back inside
        player.clamp_ip(s_r)
        # and switch to next direction
        dir = next(dirs)

    screen.fill(pygame.color.Color('Black'))
    pygame.draw.rect(screen, pygame.color.Color('Grey'), player)

    pygame.display.flip()
    timer.tick(25)

enter image description here

答案 1 :(得分:0)

由于我们确定你真正想做的是让球从边缘反弹,所以请看这里: Why isn't the ball bouncing back?

总之,需要做的是你需要跟踪球的运动方向(根据它的垂直和水平运动),并在你撞到墙壁时改变它们。

答案 2 :(得分:0)

您真的想要修复缩进,因为这会导致您的代码高度无法理解

如果你想以正方形形式移动某些东西,例如圆形,这很容易:

import pygame, sys
from pygame.locals import *
pygame.init()

DISPLAYSURF = pygame.display.set_mode((1200, 600))

x, y = (100, 100)
dir = 'right'

while True:
        for event in pygame.event.get():
                if event.type == QUIT:
                        pygame.quit()
                        sys.exit()
        DISPLAYSURF.fill((0, 0, 0))
        pygame.draw.circle(DISPLAYSURF, (255, 255, 255), (x, y), 10)
        if dir == 'right':
                x+=14
                if x >= 1100:
                        dir = 'down'
        elif dir == 'down':
                y+=14
                if y >= 500:
                        dir = 'left'
        elif dir == 'left':
                x-=14
                if x <= 100:
                        dir = 'up'
        elif dir == 'up':
                y-=14
                if y <= 100:
                        dir = 'right'
        pygame.display.flip()

enter image description here