2017-11-14 2 views
1

그래서 Python 3.6 용 Pygame에 대한 도움이 필요합니다. 이것은 프로그래밍 수업을 듣는 첫 학기이며, 네, 제가 도움이 필요한 숙제입니다. 내 빗방울이 정적 일 때 코드를 작동 시켰지만, 일단 어떤 이유로 움직임을 넣으면 이제는 한 행만로드합니다. 하단 행이 화면에서 사라질 때마다 화면 아래로 이동하고 새로운 행을로드하는 빗방울이 필요합니다. 그리고 네, 아마도이 모든 모듈을 가지고있는 것보다 쉬운 방법 일 것입니다. 그러나 그것이 책이 그것을 원하는 방법입니다. 여기에 지금까지있어 무엇 : raindropgame.py이 코드가 왜 빗방울 그리드를 만들지 않는 이유는 무엇입니까?

import sys 
import pygame 
from pygame.sprite import Group 
from settings import Settings 
from raindrop import Raindrop 
import game_functions as gf 

def run_game(): 
    # Initialize game and create a screen object. 
    pygame.init() 
    settings = Settings() 
    screen = pygame.display.set_mode((settings.screen_width, 
settings.screen_height)) 
    pygame.display.set_caption("13-3") 

    # Make raindrop group. 
    raindrops = Group() 

    # Create the raindrops. 
    gf.create_raindrops(settings, screen, raindrops) 

    # Start the main loop for the game. 
    while True: 

     gf.update_raindrops(raindrops) 
     gf.update_screen(settings, screen, raindrops) 

     # Watch for keyboard and mouse events. 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       sys.exit() 

run_game() 

settings.py

class Settings(): 
    """A class to store all settings.""" 

    def __init__(self): 
     """Initialize the game's settings.""" 
     # Screen settings 
     self.screen_width = 1200 
     self.screen_height = 800 
     self.bg_color = (0, 0, 255) 

     # Raindrop settings 
     self.raindrop_speed_factor = 1 

game_functions.py

import sys 
import pygame 
from raindrop import Raindrop 

def update_screen(settings, screen, raindrops): 
    """Update images on the screen and flip to the new screen.""" 
    # Redraw the screen during each pass through the loop. 
    screen.fill(settings.bg_color) 
    raindrops.draw(screen) 

    # Make the most recently drawn screen visible. 
    pygame.display.flip() 

def get_number_raindrops_x(settings, raindrop_width): 
    """Determine the number of raindrops that fit in a row.""" 
    available_space_x = settings.screen_width - 2 * raindrop_width 
    number_raindrops_x = int(available_space_x/(2 * raindrop_width)) 
    return number_raindrops_x 

def get_number_rows(settings, raindrop_height): 
    """Determine the number of rows of raindrops that fit on the screen.""" 
    available_space_y = settings.screen_height - (2 * raindrop_height) 
    number_rows = int(available_space_y/(2 * raindrop_height)) 
    return number_rows 

def create_raindrop(settings, screen, raindrops, raindrop_number, row_number): 
    """Create a raindrop and place it in the row.""" 
    raindrop = Raindrop(settings, screen) 
    raindrop_width = raindrop.rect.width 
    raindrop.x = raindrop_width + 2 * raindrop_width * raindrop_number 
    raindrop.rect.x = raindrop.x 
    raindrop.rect.y = raindrop.rect.height + 2 * raindrop.rect.height * row_number 
    raindrops.add(raindrop) 

def create_raindrops(settings, screen, raindrops): 
    """Create a full sky of raindrops.""" 
    # Create a raindrop and find the number of raindrops in a row. 
    raindrop = Raindrop(settings, screen) 
    number_raindrops_x = get_number_raindrops_x(settings, raindrop.rect.width) 
    number_rows = get_number_rows(settings, raindrop.rect.height) 

    # Create the full sky of raindrops. 
    for row_number in range(number_rows): 
     for raindrop_number in range(number_raindrops_x): 
      create_raindrop(settings, screen, raindrops, raindrop_number, row_number) 

def update_raindrops(raindrops): 
    """Update the positions of all raindrops in sky.""" 
    raindrops.update() 

raindrop.py

import pygame 
from pygame.sprite import Sprite 

class Raindrop(Sprite): 
    """A class to represent a single raindrop in the sky.""" 

    def __init__(self, settings, screen): 
     """Initialize the raindrop and set its starting position.""" 
     super(Raindrop, self).__init__() 
     self.screen = screen 
     self.settings = settings 

     # Load the raindrop image and set its rect attribute. 
     self.image = pygame.image.load('images/raindrop.png') 
     self.rect = self.image.get_rect() 

     # Start each new raindrop near the top left of the screen. 
     self.rect.x = self.rect.width 
     self.rect.y = self.rect.height 

     self.y = float(self.rect.y) 

    def update(self): 
     """Move raindrop down.""" 
     self.y += self.settings.raindrop_speed_factor 
     self.rect.y = self.y 

    def blitme(self): 
     """Draw the raindrop at its current location.""" 
     self.screen.blit(self.image, self.rect) 

누구나 할 수 있니? 내가 잘못한 것을 나에게 맡기고, 최하 행을 떠날 때 새로운 행을 만드는 방법도있을 것이다. 왜냐하면 나는 전혀 모른다. :/

답변

1

Raindrop.update()에서 위치를 확인하고 변경해야합니다. 이 바닥에 화면을 벗어나면

(self.rect.y >= setting,screen_height)
그런 다음 상단 (self.rect.y = 0)로 이동해야합니다. 또는 상단 경계 위의 이벤트.

def update(self): 
    """Move raindrop down.""" 
    self.y += self.settings.raindrop_speed_factor 

    # if it leaves bottom border 
    if self.y >= self.settings.screen_height: 
     # then put above top border 
     self.y = -self.rect.height 

    self.rect.y = self.y 

나는 맨 윗줄 위의 두 행을 만들 좋은 것입니다. 시작시 화면 밖으로 나옵니다.하지만 아래로 내려갈 때 맨 아래 행을 맨 위로 이동하기 전에 맨 위 행이 채워집니다. create_raindrop()에서

# Create the full sky of raindrops. 
for row_number in range(-2, number_rows): 
+0

너무 감사합니다! 나는 그것이 왜 작동하는지 전혀 모른다. 그러나 그것은한다! 단! 다시 한 번 감사드립니다! –

1

, 당신은 변경해야

raindrop.rect.y = raindrop.rect.height + 2 * raindrop.rect.height * row_number 

에 :

raindrop.y = raindrop.rect.height + 2 * raindrop.rect.height * row_number 
raindrop.rect.y = raindrop.y 
+0

고마워요! 나는 내가 지금 잘못하고있는 것을 이해하고 있다고 생각한다. 당신의 도움을 주셔서 감사합니다! –

관련 문제