2016-09-29 67 views
-1

我需要帮助我的动画代码。到目前为止,我已经在球的三个边缘周围展示了球。但我不知道如何让它在最后一个屏幕上出现。如何让球在屏幕边缘连续走动? (Python 2.7)

#------------------------------------------------------------------------------- 
# Name:  U1A4.py 
# Purpose:  To animate the ball going around the edge of the screen 
#------------------------------------------------------------------------------- 

import pygame 
import sys 
pygame.init() 

# Screen 
screenSize = (800,600) 
displayScreen = pygame.display.set_mode(screenSize,0) 
pygame.display.set_caption("Animation Assignment 1") 

# Colours 
WHITE = (255,255,255) 
GREEN = (0,255,0) 
RED = (255,0,0) 
BLUE = (0,0,255) 

displayScreen.fill(WHITE) 
pygame.display.update() 

# ----------------- Leave animation code here ---------------------------------# 

# THU/09/29 
# Need to complete the last turn with the ball 

x = 50 
y = 50 
dx = 0 
dy = 2 
stop = False 
while not stop: 
    for event in pygame.event.get(): 
     if event.type ==pygame.QUIT: 
      stop = True 

    displayScreen.fill(WHITE) 

    x = x + dx 
    y = y + dy 

    if (x>=750): 
      dx = 0 
      dy = -2 

    if (y>=550)and dy>0: 
      dy = 0 
      dx = 2 

    if (x>=750)and dy>0: 
      dy = 0 
      dx = 2 

    if (y>=550)and dy>0: 
      dx = 0 
      dy = -2 


    pygame.draw.circle(displayScreen, GREEN, (x,y),50, 0) 
    pygame.display.update() 

pygame.quit() 
sys.exit() 

球需要绕屏幕边缘不断,任何提示或直接的答案是受欢迎的。谢谢。

+0

什么是你的代码错误? –

回答

0

球仅改变方向的角落,所以你只需要支付4箱子(二if的嵌套两if的):

x += dx 
y += dy 

if x >= 750: 
    if y >= 550: 
    dx = 0 
    dy = -2 
    elif y <= 50: 
    dx = -2 
    dy = 0 
elif x <= 50: 
    if y >= 550: 
    dx = 2 
    dy = 0 
    elif y <= 50: 
    dx = 0 
    dy = 2 
1

这是我在你的问题刺伤:

import sys, pygame 
pygame.init() 
size = width, height = 800, 800 
speed = [1, 0] 
black = 0, 0, 0 
screen = pygame.display.set_mode(size) 
ball = pygame.image.load("ball.bmp") 
ballrect = ball.get_rect() 
while 1: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: sys.exit() 
    ballrect = ballrect.move(speed) 
    if ballrect.right > width: 
     speed = [0, 1] 
    if ballrect.left < 0: 
     speed = [0, -1] 
    if (ballrect.bottom > height) and not (ballrect.left < 0): 
     speed = [-1,0] 
    if (ballrect.top < 0) and not (ballrect.right > width): 
     speed = [1, 0] 
    screen.fill(black) 
    screen.blit(ball, ballrect) 
    pygame.display.flip() 

让我感觉有点不舒服。

编辑 - 用它进行ball.bmp:

http://everitas.rmcclub.ca/wp-content/uploads/2007/11/soccer_ball_1.bmp