2017-12-09 36 views
0

거북이를 사용하여 우주 침략자 게임을 만들려고하고 있으며 총알을 화면 위로 이동시키기 위해 매 0.5 초마다 y 좌표를 업데이트하려고합니다. while 루프와 time.sleep() 함수를 사용하여이 작업을 시도했지만 총알을 발사하려고 할 때마다 프로그램이 중단됩니다. 이 일을 막을 수있는 방법이 있습니까? 아니면 작동하지 않는 이유는 무엇입니까? 내 프로그램을 중단시키지 않는 똑같이 효과적인 방법이 있습니까?while 루프 내의 time.sleep 함수가 프로그램, 솔루션 또는 대체 방법을 계속 충돌합니까?

import turtle 
import time 

userx = 0 

x = 0 
y = -300 

enemies = [(300,50, "left"), (400,50, "left"), (350, -50, "right")] 

bullets = [] 

gameover = False 

def frame(): 
    pass 
    ship = turtle.Turtle() 
    ship.pu() 
    ship.ht() 
    ship.setpos(userx, -300) 
    ship.pd() 
    ship.circle(5) 

    bullet = turtle.Turtle() 
    bullet.pu() 
    bullet.ht() 
    bullet.setpos(x, y) 
    bullet.pd() 
    bullet.circle(2) 

def key_left(): 
    global userx 
    pass 
    userx += -10 
    print(userx) 

def key_right(): 
    global userx 
    pass 
    userx += 10 
    print(userx) 

def key_space(): 
    pass # your code here 
    global x 
    global y 
    global bullets 
    x = userx 
    while y <= 300: 
     time.sleep(0.5) 
     y += 4 
    else: y = 0 
    bullets += (x,y) 

def physics(): 
    global bullets 
    global enemies 
    pass 

def ai(): 
    global enemies 
    global gameover 
    pass 

def reset(): 
    global enemies 
    global bullets 
    global userx 
    global gameover 
    pass 

def main(): 
    turtle.tracer(0,0) 
    turtle.hideturtle() 
    turtle.onkey(key_left, "Left") 
    turtle.onkey(key_right, "Right") 
    turtle.onkey(key_space, "space") 
    turtle.listen() 
    reset() 
    while not gameover: 
     turtle.clear() 
     physics() 
     ai() 
     frame() 
     turtle.update() 
     time.sleep(0.05) 

main() 
+0

'통과'로 인해 충돌이 발생하지 않았습니까? –

+0

우리는이 코드 스 니펫만으로는 도움이되지 않습니다. 더 많은 컨텍스트가 필요합니다. 또한'time.sleep'을 사용하지 말고 기본 tkinter 표시 루프를 사용하십시오 (예 : ['after'] (http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method)) . –

+0

@ F.Leone, 지금 막 확인하고 문제가 지속되는 패스가 아닙니다. – Billiam

답변

1

나는 것 : 원수와의 충돌를 확인하는 코드에서

def key_space(): 
    # simply spawn a new bullet and put it into your bullets list 

def advance_bullet(): # call this after paintnig all bullets in frame 
    # iterate over all bullets inside bullets, advance the Y position by 3 

def frame(): 
    # add code to paint all bullets inside bullets - and call advance_bullets() 

: 당신이 intervall 메인 루프 다음 ​​글 머리 기호 느린 발전을 네드 경우

# remove a bullet from bullets when its outside your screen (or passed all enemies max y coord) - no need to track that bullet anymore 

하는을 "framespassed"카운터를보고 framespassed % something == 0인지 확인한 다음 총알 만 진행하십시오.

당신은이 부분을 변경할 필요가 이미

이 무엇인지에 적응 :

def frame(): 
    global bullets 
    pass 
    ship = turtle.Turtle() 
    ship.pu() 
    ship.ht() 
    ship.setpos(userx, -300) 
    ship.pd() 
    ship.circle(5) 

    # debugging all bullets: 
    # print(bullets) # remove this 

    for idx in range(0,len(bullets)): # paint ALL bullets in the bullets list 
     bulletTurtle = turtle.Turtle() 
     b = bullets[idx] # current bullet we are painting 
     bulletTurtle.pu() 
     bulletTurtle.ht() 
     bulletTurtle.setpos(b[0], b[1]) 
     bulletTurtle.pd() 
     bulletTurtle.circle(2) 
     b[1] += 13 # quick and dirty approach, move bulltet after painting 
        # to save another complete bullets-foreach-loop 

    # quick n dirty bullet removal for out of screen bullets 
    # normally I would do this probably in your "check if enemy hit" 
    # method as you are going over bullets there as well and can remove 
    # them as soon as they passed all enemies 
    bullets = [x for x in bullets if x[1] < 500] 


def key_space(): 
    global x 
    global y 
    global bullets 
    bullets.append([userx,-300]) # start a new bullet at current player position 

편집 :

당신은 거북이를 turtle.shape를 살펴 할 수 있습니다. 크기 및 거북. 스탬프 - "원형"모양과 맞는 크기를 사용하면이 도형을 "찍을"수 있습니다. 장점 : 스탬프 된 도형을 정수 ID로 간단히 삭제할 수 있습니다. 나는 거북이와 함께 일한 적이 없다 - 플레이어의 위치 및/또는 글 머리 기호를 쉽게 다시 그리는 방법이된다면 자신을 고려하십시오.

1

이상적이지는 않지만 작동하기 시작합니다.

key_space에는 목록에 새로운 글 머리 기호 만 추가됩니다.
while not gameover에서 모든 글 머리표를 움직일 수있는 기능을 실행합니다.
frame에서 모든 글 머리 기호를 그립니다.

import turtle 
import time 

userx = 0 

x = 0 
y = -300 

enemies = [(300,50, "left"), (400,50, "left"), (350, -50, "right")] 

bullets = [] 

gameover = False 

def frame(): 
    pass 
    ship = turtle.Turtle() 
    ship.pu() 
    ship.ht() 
    ship.setpos(userx, -300) 
    ship.pd() 
    ship.circle(5) 

    # redraw bullets 
    for x, y in bullets: 
     bullet = turtle.Turtle() 
     bullet.pu() 
     bullet.ht() 
     bullet.setpos(x, y) 
     bullet.pd() 
     bullet.circle(2) 

def key_left(): 
    global userx 
    pass 
    userx += -10 
    print(userx) 

def key_right(): 
    global userx 
    pass 
    userx += 10 
    print(userx) 

def key_space(): 
    pass # your code here 
    global x 
    global y 
    global bullets 
    x = userx 

    # add bullet to list 
    bullets.append([x, -300]) 

def move_bullets(): 
    global bullets 

    live_bullets = [] 

    # move every bullet and check if should still live 
    for x, y in bullets: 
     y += 4 

     # keep only some bullets and other forget (kill/remove) 
     if y <= 300: 
      live_bullets.append([x, y]) 

    bullets = live_bullets 

def physics(): 
    global bullets 
    global enemies 
    pass 

def ai(): 
    global enemies 
    global gameover 
    pass 

def reset(): 
    global enemies 
    global bullets 
    global userx 
    global gameover 
    pass 

def main(): 
    turtle.tracer(0,0) 
    turtle.hideturtle() 
    turtle.onkey(key_left, "Left") 
    turtle.onkey(key_right, "Right") 
    turtle.onkey(key_space, "space") 
    turtle.listen() 
    reset() 
    while not gameover: 
     turtle.clear() 
     physics() 
     ai() 
     move_bullets() # <-- move bullets (or maybe before physics) 
     frame() 
     turtle.update() 
     time.sleep(0.05) 

main()