请写一个简单游戏小程序
时间: 2023-12-19 19:04:17 浏览: 21
当然可以!下面是一个简单的猜数字游戏的小程序示例:
```python
import random
def guess_number():
secret_number = random.randint(1, 100)
attempts = 0
while True:
guess = int(input("猜一个1到100之间的数字:"))
attempts += 1
if guess < secret_number:
print("太小了,请再试一次!")
elif guess > secret_number:
print("太大了,请再试一次!")
else:
print("恭喜你,猜对了!")
print("你共猜了", attempts, "次。")
break
guess_number()
```
这个程序会随机生成一个1到100之间的秘密数字,然后让玩家通过输入猜测来猜出这个数字。程序会根据玩家的猜测给出相应的提示,直到玩家猜对为止。在猜对后,程序会告诉玩家共猜了多少次。
希望这个简单的游戏小程序能满足你的需求!如果有其他问题,请随时提问。
相关问题
写一个游戏小程序。
好的,下面是一个简单的猜数字游戏的小程序示例,它可以让用户猜一个随机生成的数字,直到猜中为止。
```python
import random
def play_game():
number = random.randint(1, 100)
guess = 0
count = 0
while guess != number:
guess = int(input('Guess the number (1-100): '))
count += 1
if guess < number:
print('Too low, try again.')
elif guess > number:
print('Too high, try again.')
else:
print('You got it! The number was', number)
print('It took you', count, 'tries.')
play_game()
```
运行这个程序,会提示用户输入一个数字来猜。如果猜的数字比实际数字小,则程序会提示“太小了,请再试一次”;如果猜的数字比实际数字大,则程序会提示“太大了,请再试一次”;如果猜中了,则程序会提示“你猜中了!”并显示猜了多少次。
这只是一个简单的示例,实际上,一个游戏小程序可能会更加复杂,例如增加更多的游戏规则、关卡、图形界面等。
用代码写一个贪吃蛇游戏小程序
好的,以下是一个简单的贪吃蛇游戏小程序的代码,使用 Python 语言编写:
```python
import pygame
import random
WIDTH = 500
HEIGHT = 500
BLOCK_SIZE = 20
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
class Snake:
def __init__(self):
self.body = [(WIDTH/2, HEIGHT/2)]
self.direction = (1, 0)
self.grow = False
def move(self):
head = self.body[0]
dx, dy = self.direction
new_head = (head[0] + dx*BLOCK_SIZE, head[1] + dy*BLOCK_SIZE)
self.body.insert(0, new_head)
if not self.grow:
self.body.pop()
else:
self.grow = False
def grow_snake(self):
self.grow = True
def draw(self, screen):
for block in self.body:
pygame.draw.rect(screen, (0, 255, 0), (block[0], block[1], BLOCK_SIZE, BLOCK_SIZE))
class Food:
def __init__(self):
self.pos = (random.randint(0, WIDTH-BLOCK_SIZE), random.randint(0, HEIGHT-BLOCK_SIZE))
def draw(self, screen):
pygame.draw.rect(screen, (255, 0, 0), (self.pos[0], self.pos[1], BLOCK_SIZE, BLOCK_SIZE))
snake = Snake()
food = Food()
def game_loop():
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and snake.direction != (0, 1):
snake.direction = (0, -1)
elif event.key == pygame.K_DOWN and snake.direction != (0, -1):
snake.direction = (0, 1)
elif event.key == pygame.K_LEFT and snake.direction != (1, 0):
snake.direction = (-1, 0)
elif event.key == pygame.K_RIGHT and snake.direction != (-1, 0):
snake.direction = (1, 0)
# move and grow snake
snake.move()
if snake.body[0] == food.pos:
snake.grow_snake()
food = Food()
# check for game over
head = snake.body[0]
if head[0] < 0 or head[0] >= WIDTH or head[1] < 0 or head[1] >= HEIGHT:
game_over = True
for block in snake.body[1:]:
if head == block:
game_over = True
# draw everything
screen.fill((0, 0, 0))
snake.draw(screen)
food.draw(screen)
pygame.display.flip()
# update clock
clock.tick(10)
game_loop()
pygame.quit()
```
请注意,这只是一个简单的贪吃蛇游戏小程序,它可能不够完善,需要根据实际需求来完善。如果您有任何问题或意见,请随时提出。