From 63eba9d183b51b84d140990192ffd3f728e7ba8b Mon Sep 17 00:00:00 2001 From: xu Date: Sat, 13 Jul 2024 09:55:55 +0800 Subject: [PATCH] Signed-off-by: xu --- .idea/.gitignore | 3 + .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 7 ++ .idea/modules.xml | 8 ++ .idea/绪鸣远.iml | 8 ++ 240615 移动控制.py | 73 +++++++++++++++++++ 240622 反弹的小球.py | 34 +++++++++ 7 files changed, 139 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/绪鸣远.iml create mode 100644 240615 移动控制.py create mode 100644 240622 反弹的小球.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..9de2865 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..2696a44 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/绪鸣远.iml b/.idea/绪鸣远.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/绪鸣远.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/240615 移动控制.py b/240615 移动控制.py new file mode 100644 index 0000000..e114bd4 --- /dev/null +++ b/240615 移动控制.py @@ -0,0 +1,73 @@ +import pygame,sys + +pygame.init() +window_size = (260,300) +sc = pygame.display.set_mode(window_size) +pygame.display.set_caption('反弹的小球') + +ball_xy = [110,175] +ball_r = 10 +ball_speed = [3,3] + +box = pygame.Rect(115,290,50,15) + +zk_lst = [] +zk_x = 10 +zk_y = 10 +for i in range(5): + for j in range(5): + zk_rect = pygame.Rect(zk_x,zk_y,40,20) + zk_lst.append(zk_rect) + zk_x += 50 + zk_x = 10 + zk_y += 30 + +running = True +while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + + sc.fill('white') + + ball = pygame.draw.circle(sc, 'black', ball_xy, ball_r) + + if ball_xy[0] - ball_r < 0 or ball_xy[0] + ball_r > window_size[0]: + ball_speed[0] = -ball_speed[0] + if ball_xy[1] - ball_r < 0 or ball_xy[1] + ball_r > window_size[1]: + ball_speed[1] = -ball_speed[1] + if ball_xy[1] + ball_r > 300 and not ball.colliderect(box): + running = False + if ball.colliderect(box): + ball_speed[1] = -ball_speed[1] + + for i in zk_lst: + if ball.colliderect(i): + zk_lst.remove(i) + if zk_lst == []: + running = False + + ball_xy[0] += ball_speed[0] + ball_xy[1] += ball_speed[1] + + pygame.draw.rect(sc, (0, 0, 0), rect=box) + + for i in zk_lst: + pygame.draw.rect(sc, 'blue', rect=i) + + keys = pygame.key.get_pressed() + if keys[pygame.K_LEFT]: + if box.x <= 0: + continue + else: + box.x += -2 + if keys[pygame.K_RIGHT]: + if box.x >= 260: + continue + else: + box.x += 2 + + pygame.display.flip() + pygame.time.Clock().tick(35) +pygame.quit() +sys.exit() \ No newline at end of file diff --git a/240622 反弹的小球.py b/240622 反弹的小球.py new file mode 100644 index 0000000..1baa845 --- /dev/null +++ b/240622 反弹的小球.py @@ -0,0 +1,34 @@ +import pygame,sys + +pygame.init() + +window_size = (800,600) +sc = pygame.display.set_mode(window_size) +pygame.display.set_caption('反弹的小球') +ball_xy = [400,300] +ball_r = 20 +ball_speed = [5,5] + +running = True +while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + + ball_xy[0] += ball_speed[0] + ball_xy[1] += ball_speed[1] + + if ball_xy[0] - ball_r < 0 or ball_xy[0] + ball_r > window_size[0]: + ball_speed[0] = -ball_speed[0] + if ball_xy[1] - ball_r < 0 or ball_xy[1] + ball_r > window_size[1]: + ball_speed[1] = -ball_speed[1] + + sc.fill('white') + + pygame.draw.circle(sc,'black',ball_xy,ball_r) + pygame.display.flip() + + pygame.time.Clock().tick(60) + +pygame.quit() +sys.exit() \ No newline at end of file