用户:siaet_dived查看:2 回复:3 评论:2 创建时间:2023-05-05T18:35:13
import pygame
import random
pygame.init()
# 游戏窗口大小和标题
win_size = (600, 400)
screen = pygame.display.set_mode(win_size)
pygame.display.set_caption('Greedy Snake')
# 颜色常量
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 蛇和食物
snake_body = [{'x': 200, 'y': 200}, {'x': 190, 'y': 200}, {'x': 180, 'y': 200}]
food_pos = {'x': random.randrange(0, win_size[0], 10), 'y': random.randrange(0, win_size[1], 10)}
food_exist = True
# 分数
score = 0
font = pygame.font.SysFont(None, 25)
# 方向
direction = 'RIGHT'
change_to = direction
# 移动函数
def move_snake():
global direction, change_to, snake_body, food_pos, food_exist, score
# 检测方向
if change_to == 'UP' and direction != 'DOWN':
direction = 'UP'
elif change_to == 'DOWN' and direction != 'UP':
direction = 'DOWN'
elif change_to == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
elif change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
# 更新蛇头位置
if direction == 'UP':
new_head = {'x': snake_body[0]['x'], 'y': snake_body[0]['y'] - 10}
elif direction == 'DOWN':
new_head = {'x': snake_body[0]['x'], 'y': snake_body[0]['y'] + 10}
elif direction == 'LEFT':
new_head = {'x': snake_body[0]['x'] - 10, 'y': snake_body[0]['y']}
elif direction == 'RIGHT':
new_head = {'x': snake_body[0]['x'] + 10, 'y': snake_body[0]['y']}
# 更新蛇身列表
snake_body.insert(0, new_head)
# 检测是否吃到食物
if snake_body[0] == food_pos:
score += 10
food_exist = False
else:
snake_body.pop()
# 重新生成食物
if not food_exist:
food_pos = {'x': random.randrange(0, win_size[0], 10), 'y': random.randrange(0, win_size[1], 10)}
food_exist = True
# 绘制蛇和食物
screen.fill(BLACK)
for pos in snake_body:
pygame.draw.rect(screen, GREEN, pygame.Rect(pos['x'], pos['y'], 10, 10))
pygame.draw.rect(screen, RED, pygame.Rect(food_pos['x'], food_pos['y'], 10, 10))
# 检测碰撞
if (snake_body[0]['x'] < 0 or snake_body[0]['x'] > win_size[0] - 10
or snake_body[0]['y'] < 0 or snake_body[0]['y'] > win_size[1] - 10):
game_over()
for block in snake_body[1:]:
if block == snake_body[0]:
game_over()
show_score()
# 分数显示函数
def show_score():
score_text = font.render('Score: ' + str(score), True, WHITE)
screen.blit(score_text, [0, 0])
# 游戏结束函数
def game_over():
font = pygame.font.SysFont(None, 50)
game_over_text = font.render('GAME OVER', True, WHITE)
screen.blit(game_over_text, [win_size[0]//2-100, win_size[1]//2])
pygame.display.flip()
pygame.time.delay(2000)
pygame.quit()
quit()
# 主循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
change_to = 'UP'
elif event.key == pygame.K_DOWN:
change_to = 'DOWN'
elif event.key == pygame.K_LEFT:
change_to = 'LEFT'
elif event.key == pygame.K_RIGHT:
change_to = 'RIGHT'
move_snake()
pygame.display.update()
pygame.time.delay(100) # 每隔100毫秒更新一次
把变量修改成中文
重试
复制
好的,这里是使用中文变量名的示例代码:
import pygame
import random
pygame.init()
# 游戏窗口大小和标题
窗口尺寸 = (600, 400)
屏幕 = pygame.display.set_mode(窗口尺寸)
pygame.display.set_caption('贪吃蛇')
# 颜色常量
黑色 = (0, 0, 0)
白色 = (255, 255, 255)
绿色 = (0, 255, 0)
红色 = (255, 0, 0)
# 蛇和食物
蛇身 = [{'x': 200, 'y': 200}, {'x': 190, 'y': 200}, {'x': 180, 'y': 200}]
食物位置 = {'x': random.randrange(0, 窗口尺寸[0], 10), 'y': random.randrange(0, 窗口尺寸[1], 10)}
食物存在 = True
# 分数
得分 = 0
字体 = pygame.font.SysFont(None, 25)
# 方向
方向 = 'RIGHT'
改变为 = 方向
# 移动函数
def 移动蛇():
global 方向, 改变为, 蛇身, 食物位置, 食物存在, 得分
# 检测方向
if 改变为 == 'UP' and 方向 != 'DOWN':
方向 = 'UP'
elif 改变为 == 'DOWN' and 方向 != 'UP':
方向 = 'DOWN'
elif 改变为 == 'LEFT' and 方向 != 'RIGHT':
方向 = 'LEFT'
elif 改变为 == 'RIGHT' and 方向 != 'LEFT':
方向 = 'RIGHT'
# 更新蛇头位置
if 方向 == 'UP':
新头 = {'x': 蛇身[0]['x'], 'y': 蛇身[0]['y'] - 10}
elif 方向 == 'DOWN':
新头 = {'x': 蛇身[0]['x'], 'y': 蛇身[0]['y'] + 10}
elif 方向 == 'LEFT':
新头 = {'x': 蛇身[0]['x'] - 10, 'y': 蛇身[0]['y']}
elif 方向 == 'RIGHT':
新头 = {'x': 蛇身[0]['x'] + 10, 'y': 蛇身[0]['y']}
# 更新蛇身列表
蛇身.insert(0, 新头)
# 检测是否吃到食物
if 蛇身[0] == 食物位置:
得分 += 10
食物存在 = False
else:
蛇身.pop()
# 重新生成食物
if not 食物存在:
食物位置 = {'x': random.randrange(0, 窗口尺寸[0], 10), 'y': random.randrange(0, 窗口尺寸[1], 10)}
食物存在 = True
# 绘制蛇和食物
屏幕.fill(黑色)
for 位置 in 蛇身:
pygame.draw.rect(屏幕, 绿色, pygame.Rect(位置['x'], 位置['y'], 10, 10))
pygame.draw.rect(屏幕, 红色, pygame.Rect(食物位置['x'], 食物位置['y'], 10, 10))
# 检测碰撞
if (蛇身[0]['x'] < 0 or 蛇身[0]['x'] > 窗口尺寸[0] - 10
or 蛇身[0]['y'] < 0 or 蛇身[0]['y'] > 窗口尺寸[1] - 10):
游戏结束()
for 方块 in 蛇身[1:]:
if 方块 == 蛇身[0]:
游戏结束()
显示分数()
# 分数显示函数
def 显示分数():
分数文本 = 字体.render('分数: ' + str(得分), True, 白色)
屏幕.blit(分数文本, [0, 0])
# 游戏结束函数
def 游戏结束():
字体 = pygame.font.SysFont(None, 50)
游戏结束文本 = 字体.render('游戏结束', True, 白色)
屏幕.blit(游戏结束文本, [窗口尺寸[0]//2-100, 窗口尺寸[1]//2])
pygame.display.flip()
pygame.time.delay(2000)
pygame.quit()
quit()
# 主循环
while True:
for 事件 in pygame.event.get():
if 事件.type == pygame.QUIT:
游戏结束()
elif 事件.type == pygame.KEYDOWN:
if 事件.key == pygame.K_UP:
改变为 = 'UP'
elif 事件.key == pygame.K_DOWN:
改变为 = 'DOWN'
elif 事件.key == pygame.K_LEFT:
改变为 = 'LEFT'
elif 事件.key == pygame.K_RIGHT:
改变为 = 'RIGHT'
移动蛇()
pygame.display.update()
pygame.time.delay(100) # 每隔100毫秒更新一次chatAi好厉害
他能制造自己
#include<iostream>
using namespace std;
int main(){
int pizza=1;
cout<<"你点了"<<pizza<<"份批萨";
}点赞0
评论