- 猜数字游戏
代码语言:javascript
复制
python
import random
number = random.randint(1, 100)
guess = int(input('请猜一个1到100之间的数字:'))
while guess != number:
if guess > number:
print('猜大了')
else:
print('猜小了')
guess = int(input('请重新猜一个1到100之间的数字:'))
print('恭喜你,猜对了!')
- 石头剪刀布游戏
代码语言:javascript
复制
python
import random
options = ['石头', '剪刀', '布']
computer_choice = random.choice(options)
user_choice = input('请出拳(石头、剪刀、布):')
print('你出了', user_choice)
print('电脑出了', computer_choice)
if user_choice == computer_choice:
print('平局')
elif user_choice == '石头' and computer_choice == '剪刀' or \
user_choice == '剪刀' and computer_choice == '布' or \
user_choice == '布' and computer_choice == '石头':
print('你赢了')
else:
print('你输了')
- 猜单词游戏
代码语言:javascript
复制
python
import random
words = ['apple', 'banana', 'cherry', 'orange', 'pear']
word = random.choice(words)
letters = set(word)
correct_guesses = set()
incorrect_guesses = set()
tries = 6
while tries > 0 and letters != correct_guesses:
print('可用次数:', tries)
print('已猜对的字母:', ' '.join(correct_guesses))
print('已猜错的字母:', ' '.join(incorrect_guesses))
guess = input('请猜一个字母:')
if guess in letters:
correct_guesses.add(guess)
else:
incorrect_guesses.add(guess)
tries -= 1
if letters == correct_guesses:
print('恭喜你,猜对了!单词是', word)
else:
print('很遗憾,你没有猜对。单词是', word)
- 猜词游戏
代码语言:javascript
复制
python
import random
words = ['apple', 'banana', 'cherry', 'orange', 'pear']
word = random.choice(words)
shuffle_word = list(word)
random.shuffle(shuffle_word)
print('请猜这个单词:', ''.join(shuffle_word))
guess = input('请输入你的答案:')
if guess == word:
print('恭喜你,猜对了!')
else:
print('很遗憾,你没有猜对。正确答案是', word)
5.数字连连看游戏
代码语言:javascript
复制
python
import random
numbers = list(range(1, 9)) * 2
random.shuffle(numbers)
def display_board():
for i in range(0, 16, 4):
print(numbers[i:i+4])
def remove_numbers(x1, y1, x2, y2):
global numbers
numbers[x1*4+y1] = 0
numbers[x2*4+y2] = 0
def is_valid_move(x1, y1, x2, y2):
if x1 == x2 and abs(y1-y2) == 1:
return True
if y1 == y2 and abs(x1-x2) == 1:
return True
return False
display_board()
while True:
x1 = int(input('请输入第一个数字的行号(0-3):'))
y1 = int(input('请输入第一个数字的列号(0-3):'))
x2 = int(input('请输入第二个数字的行号(0-3):'))
y2 = int(input('请输入第二个数字的列号(0-3):'))
if is_valid_move(x1, y1, x2, y2) and numbers[x1*4+y1] == numbers[x2*4+y2]:
remove_numbers(x1, y1, x2, y2)
display_board()
if all(num == 0 for num in numbers):
print('恭喜你,游戏结束!')
break
else:
print('无效的移动,请重新输入。')
- 猜单词游戏(升级版)
代码语言:javascript
复制
python
import random
words = ['apple', 'banana', 'cherry', 'orange', 'pear']
word = random.choice(words)
letters = set(word)
correct_guesses = set()
incorrect_guesses = set()
tries = 6
while tries > 0 and letters != correct_guesses:
print('可用次数:', tries)
print('已猜对的字母:', ' '.join(correct_guesses))
print('已猜错的字母:', ' '.join(incorrect_guesses))
guess = input('请猜一个字母或整个单词:')
if len(guess) == 1:
if guess in letters:
correct_guesses.add(guess)
else:
incorrect_guesses.add(guess)
tries -= 1
else:
if guess == word:
correct_guesses = letters
else:
incorrect_guesses.add(guess)
tries -= 1
if letters == correct_guesses:
print('恭喜你,猜对了!单词是', word)
else:
print('很遗憾,你没有猜对。正确答案是', word)
- 猜数字游戏(升级版)
代码语言:javascript
复制
python
import random
number = random.randint(1, 100)
guesses = []
while len(guesses) < 10:
guess = int(input('请猜一个1到100之间的数字:'))
if guess in guesses:
print('你已经猜过这个数字了,请重新输入。')
continue
guesses.append(guess)
if guess == number:
print('恭喜你,猜对了!你用了', len(guesses), '次猜中了这个数字。')
break
elif guess < number:
print('猜小了')
else:
print('猜大了')
else:
print('很遗憾,你没有在规定次数内猜对。正确答案是', number)
这个游戏中,玩家需要在10次之内猜出一个1到100之间的随机数字。每次猜测后,程序会告诉玩家猜的数字是偏大还是偏小,直到玩家猜中这个数字或者用完所有的猜测次数为止。如果玩家在规定次数内猜中了这个数字,则游戏胜利;否则游戏失败。
- 猜拳游戏(升级版)
代码语言:javascript
复制
python
import random
options = ['石头', '剪刀', '布']
wins = {'石头': '剪刀', '剪刀': '布', '布': '石头'}
computer_score = 0
player_score = 0
while True:
computer_choice = random.choice(options)
player_choice = input('请出拳(石头、剪刀、布):')
print('你出了', player_choice)
print('电脑出了', computer_choice)
if player_choice == computer_choice:
print('平局')
elif wins[player_choice] == computer_choice:
print('你赢了')
player_score += 1
else:
print('你输了')
computer_score += 1
print('当前得分:电脑', computer_score, ', 玩家', player_score)
if computer_score == 3 or player_score == 3:
break
if computer_score > player_score:
print('很遗憾,你输了。')
else:
print('恭喜你,你赢了!')
这个游戏中,玩家和电脑进行猜拳比赛,先达到3分的一方获胜。玩家每次可以选择石头、剪刀或布中的一种,电脑也会随机选择其中的一种。根据石头、剪刀、布之间的胜负关系,程序会判断哪一方获胜,并更新双方的得分。当有一方的得分达到3分时,游戏结束,获得3分的一方获胜。
9.俄罗斯方块
代码语言:javascript
复制
# 导入必要的库
import pygame
import random
# 初始化pygame
pygame.init()
# 定义常量
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BLOCK_SIZE = 30
FONT_SIZE = 36
FPS = 60
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
# 定义方块形状
SHAPES = [
[[1, 1, 1], [0, 1, 0]],
[[2, 2], [2, 2]],
[[0, 3, 3], [3, 3, 0]],
[[4, 4, 0], [0, 4, 4]],
[[5, 5, 5, 5]],
[[6, 6, 6], [0, 0, 6]],
[[7, 7, 7], [7, 0, 0]],
]
# 定义方块颜色
COLORS = [
BLUE,
YELLOW,
GREEN,
RED,
WHITE,
(255, 165, 0),
(128, 0, 128),
]
# 定义方块类
class Block:
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.shape = shape
self.color