2014-07-11 5 views
0

저는 파이 게임에 익숙하지 않아 내 배경 이미지에서 스프라이트를 이동하려고합니다. 이동 후 스프라이트가 다시 나타나지 않습니까? 어떤 아이디어? 이것은 화면이없는 대부분의 프로그램입니다. 당신은 어디서든 함수 또는 클래스 중 하나를 호출하지 않는왜 내 스프라이트가 움직이지 않습니까?

#dependencies 
import pygame as P 
import random as R 

def welcome(screen): 
    #load background 
    bg = P.image.load("space-wallpaper.jpg") 
    screen.blit(bg,[0,0]) 

    #set fonts etc 
    font = P.font.Font("Space_Age.ttf",60) 
    width, height = screen.get_size() 

    #play button 
    message = "PLAY " 
    text = font.render(message,1,[255 , 0, 0]) 
    rect = text.get_rect() 
    x, y = text.get_size() 
    rect = rect.move((width - x)/2, (height - y)/2) 
    screen.blit(text,rect) 

    #high_score button 
    message = "HIGH SCORE " 
    text = font.render(message,1,[255 , 0, 0]) 
    rect = text.get_rect() 
    x, y = text.get_size() 
    rect = rect.move((width - x)/2, (height - y)/2 +100) 
    screen.blit(text,rect) 

def play_button(screen): 
    """launch welcome screen. 

    """ 
    #welcome screen play button 
    font = P.font.Font("Space_Age.ttf",60) 
    message = "PLAY " 
    play_x,play_y = font.size(message) 
    play_text = font.render(message,1,[255 , 0, 0]) 
    width, height = 800,600 
    screen.blit(play_text,[(width - play_x)/2, (height - play_y)/2]) 
    play_rect = play_text.get_rect().move((width - play_x)/2, (height - play_y)/2) 
    P.display.flip() 
    return(play_rect) 

def welcome_background(screen): 
    # Welcome screen background 
    bg = P.image.load("space-wallpaper.jpg") 
    screen.blit(bg,[0,0]) 
    P.display.update() 


def high_score_screen(screen): 


"""opens the highscore screen""" 
    high_score_bg = P.image.load("bg_game.jpg") 
    screen.blit(high_score_bg,[0,0]) 
    P.display.update() 

def splash_screen(screen): 
    """loads the first screen in the game with a 3 sec wait""" 
    splash_image = P.image.load('splash.jpg') 
    screen.blit(splash_image,[0,0]) 
    P.display.update() 
    P.time.wait(2001) 

def play_game(screen): 
    """loads the play game screen""" 
    game_bg = P.image.load("bg_game.jpg") 
    screen.blit(game_bg,[0,0]) 
    P.display.update() 

def move_right(screen,x_cord,flagship): 

    dist = 20 
    play_game(screen) 
    x_cord = x_cord + dist 
    print(x_cord) 
    screen.blit(flagship,[x_cord]) 
    P.display.update() 

def key_detection(screen,flagship,x_cord): 


    key = P.key.get_pressed() 
    if key[P.K_RIGHT]: 
     move_right(screen,x_cord,flagship) 
     #move_right() 



    elif key[P.K_LEFT]: 
     print("left") 






class Sprite(): 
    def __init__(self,screen): 
     """ The constructor of the class """ 
     self.flagship = P.image.load("sprite2.png") 

     self.x = 0 
     self.y = 0 

    def display(self,screen): 
     #screen.blit(self.sprite,[self.x,self.y]) changed by geoff 
     screen.blit(self.flagship,[self.x,self.y]) 
     P.display.update() 





_init_ 

# dependencies 
from mods import * 
import pygame as P 

#initialise pygame 
P.init() 


def main(): 
    # parameters to control pygame basics 
    screen_size = width, height = 800,600 #sixe of playing screen 
    P.display.set_caption('Space Smasher!') 
    screen = P.display.set_mode(screen_size) 
    clock = P.time.Clock() # timer used to control rate of looping 
    loop_rate = 20 #number of times per second does loop 
    play = True #control the playing of the actual game 
    splash_screen(screen) 
    welcome(screen) 
    P.display.flip() 
    rect_play = play_button(screen) 
    flagship = Sprite(screen) 
    while play: 
     key_detection(screen,flagship.image,flagship.x) 
#  for event in P.event.poll(): changed by geoff 
     event = P.event.poll() #did the player do something? 
     if event.type == P.QUIT: 
      play = False 


     if event.type == P.MOUSEBUTTONDOWN: 
      player_position = P.mouse.get_pos() 
      if rect_play.collidepoint(player_position): 
       play_game(screen) 
       flagship.display(screen) 



    P.quit() 


if __name__ == '__main__': 
    main() 
+1

모두 갖고 계십니까? 'mainloop'은 어디에 있습니까? 모든 루프에서 메인 루프의 스프라이트를 blit해야합니다. – furas

+0

안녕하세요 고맙습니다. 도움을 주셔서 감사합니다. 이제 전체 프로그램으로 업데이트하겠습니다. – Allanco

+1

코드를 사용하여 외부 장소에 대한 링크를 제공 할 수 있습니다. – furas

답변

0

나는이 많은 시간 동안 일을 얻기 위해 노력 해왔다. 상단에 다음과 같은 수입으로

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

:

import pygame, sys 
from pygame.locals import * 
pygame.init() 
WIDTH=1439 
HEIGHT=791 
DISPLAYSURF = pygame.display.set_mode((WIDTH, HEIGHT)) 
pygame.display.set_caption('Hello Pygame World!') 
pygame.key.set_repeat(1, 10) 
circx, circy = 200, 150 
CIRWIDTH=20 
while True: # main game loop 
    if pygame.key.get_pressed()[pygame.K_UP]: 
     circy-=5 
    if pygame.key.get_pressed()[pygame.K_DOWN]: 
     circy+=5 
    if pygame.key.get_pressed()[pygame.K_RIGHT]: 
     circx+=5 
    if pygame.key.get_pressed()[pygame.K_LEFT]: 
     circx-=5 
    try: 
      for event in pygame.event.get(): 
       if event.type == QUIT or event.key == pygame.K_ESCAPE or event.key == pygame.K_q: 
        pygame.quit() 
        sys.exit() 
    except AttributeError: 
      pass 
    DISPLAYSURF.fill((0, 0, 0)) 
    pygame.draw.circle(DISPLAYSURF, (158, 219, 222), (circx, circy), CIRWIDTH) 
    pygame.display.flip() 
: 여기
import pygame, sys 
from pygame.locals import * 

이 키를 사용하여 객체를 이동의 예입니다 당신은 유사하게이로 구성되어 while 루프를 필요
0

홈페이지 루프가 있어야한다 유사에 :

while True: 

    # events - check keyboad and mouse and change player direction or speed 

    # move - move sprite with speed and direction 

    # check collison 

    # draw background, all objects and all sprites 

    # clock - to keep constant speed - FPS (Frames Per Seconds) 
,

그래서 모든 루프에서 스프라이트를 이동하여 그려야합니다.

+0

그래서 배경에 새로운 스프라이트를 그리지 않습니까? – Allanco

+0

당신은 매우 많은'display.update()'를 가지고있어서 어떤 것들이 실행되고 어느 것이 주력을 덮어 쓰는지 인식하기가 어렵습니다. – furas

+0

'move_right'는'cord_x' 만 변경해야합니다. 'blit (flagship, ...)'과'display.update()'는 메인 루프에서 한 번만 사용해야합니다. – furas

관련 문제