帮我写一个基于python的小游戏代码
时间: 2023-08-28 15:02:37 浏览: 74
当然可以!以下是一个简单的基于Python的猜数字小游戏的示例代码:
```python
import random
def guess_number():
number = random.randint(1, 100)
guesses_taken = 0
while True:
guess = int(input("猜一个1到100之间的数字:"))
guesses_taken += 1
if guess < number:
print("猜小了!")
elif guess > number:
print("猜大了!")
else:
print(f"恭喜你,猜对了!你用了{guesses_taken}次猜测。")
break
guess_number()
```
这个游戏会随机生成一个1到100之间的数字,然后要求玩家猜测这个数字。根据玩家的猜测,程序会给出相应的提示,直到玩家猜对为止。
希望这个示例代码能帮到你!如果你有其他需求或者更多细节,请随时告诉我。
相关问题
帮我写一个300行的python小游戏
好的,我可以给您提供一个简单的贪吃蛇小游戏的代码,希望能够帮到您:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 设置游戏窗口参数
window_width = 800
window_height = 600
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("贪吃蛇")
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# 设置字体
font_style = pygame.font.SysFont(None, 50)
# 定义蛇的块大小和速度
block_size = 20
snake_speed = 10
# 定义函数用于显示消息
def message(msg, color):
message = font_style.render(msg, True, color)
window.blit(message, [window_width/6, window_height/3])
# 定义函数用于绘制蛇的身体
def draw_snake(block_size, snake_list):
for x in snake_list:
pygame.draw.rect(window, green, [x[0], x[1], block_size, block_size])
# 定义函数用于显示得分
def show_score(score):
score_font = font_style.render("得分: " + str(score), True, white)
window.blit(score_font, [0, 0])
# 定义函数用于运行游戏
def gameLoop():
# 设置游戏结束标志
game_over = False
# 初始化蛇的位置和长度
snake_list = []
snake_length = 1
# 初始化蛇的移动方向
x1 = window_width / 2
y1 = window_height / 2
x1_change = 0
y1_change = 0
# 初始化食物的位置
foodx = round(random.randrange(0, window_width - block_size) / 10.0) * 10.0
foody = round(random.randrange(0, window_height - block_size) / 10.0) * 10.0
# 运行游戏主循环
while not game_over:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -block_size
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = block_size
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -block_size
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = block_size
x1_change = 0
# 更新蛇的位置
x1 += x1_change
y1 += y1_change
# 绘制游戏背景
window.fill(black)
# 绘制食物
pygame.draw.rect(window, blue, [foodx, foody, block_size, block_size])
# 将蛇的位置保存到列表中
snake_head = []
snake_head.append(x1)
snake_head.append(y1)
snake_list.append(snake_head)
# 如果蛇超出窗口范围或者碰到自己,游戏结束
if x1 >= window_width or x1 < 0 or y1 >= window_height or y1 < 0:
game_over = True
for x in snake_list[:-1]:
if x == snake_head:
game_over = True
# 绘制蛇的身体
draw_snake(block_size, snake_list)
# 如果蛇吃到了食物,增加得分并重新生成食物
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, window_width - block_size) / 10.0) * 10.0
foody = round(random.randrange(0, window_height - block_size) / 10.0) * 10.0
snake_length += 1
# 如果蛇的长度超过一定值,删除列表中的第一个元素
if len(snake_list) > snake_length:
del snake_list[0]
# 显示得分
show_score(snake_length-1)
# 更新屏幕
pygame.display.update()
# 控制游戏帧率
clock.tick(snake_speed)
# 显示游戏结束消息
message("游戏结束!", red)
pygame.display.update()
pygame.time.sleep(2)
# 退出pygame
pygame.quit()
quit()
# 运行游戏
gameLoop()
```
这是一个基于 Pygame 模块实现的简单贪吃蛇小游戏,代码共计 109 行。您可以自行添加修改其中的功能,实现自己想要的游戏。
写一篇基于python的五子棋小游戏可视化代码
好的,以下是基于Python的五子棋小游戏可视化代码:
```
import pygame
# 定义棋盘大小和格子大小
BOARD_SIZE = 15
CELL_SIZE = 40
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (200, 200, 200)
# 初始化pygame
pygame.init()
# 创建游戏窗口
screen = pygame.display.set_mode((BOARD_SIZE * CELL_SIZE, BOARD_SIZE * CELL_SIZE))
pygame.display.set_caption("五子棋小游戏")
# 加载棋子图片
black_stone = pygame.image.load("black.png")
white_stone = pygame.image.load("white.png")
# 初始化棋盘数组
board = [[0 for i in range(BOARD_SIZE)] for j in range(BOARD_SIZE)]
# 判断是否下棋成功
def check_win(x, y, player):
# 横向判断
count = 0
for i in range(BOARD_SIZE):
if board[x][i] == player:
count += 1
if count == 5:
return True
else:
count = 0
# 竖向判断
count = 0
for i in range(BOARD_SIZE):
if board[i][y] == player:
count += 1
if count == 5:
return True
else:
count = 0
# 斜向判断
count = 0
for i in range(-4, 5):
if x + i < 0 or x + i >= BOARD_SIZE or y + i < 0 or y + i >= BOARD_SIZE:
continue
if board[x + i][y + i] == player:
count += 1
if count == 5:
return True
else:
count = 0
# 反斜向判断
count = 0
for i in range(-4, 5):
if x + i < 0 or x + i >= BOARD_SIZE or y - i < 0 or y - i >= BOARD_SIZE:
continue
if board[x + i][y - i] == player:
count += 1
if count == 5:
return True
else:
count = 0
return False
# 绘制棋盘和棋子
def draw_board():
# 绘制棋盘背景
screen.fill(GRAY)
# 绘制棋盘网格
for i in range(BOARD_SIZE):
pygame.draw.line(screen, BLACK, (0, i * CELL_SIZE), (BOARD_SIZE * CELL_SIZE, i * CELL_SIZE))
pygame.draw.line(screen, BLACK, (i * CELL_SIZE, 0), (i * CELL_SIZE, BOARD_SIZE * CELL_SIZE))
# 绘制棋子
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == 1:
screen.blit(black_stone, (i * CELL_SIZE, j * CELL_SIZE))
elif board[i][j] == 2:
screen.blit(white_stone, (i * CELL_SIZE, j * CELL_SIZE))
# 游戏主循环
def main():
# 初始化棋盘
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
board[i][j] = 0
# 初始化玩家
player = 1
# 游戏开始
while True:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
# 获取鼠标点击位置
x, y = event.pos[0] // CELL_SIZE, event.pos[1] // CELL_SIZE
# 判断下棋位置是否合法
if x < 0 or x >= BOARD_SIZE or y < 0 or y >= BOARD_SIZE:
continue
if board[x][y] != 0:
continue
# 下棋
board[x][y] = player
# 绘制棋盘和棋子
draw_board()
# 判断是否胜利
if check_win(x, y, player):
pygame.time.delay(200)
if player == 1:
print("黑方获胜!")
else:
print("白方获胜!")
# 重新开始游戏
pygame.time.delay(2000)
main()
# 切换玩家
player = 1 if player == 2 else 2
# 绘制棋盘和棋子
draw_board()
# 刷新屏幕
pygame.display.flip()
# 启动游戏
main()
```
以上代码实现了一个基本的五子棋游戏可视化,支持玩家与玩家对战,在窗口中可以进行操作和查看游戏状态。使用者可以根据需要进行修改和扩展。
相关推荐















