游戏

小游戏,代码:


import pygame
import sys

# 初始化Pygame
pygame.init()

# 设置窗口尺寸
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Simple Game")

# 设置颜色
white = (255, 255, 255)
blue = (0, 0, 255)

# 小球的初始位置和速度

 


ball_radius = 50
ball_x, ball_y = width // 2, height // 2
ball_dx, ball_dy = 5, 5

# 游戏循环
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# 更新小球位置
ball_x += ball_dx
ball_y += ball_dy

# 边界碰撞检测
if ball_x + ball_radius > width or ball_x - ball_radius < 0:
ball_dx *= -1
if ball_y + ball_radius > height or ball_y - ball_radius < 0:
ball_dy *= -1

# 填充背景色
screen.fill(white)

# 画出小球
pygame.draw.circle(screen, blue, (ball_x, ball_y), ball_radius)

# 刷新屏幕
pygame.display.flip()

# 控制帧率
pygame.time.delay(30)

# 退出游戏
pygame.quit()
sys.exit()

posted @ 2023-12-17 22:50  小张真的不会打代码  阅读(53)  评论(0)    收藏  举报