如果我想让底下绿色方块也移动躲避红色掉落 该怎么修改代码
时间: 2024-01-16 14:02:04 浏览: 18
要让底部的绿色方块也能移动,你可以在游戏循环中添加对底部方块移动的逻辑。下面是修改后的代码:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置窗口的尺寸
width = 800
height = 600
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("简单游戏")
# 设置颜色
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
# 设置游戏结束标志和分数
game_over = False
score = 0
# 设置玩家的初始位置和移动速度
player_x = width // 2
player_y = height - 50
player_speed = 5
# 设置底部方块的初始位置和移动速度
bottom_block_x = width // 2
bottom_block_y = height - 30
bottom_block_speed = 3
# 设置顶部方块的初始位置和移动速度
top_block_x = random.randint(0, width)
top_block_y = random.randint(50, 200)
top_block_speed = 3
# 游戏循环
while not game_over:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# 获取键盘按键状态
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_x -= player_speed
if keys[pygame.K_RIGHT]:
player_x += player_speed
# 更新玩家和方块的位置
bottom_block_x += bottom_block_speed
if bottom_block_x >= width or bottom_block_x <= 0:
bottom_block_speed *= -1
top_block_y += top_block_speed
if top_block_y > height:
top_block_x = random.randint(0, width)
top_block_y = random.randint(50, 200)
score += 1
# 检测碰撞
if (player_x < bottom_block_x + 50 and player_x + 50 > bottom_block_x and
player_y < bottom_block_y + 20 and player_y + 50 > bottom_block_y):
game_over = True
if (player_x < top_block_x + 20 and player_x + 50 > top_block_x and
player_y < top_block_y + 20 and player_y + 50 > top_block_y):
game_over = True
# 绘制背景和物体
window.fill(black)
pygame.draw.rect(window, green, (player_x, player_y, 50, 50))
pygame.draw.rect(window, red, (bottom_block_x, bottom_block_y, 50, 20))
pygame.draw.rect(window, red, (top_block_x, top_block_y, 20, 20))
# 绘制分数
font = pygame.font.Font(None, 30)
text = font.render("Score: " + str(score), True, white)
window.blit(text, (10, 10))
# 更新屏幕
pygame.display.update()
# 游戏结束后退出 Pygame
pygame.quit()
```
在代码中,我添加了一个底部方块(绿色方块)并设置了其初始位置和移动速度。在游戏循环中,我更新了底部方块的位置,并检测玩家方块与底部方块的碰撞。如果玩家方块与底部方块发生碰撞,游戏结束。
希望这次修改能满足你的需求!如果你还有其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)