python 移動オブジェクトの描画

初めて質問します。足りない情報があれば指摘してください。
環境としてはJupyter Notebookでpythonのプログラムを作っています。

ex13.ipynbという家や雲、木、蝶々などが描画されるプログラム(以下に添付)に対して、ex21.ipynbというR.jpg画像がスクリーンの上部及び下部で横に繰り返して流し、スクリーン全体でブラウンで運動するプログラム(以下に添付)を参考にex13.ipynbのプログラムの雲が横に流れるかつ、蝶々がブラウン運動するように改変せよという課題が出ました。

自分なりに改変してみたのですが雲や蝶々がそもそも消消えたり上手くいきません。自分なりに改変したプログラム(ex13改.ipynb)も以下に添付しますのでおかしな点があれば指摘していただければ幸いです。正しいコードなども教えていただければ尚嬉しいです。よろしくお願いします。

<ex13.ipynb>

# setup import random import pygame pygame.init() screen = pygame.display.set_mode((640, 480)) def random_color(): red = random.randint(0, 255) green = random.randint(0, 255) blue = random.randint(0, 255) return (red, green, blue) def draw_tree(x, y): # tree trunk (50 wide and 100 tall) pygame.draw.rect(screen, (117, 90, 0), (x, y-100, 50, 100)) # leaves are a circle pygame.draw.circle(screen, (27, 117, 0), (x+25, y-120), 50) def draw_house(x, y): # pink house pygame.draw.rect(screen, (255, 171, 244), (x, y-180, 200, 180)) # brown door pygame.draw.rect(screen, (89, 71, 0), (x+80, y-60, 40, 60)) # yellow door knob pygame.draw.circle(screen, (255, 204, 0), (x+112, y-30), 4) # triangle roof pygame.draw.polygon(screen, (125, 125, 125), (( x, y-180), (x+100, y-250), (x+200, y-180))) draw_window(x+20, y-90) draw_window(x+130, y-90) def draw_window(x, y): # glass pygame.draw.rect(screen, random_color(), (x, y-50, 50, 50)) # frame pygame.draw.rect(screen, (0, 0, 0), (x, y-50, 50, 50), 5) pygame.draw.rect(screen, (0, 0, 0), (x+23, y-50, 5, 50)) pygame.draw.rect(screen, (0, 0, 0), (x, y-27, 50, 5)) # this function is able to draw clouds of different sizes def draw_cloud(x, y, size): # put int() around any multiplications by decimals to get rid of this warning: # DeprecationWarning: integer argument expected, got float pygame.draw.circle(screen, (255, 255, 255), (x, y), int(size*.5)) pygame.draw.circle(screen, (255, 255, 255), (int(x+size*.5), y), int(size*.6)) pygame.draw.circle(screen, (255, 255, 255), (x+size, int(y-size*.1)), int(size*.4)) # green ground pygame.draw.rect(screen, (0, 160, 3), (0, 400, 640, 80)) # light blue sky pygame.draw.rect(screen, (135, 255, 255), (0, 0, 640, 400)) draw_tree(60, 400) # x and y location are the bottom left of tree trunk draw_tree(550, 400) draw_house(225, 400) draw_cloud(60, 120, 80) draw_cloud(200, 50, 40) draw_cloud(450, 100, 120) pygame.display.flip() running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit()

<ex21.ipynb>

# setup import pygame import math from scipy.stats import norm import time pygame.init() screen = pygame.display.set_mode([640, 480]) white = [255, 255, 255] def simple_movement(x, y, s_x, s_y): return int(x+s_x), int(y+s_y) def sin_movement(x, y, s_x, s_y): return int(x+s_x), int(y+math.sin((x+s_x)/screen.get_width()*2*math.pi*s_y)*my_R.get_height()) def Brownian_movement(x, y, s, delta, dt): return int(x + s*norm.rvs(delta**2*dt)), int(y + s*norm.rvs(delta**2*dt)) # load the R image (must be saved in the same place as this file) my_R = pygame.image.load('R.jpg') # set variables to use in our program x_Rh = 0 y_Rh = 2*my_R.get_height() speed_x_Rh = 5 speed_y_Rh = 0 x_Rs = 0 y_Rs0 = screen.get_height()-3*my_R.get_height() speed_x_Rs = 2.5 speed_y_Rs = 5 x_Rr = screen.get_width()/2 y_Rr = screen.get_height()/2 speed_Rr = 50 delta = 0.25 dt = 0.1 my_time = time.gmtime() my_pre_sec = my_time[5] running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE): running = False pygame.time.delay(17) # pause for 17 milliseconds screen.fill(white) # make the screen completely white # the animation logic my_time = time.gmtime() if my_time[5] != my_pre_sec: my_pre_sec = my_time[5] (x_Rh, y_Rh) = simple_movement(x_Rh, y_Rh, speed_x_Rh, speed_y_Rh) if x_Rh > screen.get_width(): x_Rh = 0 (x_Rs, y_Rs) = sin_movement(x_Rs, y_Rs0, speed_x_Rs, speed_y_Rs) if x_Rs > screen.get_width(): x_Rs = 0 (x_Rr, y_Rr) = Brownian_movement(x_Rr, y_Rr, speed_Rr, delta, dt) if x_Rr > screen.get_width(): x_Rr = screen.get_width() - my_R.get_width() elif x_Rr < 0: x_Rr = 0 if y_Rr > screen.get_height(): y_Rr = screen.get_height() - my_R.get_height() elif y_Rr < 0: y_Rr = 0 # draw the image on the screen at the x and y location screen.blit(my_R, [x_Rh, y_Rh]) screen.blit(my_R, [x_Rs, y_Rs]) screen.blit(my_R, [x_Rr, y_Rr]) # draw a line on the screen from the center to the Brownian moving R image pygame.draw.line(screen, pygame.color.THECOLORS['red'], (screen.get_width( )/2, screen.get_height()/2), (x_Rr, y_Rr), 2) # update the relavant portions in the display pygame.display.update() pygame.quit()

