如何随意生成墙的代码,并且撞墙就游戏结束的代码
时间: 2024-02-13 14:05:17 浏览: 22
这里提供一个用Python和Pygame实现的简单游戏,其中包括随机生成墙和碰撞检测的代码。这个游戏中,玩家可以通过方向键控制一个小球避开随机生成的墙,碰到墙则游戏结束。
```python
import pygame
import random
# 定义常量
WIDTH = 800
HEIGHT = 600
BALL_RADIUS = 10
WALL_WIDTH = 20
WALL_MIN_HEIGHT = 100
WALL_MAX_HEIGHT = 300
WALL_MIN_DISTANCE = 100
WALL_MAX_DISTANCE = 200
# 初始化Pygame
pygame.init()
# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# 设置游戏标题
pygame.display.set_caption("Hit the Wall")
# 定义颜色常量
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# 定义字体
font = pygame.font.SysFont(None, 48)
# 定义游戏结束函数
def game_over():
# 显示游戏结束的文字
text = font.render("GAME OVER", True, WHITE)
text_rect = text.get_rect(center=(WIDTH/2, HEIGHT/2))
screen.blit(text, text_rect)
pygame.display.flip()
# 等待3秒钟后关闭游戏窗口
pygame.time.wait(3000)
pygame.quit()
exit()
# 定义碰撞检测函数
def collide(rect1, rect2):
if rect1.right < rect2.left or rect1.left > rect2.right:
return False
if rect1.bottom < rect2.top or rect1.top > rect2.bottom:
return False
return True
# 随机生成墙的函数
def create_wall(x):
# 随机生成墙的高度和距离
wall_height = random.randint(WALL_MIN_HEIGHT, WALL_MAX_HEIGHT)
wall_distance = random.randint(WALL_MIN_DISTANCE, WALL_MAX_DISTANCE)
# 创建墙的矩形
wall_rect = pygame.Rect(x, 0, WALL_WIDTH, wall_height)
# 创建墙的间隔
gap_rect = pygame.Rect(x-WALL_WIDTH/2, wall_height, WALL_WIDTH*2, wall_distance)
return wall_rect, gap_rect
# 定义游戏循环
def game_loop():
# 初始化小球的位置和速度
ball_x = WIDTH/2
ball_y = HEIGHT/2
ball_vx = 0
ball_vy = 0
# 初始化墙的列表和起始位置
walls = []
wall_x = WIDTH
# 记录分数
score = 0
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
ball_vy = -5
elif event.key == pygame.K_DOWN:
ball_vy = 5
elif event.key == pygame.K_LEFT:
ball_vx = -5
elif event.key == pygame.K_RIGHT:
ball_vx = 5
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
ball_vy = 0
elif event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
ball_vx = 0
# 移动小球
ball_x += ball_vx
ball_y += ball_vy
# 绘制背景
screen.fill(BLACK)
# 绘制小球
pygame.draw.circle(screen, WHITE, (int(ball_x), int(ball_y)), BALL_RADIUS)
# 遍历墙列表,移动墙和间隔,并绘制墙和间隔
for wall, gap in walls:
wall.left -= 5
gap.left -= 5
pygame.draw.rect(screen, WHITE, wall)
pygame.draw.rect(screen, BLACK, gap)
# 检测小球是否碰到墙或间隔
if collide(pygame.Rect(ball_x-BALL_RADIUS, ball_y-BALL_RADIUS, BALL_RADIUS*2, BALL_RADIUS*2), wall) or \
collide(pygame.Rect(ball_x-BALL_RADIUS, ball_y-BALL_RADIUS, BALL_RADIUS*2, BALL_RADIUS*2), gap):
game_over()
# 如果小球通过了墙和间隔,加分并移除墙
if wall.right < ball_x and not gap.collidepoint(wall.right, wall.bottom+1):
score += 1
walls.remove((wall, gap))
# 如果最后一堵墙的右边界超过了屏幕左边界,创建新的墙
if walls and walls[-1][0].right < WIDTH-WALL_MIN_DISTANCE:
wall, gap = create_wall(WIDTH)
walls.append((wall, gap))
# 如果没有墙,创建新的墙
elif not walls:
wall, gap = create_wall(WIDTH)
walls.append((wall, gap))
# 绘制分数
score_text = font.render(str(score), True, WHITE)
screen.blit(score_text, (10, 10))
# 更新屏幕
pygame.display.flip()
# 设置帧率
pygame.time.Clock().tick(60)
# 启动游戏
game_loop()
```
这段代码中,`create_wall`函数用于随机生成墙和间隔,`collide`函数用于检测小球和墙或间隔是否碰撞,`game_over`函数用于在游戏结束时显示文字并关闭游戏窗口。在游戏循环中,首先处理用户的输入,然后移动小球并绘制背景和小球。然后遍历墙列表,移动墙和间隔,并绘制墙和间隔。每一次循环都检测小球是否碰到墙或间隔,如果碰到则调用`game_over`函数结束游戏。如果小球通过了墙和间隔,加分并移除墙。最后,根据墙列表的情况生成新的墙并绘制分数,然后更新屏幕并设置帧率。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)