Hazırlayan: Chris Piech
Çeviren: Ceren Kocaoğullar
Kullanıcının faresini yatay olarak izleyerek ekranın alt kısmında hareket eden bir rakete (paddle) sahip bir program oluşturun. Kullanıcı fareyi tıkladığında, raketten bir roket ateşleyin ve onu ekranda yukarı doğru hareket ettirin! Ateşleyebileceğiniz roket sayısının bir sınırı yoktur.
"""File: rocket_paddle.py-------------------Creates a paddle that tracks the user's mouse, and that shoots rocketson click that travel up the screen."""from graphics import Canvasimport timeROCKET_DIAMETER = 10# The amount a rocket moves each frameROCKET_MOVE_AMOUNT = 5# The paddle location and dimensionsPADDLE_Y_OFFSET = 50PADDLE_WIDTH = 100PADDLE_HEIGHT = 20ANIMATION_DELAY_SECONDS = 0.02def main():canvas = Canvas()canvas.set_canvas_title("Debris Sweeper")# The list of all rockets on the canvasrocket_list = []paddle = create_paddle(canvas)canvas.update()while True:update_paddle_location(canvas, paddle)check_for_new_rockets(canvas, rocket_list)animate_rockets(canvas, rocket_list)canvas.update()time.sleep(ANIMATION_DELAY_SECONDS)canvas.mainloop()def create_paddle(canvas):"""Creates and returns a paddle rectangle initialized at the bottom ofthe screen at the appropriate vertical offset, filled black."""y = canvas.get_canvas_height() - PADDLE_Y_OFFSETpaddle = canvas.create_rectangle(0, y, PADDLE_WIDTH, y + PADDLE_HEIGHT)canvas.set_color(paddle, 'black')return paddledef update_paddle_location(canvas, paddle):"""Updates the paddle location to track the mouse. The paddle ycoordinate will not change, but the x coordinate will be centeredat the mouse x."""canvas.moveto(paddle, canvas.get_mouse_x() - canvas.get_width(paddle) / 2,canvas.get_top_y(paddle))def check_for_new_rockets(canvas, rocket_list):"""Checks if there are any new mouse clicks, and if there were, addsa new rocket to the screen at the location of each mouse click, andalso adds the rocket (circle) to the rocket list."""clicks = canvas.get_new_mouse_clicks()for click in clicks:y = canvas.get_canvas_height() - PADDLE_Y_OFFSETrocket = canvas.create_oval(click.x, y,click.x + ROCKET_DIAMETER,y + ROCKET_DIAMETER)canvas.set_color(rocket, 'blue')rocket_list.append(rocket)def animate_rockets(canvas, rocket_list):"""Animates all rockets in the rocket list that are still visible onthe canvas."""for rocket in rocket_list:if canvas.get_top_y(rocket) + canvas.get_height(rocket) >= 0:canvas.move(rocket, 0, -ROCKET_MOVE_AMOUNT)if __name__ == "__main__":main()