Spaces:
Running
Running
Commit ·
8f48bbc
1
Parent(s): 0c25511
Style redesign to match telearn.best aesthetic, URL ID hashing, and share link generator
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- projects/.DS_Store +0 -0
- projects/2026project_class/.DS_Store +0 -0
- projects/2026project_class/examples/.DS_Store +0 -0
- projects/DEMO/Student1/MakeCode 跳舞機/link.txt +1 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/ball_blue.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_blue.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_blue_cracked.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_brown.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_brown_cracked.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_cyan.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_cyan_cracked.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_dark_green.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_dark_green_cracked.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_green.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_green_cracked.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_grey.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_grey_cracked.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_orange.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_orange_cracked.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_purple.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_purple_cracked.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_red.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_red_cracked.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_yellow.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/brick_yellow_cracked.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/bullet.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/heart.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/paddle_1.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/paddle_2.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/paddle_3.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/paddle_gun_1.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/paddle_gun_2.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/paddle_gun_3.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/paddle_long.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/powerup_acid_ball.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/powerup_capsule_empty.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/powerup_expand.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/powerup_fast.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/powerup_fireball.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/powerup_laser.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/powerup_multi_ball.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/powerup_plus_100.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/powerup_plus_250.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/powerup_plus_50.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/powerup_plus_500.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/powerup_shrink.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/powerup_slow.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/assets/star_yellow.png +3 -0
- projects/DEMO/Student1/Pygame_Breakout/main.py +257 -0
- projects/DEMO/Student1/Pygame乒乓/main.py +172 -0
projects/.DS_Store
DELETED
|
Binary file (6.15 kB)
|
|
|
projects/2026project_class/.DS_Store
DELETED
|
Binary file (6.15 kB)
|
|
|
projects/2026project_class/examples/.DS_Store
DELETED
|
Binary file (6.15 kB)
|
|
|
projects/DEMO/Student1/MakeCode 跳舞機/link.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
https://makecode.com/_fcva84c8D5jD
|
projects/DEMO/Student1/Pygame_Breakout/assets/ball_blue.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_blue.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_blue_cracked.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_brown.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_brown_cracked.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_cyan.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_cyan_cracked.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_dark_green.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_dark_green_cracked.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_green.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_green_cracked.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_grey.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_grey_cracked.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_orange.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_orange_cracked.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_purple.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_purple_cracked.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_red.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_red_cracked.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_yellow.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/brick_yellow_cracked.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/bullet.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/heart.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/paddle_1.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/paddle_2.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/paddle_3.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/paddle_gun_1.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/paddle_gun_2.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/paddle_gun_3.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/paddle_long.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/powerup_acid_ball.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/powerup_capsule_empty.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/powerup_expand.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/powerup_fast.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/powerup_fireball.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/powerup_laser.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/powerup_multi_ball.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/powerup_plus_100.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/powerup_plus_250.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/powerup_plus_50.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/powerup_plus_500.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/powerup_shrink.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/powerup_slow.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/assets/star_yellow.png
ADDED
|
Git LFS Details
|
projects/DEMO/Student1/Pygame_Breakout/main.py
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
| 1 |
+
import pygame
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
SCREEN_WIDTH = 800
|
| 5 |
+
SCREEN_HEIGHT = 600
|
| 6 |
+
|
| 7 |
+
BLACK = (0, 0, 0)
|
| 8 |
+
WHITE = (255, 255, 255)
|
| 9 |
+
YELLOW = (255, 255, 0)
|
| 10 |
+
RED = (255, 0, 0)
|
| 11 |
+
|
| 12 |
+
PADDLE_NEW_WIDTH = 110
|
| 13 |
+
PADDLE_NEW_HEIGHT = 22
|
| 14 |
+
BRICK_NEW_WIDTH = 80
|
| 15 |
+
BRICK_NEW_HEIGHT = 25
|
| 16 |
+
|
| 17 |
+
BALL_RADIUS = 10
|
| 18 |
+
|
| 19 |
+
def draw_text(surface, text, size, x, y):
|
| 20 |
+
font = pygame.font.Font(None, size)
|
| 21 |
+
text_surface = font.render(text, True, WHITE)
|
| 22 |
+
text_rect = text_surface.get_rect()
|
| 23 |
+
text_rect.midtop = (x, y)
|
| 24 |
+
surface.blit(text_surface, text_rect)
|
| 25 |
+
|
| 26 |
+
class Paddle(pygame.sprite.Sprite):
|
| 27 |
+
def __init__(self):
|
| 28 |
+
super().__init__()
|
| 29 |
+
|
| 30 |
+
self.animation_frames = [
|
| 31 |
+
pygame.transform.scale( pygame.image.load("assets/paddle_1.png").convert_alpha(), (PADDLE_NEW_WIDTH, PADDLE_NEW_HEIGHT) ),
|
| 32 |
+
pygame.transform.scale( pygame.image.load("assets/paddle_2.png").convert_alpha(), (PADDLE_NEW_WIDTH, PADDLE_NEW_HEIGHT) ),
|
| 33 |
+
pygame.transform.scale( pygame.image.load("assets/paddle_3.png").convert_alpha(), (PADDLE_NEW_WIDTH, PADDLE_NEW_HEIGHT) )
|
| 34 |
+
]
|
| 35 |
+
self.current_frame = 0
|
| 36 |
+
self.image = self.animation_frames[self.current_frame]
|
| 37 |
+
|
| 38 |
+
self.rect = self.image.get_rect()
|
| 39 |
+
self.rect.x = (SCREEN_WIDTH - self.rect.width) // 2
|
| 40 |
+
self.rect.y = SCREEN_HEIGHT - self.rect.height - 10
|
| 41 |
+
|
| 42 |
+
self.last_update = pygame.time.get_ticks()
|
| 43 |
+
self.frame_rate = 100
|
| 44 |
+
|
| 45 |
+
def update(self):
|
| 46 |
+
now = pygame.time.get_ticks()
|
| 47 |
+
if now - self.last_update > self.frame_rate:
|
| 48 |
+
self.last_update = now
|
| 49 |
+
self.current_frame = (self.current_frame + 1) % len(self.animation_frames)
|
| 50 |
+
self.image = self.animation_frames[self.current_frame]
|
| 51 |
+
|
| 52 |
+
def move(self,keys):
|
| 53 |
+
if keys[pygame.K_LEFT]:
|
| 54 |
+
self.rect.x -= 7
|
| 55 |
+
if keys[pygame.K_RIGHT]:
|
| 56 |
+
self.rect.x += 7
|
| 57 |
+
if keys[pygame.K_UP]:
|
| 58 |
+
self.rect.y -= 7
|
| 59 |
+
if keys[pygame.K_DOWN]:
|
| 60 |
+
self.rect.y += 7
|
| 61 |
+
if self.rect.right >= SCREEN_WIDTH:
|
| 62 |
+
self.rect.right = SCREEN_WIDTH
|
| 63 |
+
if self.rect.left <= 0:
|
| 64 |
+
self.rect.left = 0
|
| 65 |
+
def widden(self):
|
| 66 |
+
print("球拍變寬了")
|
| 67 |
+
center = self.rect.x
|
| 68 |
+
new_width = int(PADDLE_NEW_WIDTH*1.5)
|
| 69 |
+
self.animation_frames = [
|
| 70 |
+
pygame.transform.scale(frame, (new_width, PADDLE_NEW_HEIGHT))
|
| 71 |
+
for frame in self.animation_frames
|
| 72 |
+
]
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
class Ball(pygame.sprite.Sprite):
|
| 76 |
+
def __init__(self):
|
| 77 |
+
super().__init__()
|
| 78 |
+
self.image = pygame.image.load("assets/ball_blue.png").convert_alpha()
|
| 79 |
+
self.image = pygame.transform.scale(self.image, (BALL_RADIUS * 2, BALL_RADIUS * 2))
|
| 80 |
+
self.rect = self.image.get_rect()
|
| 81 |
+
self.reset()
|
| 82 |
+
|
| 83 |
+
def reset(self):
|
| 84 |
+
self.rect.x = SCREEN_WIDTH // 2
|
| 85 |
+
self.rect.y = SCREEN_HEIGHT // 2 + 100
|
| 86 |
+
self.speed_x = random.choice([4, -4])
|
| 87 |
+
self.speed_y = -4
|
| 88 |
+
|
| 89 |
+
def update(self):
|
| 90 |
+
self.rect.x += self.speed_x
|
| 91 |
+
self.rect.y += self.speed_y
|
| 92 |
+
if self.rect.left <= 0 or self.rect.right >= SCREEN_WIDTH:
|
| 93 |
+
self.speed_x *= -1
|
| 94 |
+
|
| 95 |
+
if self.rect.top <= 0:
|
| 96 |
+
self.speed_y *= -1
|
| 97 |
+
|
| 98 |
+
class PowerUp(pygame.sprite.Sprite):
|
| 99 |
+
def __init__(self, x, y, color):
|
| 100 |
+
super().__init__()
|
| 101 |
+
self.color = color
|
| 102 |
+
self.image = pygame.Surface((15,15))
|
| 103 |
+
self.image.fill(self.color)
|
| 104 |
+
self.rect = self.image.get_rect()
|
| 105 |
+
self.rect.centerx = x
|
| 106 |
+
self.rect.centery = y
|
| 107 |
+
self.speed_y = 3
|
| 108 |
+
|
| 109 |
+
def update(self):
|
| 110 |
+
self.rect.y += self.speed_y
|
| 111 |
+
if self.rect.top > SCREEN_HEIGHT:
|
| 112 |
+
self.kill()
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
class Brick(pygame.sprite.Sprite):
|
| 117 |
+
def __init__(self, x, y, color, points):
|
| 118 |
+
super().__init__()
|
| 119 |
+
self.points = points
|
| 120 |
+
self.color = color
|
| 121 |
+
self.image_normal = pygame.transform.scale(pygame.image.load(f"assets/brick_{self.color}.png").convert_alpha(), (BRICK_NEW_WIDTH, BRICK_NEW_HEIGHT))
|
| 122 |
+
self.image_cracked = pygame.transform.scale(pygame.image.load(f"assets/brick_{self.color}_cracked.png").convert_alpha(), (BRICK_NEW_WIDTH, BRICK_NEW_HEIGHT))
|
| 123 |
+
|
| 124 |
+
self.image = self.image_normal
|
| 125 |
+
self.rect = self.image.get_rect()
|
| 126 |
+
self.rect.x = x
|
| 127 |
+
self.rect.y = y
|
| 128 |
+
self.health = 2
|
| 129 |
+
|
| 130 |
+
self.all_sprites_group = None
|
| 131 |
+
self.powerups_group = None
|
| 132 |
+
|
| 133 |
+
def hit(self):
|
| 134 |
+
self.health -= 1
|
| 135 |
+
if self.health <= 0:
|
| 136 |
+
if random.randint(1,100) <= 20:
|
| 137 |
+
new_powerup = PowerUp(self.rect.x, self.rect.y, random.choice([YELLOW, RED]))
|
| 138 |
+
self.all_sprites_group.add(new_powerup)
|
| 139 |
+
self.powerups_group.add(new_powerup)
|
| 140 |
+
self.kill()
|
| 141 |
+
return self.points
|
| 142 |
+
else:
|
| 143 |
+
self.image = self.image_cracked
|
| 144 |
+
return 0
|
| 145 |
+
|
| 146 |
+
def game_loop():
|
| 147 |
+
pygame.init()
|
| 148 |
+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
| 149 |
+
pygame.display.set_caption("打磚塊遊戲")
|
| 150 |
+
clock = pygame.time.Clock()
|
| 151 |
+
|
| 152 |
+
game_state = "start"
|
| 153 |
+
score = 0
|
| 154 |
+
|
| 155 |
+
all_sprites = pygame.sprite.Group()
|
| 156 |
+
bricks = pygame.sprite.Group()
|
| 157 |
+
powerups = pygame.sprite.Group()
|
| 158 |
+
paddle = Paddle()
|
| 159 |
+
ball = Ball()
|
| 160 |
+
|
| 161 |
+
def create_brick_wall():
|
| 162 |
+
bricks.empty()
|
| 163 |
+
powerups.empty()
|
| 164 |
+
all_sprites.empty()
|
| 165 |
+
all_sprites.add(paddle)
|
| 166 |
+
all_sprites.add(ball)
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
BRICK_COLORS = ["red", "orange", "yellow", "green", "cyan", "blue", "purple"]
|
| 170 |
+
for row, color in enumerate(BRICK_COLORS):
|
| 171 |
+
points = (7 - row) * 10
|
| 172 |
+
for column in range(SCREEN_WIDTH // (BRICK_NEW_WIDTH + 5)):
|
| 173 |
+
brick = Brick(
|
| 174 |
+
column * (BRICK_NEW_WIDTH + 5) + 10,
|
| 175 |
+
row * (BRICK_NEW_HEIGHT + 10) + 50,
|
| 176 |
+
color,
|
| 177 |
+
points
|
| 178 |
+
)
|
| 179 |
+
all_sprites.add(brick)
|
| 180 |
+
bricks.add(brick)
|
| 181 |
+
|
| 182 |
+
brick.all_sprites_group = all_sprites
|
| 183 |
+
brick.powerups_group = powerups
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
running = True
|
| 188 |
+
while running:
|
| 189 |
+
clock.tick(60)
|
| 190 |
+
|
| 191 |
+
for event in pygame.event.get():
|
| 192 |
+
if event.type == pygame.QUIT:
|
| 193 |
+
running = False
|
| 194 |
+
if event.type == pygame.KEYDOWN:
|
| 195 |
+
if game_state == "start":
|
| 196 |
+
game_state = "playing"
|
| 197 |
+
score = 0
|
| 198 |
+
paddle.rect.x = (SCREEN_WIDTH - paddle.rect.width) // 2
|
| 199 |
+
paddle.rect.y = SCREEN_HEIGHT - paddle.rect.height - 10
|
| 200 |
+
ball.reset()
|
| 201 |
+
create_brick_wall()
|
| 202 |
+
elif game_state == "game_over":
|
| 203 |
+
game_state = "start"
|
| 204 |
+
|
| 205 |
+
if game_state == "start":
|
| 206 |
+
screen.fill(BLACK)
|
| 207 |
+
draw_text(screen, "BREAKOUT", 64, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4)
|
| 208 |
+
draw_text(screen, "Press any key to start", 40, SCREEN_WIDTH / 2, SCREEN_HEIGHT * 3 / 4)
|
| 209 |
+
pygame.display.flip()
|
| 210 |
+
elif game_state == "playing":
|
| 211 |
+
keys = pygame.key.get_pressed()
|
| 212 |
+
paddle.move(keys)
|
| 213 |
+
all_sprites.update()
|
| 214 |
+
|
| 215 |
+
if pygame.sprite.collide_rect(ball, paddle):
|
| 216 |
+
ball.speed_y *= -1
|
| 217 |
+
brick_hit_list = pygame.sprite.spritecollide(ball, bricks, False)
|
| 218 |
+
if brick_hit_list:
|
| 219 |
+
ball.speed_y *= -1
|
| 220 |
+
for brick in brick_hit_list:
|
| 221 |
+
score += brick.hit()
|
| 222 |
+
powerups_hit_list = pygame.sprite.spritecollide(paddle, powerups, False)
|
| 223 |
+
for _ in powerups_hit_list:
|
| 224 |
+
if _.color == YELLOW:
|
| 225 |
+
paddle.widden()
|
| 226 |
+
score += 50
|
| 227 |
+
elif _.color == RED:
|
| 228 |
+
ball.speed_y *= 0.3
|
| 229 |
+
ball.speed_x *= 3
|
| 230 |
+
score += 50
|
| 231 |
+
_.kill()
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
if ball.rect.top >= SCREEN_HEIGHT:
|
| 235 |
+
game_state = "game_over"
|
| 236 |
+
if not bricks:
|
| 237 |
+
game_state = "game_over"
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
screen.fill(BLACK)
|
| 241 |
+
all_sprites.draw(screen)
|
| 242 |
+
draw_text(screen, f"Score: {score}", 24, SCREEN_WIDTH / 2, 10)
|
| 243 |
+
pygame.display.flip()
|
| 244 |
+
|
| 245 |
+
elif game_state == "game_over":
|
| 246 |
+
screen.fill(BLACK)
|
| 247 |
+
draw_text(screen, "GAME OVER", 64, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4)
|
| 248 |
+
draw_text(screen, f"Final Score: {score}", 50, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4 - 50)
|
| 249 |
+
draw_text(screen, "Press any key to restart", 40, SCREEN_WIDTH / 2, SCREEN_HEIGHT * 3 / 4)
|
| 250 |
+
pygame.display.flip()
|
| 251 |
+
|
| 252 |
+
pygame.quit()
|
| 253 |
+
|
| 254 |
+
if __name__ == "__main__":
|
| 255 |
+
game_loop()
|
| 256 |
+
#| |
|
| 257 |
+
# ˇ
|
projects/DEMO/Student1/Pygame乒乓/main.py
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
| 4 |
+
|
| 5 |
+
SCREEN_WIDTH = 900
|
| 6 |
+
SCREEN_HEIGHT = 600
|
| 7 |
+
BG_COLOR = pygame.Color("#00D000")
|
| 8 |
+
PADDLE_COLOR = pygame.Color("#FF0000")
|
| 9 |
+
PADDLE_WIDTH = 10
|
| 10 |
+
PADDLE_HEIGHT = 100
|
| 11 |
+
BALL_COLOR = pygame.Color(255, 144, 0)
|
| 12 |
+
x = 144
|
| 13 |
+
BALL_SIZE = 15
|
| 14 |
+
LINE_COLOR = pygame.Color("#FFFFFF")
|
| 15 |
+
TEXT_COLOR = pygame.Color("#FFFFFF")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
title_font = pygame.font.Font(None, 64)
|
| 19 |
+
game_font = pygame.font.Font(None, 48)
|
| 20 |
+
prompt_font = pygame.font.Font(None, 32)
|
| 21 |
+
paddle1 = pygame.Rect(20, SCREEN_HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
|
| 22 |
+
paddle2 = pygame.Rect(SCREEN_WIDTH - 20 - PADDLE_WIDTH, SCREEN_HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
|
| 23 |
+
ball = pygame.Rect(SCREEN_WIDTH // 2 - BALL_SIZE // 2, SCREEN_HEIGHT // 2 - BALL_SIZE // 2, BALL_SIZE, BALL_SIZE)
|
| 24 |
+
player_1_score = 0
|
| 25 |
+
player_2_score = 0
|
| 26 |
+
|
| 27 |
+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
| 28 |
+
pygame.display.set_caption("Pong by Pygame")
|
| 29 |
+
clock = pygame.time.Clock()
|
| 30 |
+
|
| 31 |
+
ball_moving = False
|
| 32 |
+
running = True
|
| 33 |
+
game_start = False
|
| 34 |
+
game_over = False
|
| 35 |
+
deuce = False
|
| 36 |
+
winner_text = ""
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
while running:
|
| 40 |
+
for event in pygame.event.get():
|
| 41 |
+
if event.type == pygame.QUIT:
|
| 42 |
+
running = False
|
| 43 |
+
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
|
| 44 |
+
if not ball_moving and not game_over:
|
| 45 |
+
ball_moving = True
|
| 46 |
+
if not deuce :
|
| 47 |
+
if (player_1_score + player_2_score) % 4 == 0 or (player_1_score + player_2_score) % 4 == 1:
|
| 48 |
+
ball_speed_x = 7
|
| 49 |
+
else:
|
| 50 |
+
ball_speed_x = -7
|
| 51 |
+
else:
|
| 52 |
+
if (player_1_score + player_2_score) % 2 == 0:
|
| 53 |
+
ball_speed_x = 7
|
| 54 |
+
else:
|
| 55 |
+
ball_speed_x = -7
|
| 56 |
+
ball_speed_y = 7*random.choice((1, -1))
|
| 57 |
+
|
| 58 |
+
if game_over:
|
| 59 |
+
game_over = False
|
| 60 |
+
winner_text = ""
|
| 61 |
+
player_1_score = 0
|
| 62 |
+
player_2_score = 0
|
| 63 |
+
ball_moving = False
|
| 64 |
+
ball.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
|
| 65 |
+
if not game_start:
|
| 66 |
+
game_start = True
|
| 67 |
+
|
| 68 |
+
keys = pygame.key.get_pressed()
|
| 69 |
+
if keys[pygame.K_w]:
|
| 70 |
+
paddle1.y -= 10
|
| 71 |
+
if keys[pygame.K_s]:
|
| 72 |
+
paddle1.y += 10
|
| 73 |
+
if keys[pygame.K_UP]:
|
| 74 |
+
paddle2.y -= 10
|
| 75 |
+
if keys[pygame.K_DOWN]:
|
| 76 |
+
paddle2.y += 10
|
| 77 |
+
screen.fill(BG_COLOR)
|
| 78 |
+
pygame.draw.rect(screen, PADDLE_COLOR, paddle1)
|
| 79 |
+
pygame.draw.rect(screen, PADDLE_COLOR, paddle2)
|
| 80 |
+
pygame.draw.ellipse(screen, BALL_COLOR, ball)
|
| 81 |
+
pygame.draw.aaline(screen, LINE_COLOR, (SCREEN_WIDTH // 2, 0), (SCREEN_WIDTH // 2, SCREEN_HEIGHT))
|
| 82 |
+
|
| 83 |
+
text1 = game_font.render(f"{player_1_score}", True, TEXT_COLOR)
|
| 84 |
+
p1_rect = text1.get_rect(topright = (SCREEN_WIDTH // 2 - 60, 20))
|
| 85 |
+
screen.blit(text1, p1_rect)
|
| 86 |
+
text2 = game_font.render(f"{player_2_score}", True, TEXT_COLOR)
|
| 87 |
+
p2_rect = text2.get_rect(topleft = (SCREEN_WIDTH // 2 + 60, 20))
|
| 88 |
+
screen.blit(text2, p2_rect)
|
| 89 |
+
|
| 90 |
+
if not game_start:#開始畫面
|
| 91 |
+
title_surface = title_font.render("Pong Game", True, TEXT_COLOR)
|
| 92 |
+
title_rect = title_surface.get_rect(center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 - 100))
|
| 93 |
+
|
| 94 |
+
prompt_surface = prompt_font.render("Press Space to Start", True, TEXT_COLOR)
|
| 95 |
+
prompt_rect = prompt_rect = prompt_surface.get_rect(center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
|
| 96 |
+
|
| 97 |
+
screen.blit(title_surface, title_rect)
|
| 98 |
+
screen.blit(prompt_surface, prompt_rect)
|
| 99 |
+
|
| 100 |
+
pong_img = pygame.image.load("pong.png")
|
| 101 |
+
pong_img = pygame.transform.scale(pong_img, (100, 100))
|
| 102 |
+
screen.blit(pong_img, (SCREEN_WIDTH // 2 - pong_img.get_width() // 2, SCREEN_HEIGHT//2+40))
|
| 103 |
+
if not ball_moving and game_start and not game_over:
|
| 104 |
+
prompt_surface = prompt_font.render("Press Space to Serve", True, TEXT_COLOR)
|
| 105 |
+
prompt_rect = prompt_surface.get_rect(center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 50))
|
| 106 |
+
screen.blit(prompt_surface, prompt_rect)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
if ball_moving:
|
| 110 |
+
ball.x += ball_speed_x
|
| 111 |
+
ball.y += ball_speed_y
|
| 112 |
+
if ball.top <= 0 or ball.bottom >= SCREEN_HEIGHT:
|
| 113 |
+
ball_speed_y *= -1
|
| 114 |
+
if ball.colliderect(paddle1) or ball.colliderect(paddle2):
|
| 115 |
+
ball_speed_x *= -1.05
|
| 116 |
+
ball_speed_y *= 1.05
|
| 117 |
+
if x > 0:
|
| 118 |
+
x -= x/10
|
| 119 |
+
BALL_COLOR = (255, x, 0)
|
| 120 |
+
if ball.left <= 0:
|
| 121 |
+
player_2_score += 1
|
| 122 |
+
x = 144
|
| 123 |
+
if deuce and player_2_score - player_1_score >= 2:
|
| 124 |
+
winner_text = "Player 2 Wins !"
|
| 125 |
+
game_over = True
|
| 126 |
+
deuce = False
|
| 127 |
+
if not deuce and player_2_score >= 11:
|
| 128 |
+
winner_text = "Player 2 Wins !"
|
| 129 |
+
game_over = True
|
| 130 |
+
deuce = False
|
| 131 |
+
if not deuce and player_1_score >= 10 and player_2_score >= 10:
|
| 132 |
+
deuce = True
|
| 133 |
+
ball.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
|
| 134 |
+
ball_moving = False
|
| 135 |
+
if ball.right >= SCREEN_WIDTH:
|
| 136 |
+
player_1_score += 1
|
| 137 |
+
x = 144
|
| 138 |
+
if deuce and player_1_score - player_2_score >= 2:
|
| 139 |
+
winner_text = "Player 1 Wins !"
|
| 140 |
+
game_over = True
|
| 141 |
+
deuce = False
|
| 142 |
+
if not deuce and player_1_score >= 11:
|
| 143 |
+
winner_text = "Player 1 Wins !"
|
| 144 |
+
game_over = True
|
| 145 |
+
if not deuce and player_1_score >= 10 and player_2_score >= 10:
|
| 146 |
+
deuce = True
|
| 147 |
+
deuce = False
|
| 148 |
+
ball.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
|
| 149 |
+
ball_moving = False
|
| 150 |
+
if game_over:
|
| 151 |
+
game_over_surface = title_font.render(winner_text, True, TEXT_COLOR)
|
| 152 |
+
game_over_rect = game_over_surface.get_rect(center = (SCREEN_WIDTH // 2 , SCREEN_HEIGHT // 2 -50))
|
| 153 |
+
screen.blit(game_over_surface, game_over_rect)
|
| 154 |
+
|
| 155 |
+
restart_surface = prompt_font.render("Press Space to Restart", True, TEXT_COLOR)
|
| 156 |
+
restart_rect = restart_surface.get_rect(center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 20))
|
| 157 |
+
screen.blit(restart_surface, restart_rect)
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
if paddle1.top <= 0:
|
| 161 |
+
paddle1.top = 0
|
| 162 |
+
if paddle1.bottom >= SCREEN_HEIGHT:
|
| 163 |
+
paddle1.bottom = SCREEN_HEIGHT
|
| 164 |
+
if paddle2.top <= 0:
|
| 165 |
+
paddle2.top = 0
|
| 166 |
+
if paddle2.bottom >= SCREEN_HEIGHT:
|
| 167 |
+
paddle2.bottom = SCREEN_HEIGHT
|
| 168 |
+
pygame.display.flip()
|
| 169 |
+
clock.tick(60)
|
| 170 |
+
|
| 171 |
+
pygame.quit()
|
| 172 |
+
sys.exit()
|