用python写一个简单好玩的小游戏
时间: 2023-08-04 16:31:13 浏览: 55
好的,我可以为你提供一个简单的 Python 游戏示例,名为“猜数字游戏”:
```
import random
# 生成随机数字
secret_number = random.randint(1, 100)
# 初始化猜测次数
guesses = 0
print("猜数字游戏:猜测 1~100 中的数字")
# 猜测循环
while guesses < 10:
# 获取用户输入
guess = int(input("请输入你的猜测:"))
guesses += 1
# 检查猜测是否正确
if guess == secret_number:
print("恭喜你,你猜对了!")
break
elif guess < secret_number:
print("你的猜测太低了。")
else:
print("你的猜测太高了。")
# 判断是否用完了10次机会仍未猜中
if guesses == 10:
print("很遗憾,你用完了所有机会。正确答案是:", secret_number)
```
这个游戏生成一个随机数字,然后让玩家在 1~100 中猜测数字,玩家有 10 次机会来猜测正确的数字。当玩家猜测正确或用完所有机会时,游戏结束。你可以根据自己的需求进行修改和扩展,例如增加难度、计分等等。
相关问题
用python写一个简单好玩的小游戏,要有界面
好,我可以为你提供一个基于 Python Tkinter 模块的简单游戏示例,名为“打地鼠游戏”:
```
import tkinter as tk
import random
# 创建主窗口
root = tk.Tk()
root.title("打地鼠游戏")
# 设置窗口大小
WIDTH = 400
HEIGHT = 400
root.geometry(f"{WIDTH}x{HEIGHT}")
# 创建游戏区域
canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT)
canvas.pack()
# 定义地鼠类
class Mole:
def __init__(self):
self.x = random.randint(50, WIDTH-50)
self.y = random.randint(50, HEIGHT-50)
self.radius = 30
self.color = "brown"
self.speed = random.randint(1000, 2000)
self.draw()
def draw(self):
self.shape = canvas.create_oval(self.x-self.radius, self.y-self.radius,
self.x+self.radius, self.y+self.radius,
fill=self.color)
def hide(self):
canvas.delete(self.shape)
# 定义游戏函数
def start_game():
global score, moles
score = 0
moles = []
for i in range(10):
moles.append(Mole())
update_score()
root.after(30000, end_game)
def end_game():
global score, moles
for mole in moles:
mole.hide()
canvas.create_text(WIDTH/2, HEIGHT/2, text=f"游戏结束!得分:{score}", font=("Arial", 24))
def update_score():
canvas.delete("score")
canvas.create_text(50, 20, text=f"得分:{score}", font=("Arial", 16), anchor="nw", tag="score")
def hit(event):
global score, moles
for mole in moles:
if abs(event.x-mole.x) < mole.radius and abs(event.y-mole.y) < mole.radius:
score += 10
update_score()
mole.hide()
moles.remove(mole)
moles.append(Mole())
# 绑定事件
canvas.bind("<Button-1>", hit)
# 创建开始按钮
button = tk.Button(root, text="开始游戏", command=start_game, font=("Arial", 20))
canvas.create_window(WIDTH/2, HEIGHT/2, window=button)
# 运行主程序
root.mainloop()
```
这个游戏创建一个主窗口,包含一个游戏区域和一个“开始游戏”按钮。游戏区域随机生成 10 个地鼠,玩家需要在 30 秒内点击地鼠来得分。当时间到或者玩家点击了所有地鼠后,游戏结束,显示得分。你可以根据自己的需求进行修改和扩展,例如增加难度、音效等等。
用python写一个好玩的小游戏
这里给出一个基于Python Turtle模块的小游戏,叫做“躲避球”(Dodgeball)。
游戏规则:
1. 控制一个小球躲避落下的球。
2. 落下的球速度逐渐加快,难度逐渐增加。
3. 如果小球被击中,游戏结束,显示得分。
代码如下:
```python
import turtle
import random
# 设置窗口大小和标题
wn = turtle.Screen()
wn.title("Dodgeball")
wn.bgcolor("black")
wn.setup(width=600, height=600)
# 创建玩家小球
player = turtle.Turtle()
player.shape("circle")
player.color("white")
player.penup()
# 设置落下的球列表和速度
balls = []
ball_speed = 1
# 创建落下的球
def create_ball():
ball = turtle.Turtle()
ball.shape("circle")
ball.color("red")
ball.penup()
ball.goto(random.randint(-280, 280), 280)
balls.append(ball)
# 移动玩家小球
def move_left():
x = player.xcor()
x -= 20
player.setx(x)
def move_right():
x = player.xcor()
x += 20
player.setx(x)
# 绑定键盘事件
wn.listen()
wn.onkeypress(move_left, "Left")
wn.onkeypress(move_right, "Right")
# 循环游戏
score = 0
while True:
# 创建落下的球
if random.randint(1, 10) == 1:
create_ball()
# 移动落下的球
for ball in balls:
y = ball.ycor()
y -= ball_speed
ball.sety(y)
# 判断是否击中玩家小球
if ball.distance(player) < 20:
wn.bgcolor("red")
player.hideturtle()
ball.hideturtle()
print("Game Over! Score:", score)
wn.exitonclick()
# 判断是否落到底部
if y < -280:
ball.hideturtle()
balls.remove(ball)
# 加速落下的球
ball_speed += 0.001
score += 1
```
运行代码,就可以开始玩“躲避球”小游戏啦!