2014-07-12 1 views
-3

저는 'Absolute Beginner를위한 더 많은 파이썬 프로그래밍'이라는 책을 읽고 있습니다. 물론 27 페이지에 코드를 작성하려고합니다. (애니메이션 사각형) 물론 개선이 필요합니다.파이 게임 - 왜 내 모양이 움직이지 않습니까?

저는 pygame에게 모양의 x 값을 변경한다고 분명하게 말하고 있지만 움직이지는 않습니다. 나는 모양이 화면을 가로 질러 수평으로 움직이고 다른 끝에 도달 할 때 돌아 서고 그 반대도 마찬가지입니다. 여기

는 코드 : 문제가있는 곳입니다

# Animated Rectangle 
# 
# Demonstrating the basics of ANIMATION!!! 
# 
# --------------------------------------------------- 

import pygame, sys 
from pygame.locals import * 

WHITE = 255, 255, 255 
GREEN = 0, 255, 255 

# rectangle properties 
COLOUR = GREEN 
POS_X = 400 # position 
POS_Y = 200 
THICKNESS = 0 

VELOCITY = 1 

pygame.init() 

SURFACE = pygame.display.set_mode((600, 500)) 
SURFACE.fill(WHITE) 
pygame.display.set_caption("Animationing") 

if POS_X > 400 or POS_X < 100: 
    VELOCITY = -VELOCITY 

def draw(POS_X, VELOCITY): 
    pygame.draw.rect(SURFACE, COLOUR, (POS_X, POS_Y, 100, 100), THICKNESS) 
    POS_X -= VELOCITY # HERE IS THE SOURCE OF MY PROBLEM 
    pygame.display.update() 

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

    draw(POS_X, VELOCITY) 

;

def draw(POS_X, VELOCITY): 
    pygame.draw.rect(SURFACE, COLOUR, (POS_X, POS_Y, 100, 100), THICKNESS) 
    POS_X -= VELOCITY # HERE IS THE SOURCE OF MY PROBLEM 
    pygame.display.update() 

편집 : 일부 사람이 내 문법을 수정하려고했습니다. 나는 이것을 좋아한다, 적어도 당신은 그것을 이해할 수있다.

편집 2 : 내 질문은 주제를 벗어나기 위해 잠시 대기 했으므로 수정하려고했습니다. 당신이이 지역 변수 POS_X

def draw(POS_X, VELOCITY): 

을 만들 전역 변수와 지역 변수 POS_X

에 같은 이름을 사용하고

사용 global이 함수 내에서 POS_X 지역 변경 때문에이 문제가

+0

'어떤 사람이, 내가 이렇게 좋아 내 문법 문제를 해결하려고, 적어도 당신이 it.'을 이해할 수처럼 보일 수 있습니다 - 어쩌면 당신이 그것을 좋아하지만 우리는 당신을 이해할 수 없을 것입니다. – furas

+0

"우리는 당신을 이해할 수 없습니다" – PylonBuffering

답변

1

글로벌 POS_X에 액세스하려면

def draw(VELOCITY): 
    global POS_X 

한 다음 그것을 BTW

draw(VELOCITY) 

를 호출 만 색상 WHITE, GREEN 같은 상수 값 대문자의 변수를 사용 규칙 (PEP8)이있다.


편집 :

코드는이

import pygame, sys 
from pygame.locals import * 


#--------------------- 
# constants 

WHITE = (255, 255, 255) 
GREEN = ( 0, 255, 255) 

#--------------------- 
# rectangle properties 

color = GREEN 
pos_x = 400 # position 
pos_y = 200 
thickness = 0 

velocity = 1 

#--------------------- 
# classes and functions 

def move_player(): 
    global pos_x, velocity 

    pos_x -= velocity 
    if pos_x > 400 or pos_x < 100: 
     velocity = -velocity 

def draw_player(): 
    pygame.draw.rect(screen, color, (pos_x, pos_y, 100, 100), thickness) 

#--------------------- 
# GAME 

pygame.init() 

screen = pygame.display.set_mode((600, 500)) 
pygame.display.set_caption("Animationing") 

# mainloop 

running = True 

while running: 

    # events 

    for event in pygame.event.get(): 
     if event.type == QUIT: 
      running = False 
     if event.type == KEYDOWN: 
      if event.key == K_ESCAPE: 
       running = False 

    # moves 

    move_player() 
    # move_enemy() 

    # collision detection - nothing yet 

    # draws 

    screen.fill(WHITE) 
    draw_player() 
    # draw_enemy() 
    pygame.display.update() 

    # clock - to keep constant speed - nothing yet 

# quit 

pygame.quit() 
+0

'VELOCITY'도 글로벌입니다. – hjpotter92

+0

yes,'VELOCITY' 역시 글로벌합니다. 그러나이 문제는 중요하지 않습니다. . – furas

+0

감사합니다, 나는 내일 그것을 시험 할 것이다 모든 것을 기억하는 것이 어렵다. ... – PylonBuffering