<ex13改.ipynb>

import pygame import math from scipy.stats import norm import random import time pygame.init() screen = pygame.display.set_mode((640, 480)) def simple_movement(x, y, s_x, s_y): return x + s_x, y + s_y def Brownian_movement(x, y, s, delta, dt): return int(x + s * norm.rvs(delta ** 2 * dt)), int(y + s * norm.rvs(delta ** 2 * dt)) def random_color(): red = random.randint(0, 255) green = random.randint(0, 255) blue = random.randint(0, 255) return (red, green, blue) def draw_tree(x, y): pygame.draw.rect(screen, (117, 90, 0), (x, y-100, 50, 100)) pygame.draw.circle(screen, (27, 117, 0), (x+25, y-120), 50) def draw_house(x, y): pygame.draw.rect(screen, (255, 171, 244), (x, y-180, 200, 180)) pygame.draw.rect(screen, (89, 71, 0), (x+80, y-60, 40, 60)) pygame.draw.circle(screen, (255, 204, 0), (x+112, y-30), 4) pygame.draw.polygon(screen, (125, 125, 125), ((x, y-180), (x+100, y-250), (x+200, y-180))) draw_window(x+20, y-90) draw_window(x+130, y-90) def draw_window(x, y): pygame.draw.rect(screen, random_color(), (x, y-50, 50, 50)) pygame.draw.rect(screen, (0, 0, 0), (x, y-50, 50, 50), 5) pygame.draw.rect(screen, (0, 0, 0), (x+23, y-50, 5, 50)) pygame.draw.rect(screen, (0, 0, 0), (x, y-27, 50, 5)) def draw_cloud(x_c, y_c, size, speed): pygame.draw.circle(screen, (255, 255, 255), (x_c, y_c), int(size*.5)) pygame.draw.circle(screen, (255, 255, 255), (int(x_c+size*.5), y_c), int(size*.6)) pygame.draw.circle(screen, (255, 255, 255), (x_c+size, int(y_c-size*.1)), int(size*.4)) def draw_butterfly(x_b, y_b, size_b, speed, delta, dt): pygame.draw.ellipse(screen, random_color(), (x_b, y_b, size, int(size * 0.3))) pygame.draw.ellipse(screen, random_color(), (x_b, y_b + 15, size, int(size * 0.3))) # Draw wings pygame.draw.polygon(screen, (0, 0, 0), [(x_b + size * 0.1, y_b + size * 0.3), (x_b + size * 0.5, y_b + size * 0.1), (x_b + size * 0.9, y_b + size * 0.3)]) pygame.draw.polygon(screen, random_color(), [(x_b + size * 0.1, y_b + size * 0.3), (x_b + size * 0.5, y_b + size * 0.5), (x_b + size * 0.9, y_b + size * 0.3)]) # Draw antennae pygame.draw.line(screen, (0, 0, 0), (x_b + size * 0.4, y_b + size * 0.03), (x_b + size * 0.2, y_b - size * 0.2), 2) pygame.draw.line(screen, (0, 0, 0), (x_b + size * 0.6, y_b + size * 0.03), (x_b + size * 0.8, y_b - size * 0.2), 2) # Ground and sky pygame.draw.rect(screen, (0, 160, 3), (0, 400, 640, 80)) pygame.draw.rect(screen, (135, 255, 255), (0, 0, 640, 400)) # Scene elements draw_tree(60, 400) draw_tree(550, 400) draw_tree(590, 440) draw_tree(20, 440) draw_house(225, 400) draw_window(355, 390) pygame.display.flip() x_c = 0 y_c = screen.get_height() speed_x_c = 5 speed_y_c = 0 x_b = screen.get_width()/2 y_b = screen.get_height()/2 speed_b = 50 delta = 0.25 dt = 0.1 my_time = time.gmtime() my_pre_sec = my_time[5] running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False my_time = time.gmtime() if my_time[5] != my_pre_sec: my_pre_sec = my_time[5] pygame.display.update() (x_c, y_c) = simple_movement(x_c, y_c, speed_x_c, 0) if x_c > screen.get_width(): x_c = 0 elif x_c < 0: x_c = screen.get_width() (x_b, y_b) = Brownian_movement(x_b, y_b, speed_b, delta, dt) if x_b > screen.get_width(): x_b = screen.get_width() elif x_b < 0: x_b = 0 if y_b > screen.get_height(): y_b = screen.get_height() elif y_b < 0: y_b = 0 pygame.quit()

コメントを投稿

0 コメント