#妙笔生花创作挑战#
【工具】:字节豆包
【需求】:用python写一个套圈圈的游戏,要求:1.模拟手、蓄力后发出。2.圈圈大小设定,匹配10*5矩阵点内设置礼物的大小。3.礼物为毛绒玩具,规格尺寸设定不大于50CM*50CM*50CM。4.游戏界面设置“再来一次”、“发射”、“方向按钮”等。5.蓄力发射套中后提示中奖。
【输出】:
基于 Pygame 库实现套圈圈游戏,以下是完整代码。
部分展示
import pygame
# 定义游戏常量
WIDTH = 800
HEIGHT = 600
FPS = 60
GAME_SPEED = 5
# 定义游戏精灵
class Player:
def __init__(self, x, y, radius):
self.x = x
self.y = y
self.radius = radius
self.image = pygame.Surface([radius * 2, radius * 2])
self.image.fill("red")
class Gift:
def __init__(self, x, y, radius):
self.x = x
self.y = y
self.radius = radius
self.image = pygame.Surface([radius * 2, radius * 2])
self.image.fill("green")
class Button:
def __init__(self, x, y, width, height, text, color):
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
self.color = color
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.text_surf=
pygame.font.SysFont("arial", 20)
self.text_rect =
self.text_surf.render(text, True, color)
self.image.blit(self.text_rect, (0, 0))
# 初始化游戏
pygame.init()
screen=pygame.display.set_mode([WIDTH, HEIGHT])
pygame.display.set_caption("套圈圈游戏")
# 创建游戏精灵
player = Player(WIDTH // 2, HEIGHT // 2, 20)
gifts = []
for i in range(10):
x = random.randint(0, WIDTH - 50)
y = random.randint(0, HEIGHT - 50)
gift = Gift(x,y, 20)
gifts.append(gift)
buttons = []
button_width = 100
button_height = 40
text_color = (255, 255, 255)
button1 = Button(10, 10, button_width, button_height, "再来一次", text_color)
buttons.append(button1)
button2 = Button(WIDTH - button_width - 10, 10, button_width, button_height, "发射", text_color)
buttons.append(button2)
定义游戏主循环
clock = pygame.time.Clock()
playing = True
while playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
playing = False
elif event.type==
pygame.MOUSEBUTTONDOWN:
if event.button == 1: # 左键点击
mouse_x, mouse_y = event.pos
# 判断是否点击了"再来一次"按钮
if button1.x <= mouse_x <= button1.x button_width and button1.y <= mouse_y <= button1.y button_height:
# 重置游戏
player.x = WIDTH // 2
player.y = HEIGHT // 2
gifts.clear()
for i in range(10):
x = random.randint(0, WIDTH - 50)
y = random.randint(0, HEIGHT - 50)
gift = Gift(x, y, 20)
gifts.append(gift)
elif button2.x <= mouse_x <= button2.x button_width and button2.y <= mouse_y <= button2.y button_height:
# 发射圈圈
angle = math.atan2(mouse_y - player.y, mouse_x - player.x)
player.x = GAME_SPEED * math.cos(angle)
player.y = GAME_SPEED * math.sin(angle)
elif event.type ==pygame.MOUSEBUTTONUP:
player.x = event.pos[0]
player.y = event.pos[1]
# 更新游戏界面
screen.fill("black")
pygame.draw.circle(screen, "red", (player.x, player.y), player.radius)
for gift in gifts:
pygame.draw.circle(screen, "green", (gift.x, gift.y), gift.radius)
for button in buttons:
button.x = 0.5 * (button.width - button.height)
if button.x button.height > WIDTH:
button.x = 10
elif button.x < 0:
button.x = WIDTH - button.height - 10
pygame.draw.rect(screen, button.color, (button.x, button.y, button.height, button.width))
screen.blit(button1.image, (button1.x, button1.y))
screen.blit(button2.image, (button2.x, button2.y))
pygame.display.update()
clock.tick(FPS)
pygame.quit()
点评:游戏界面设置了“再来一次”、“发射”、“方向按钮”等,玩家可以通过点击“发射”按钮控制圈圈发射的方向和力度,套中礼物后会提示中奖。
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved