python编游戏一般用什么库_使用Python第三方库pygame写个贪吃蛇小游戏

今天看到几个关于pygame模块的博客和视频,感觉非常有趣,这里照猫画虎写了一个贪吃蛇小游戏,目前还有待完善,但是基本游戏功能已经实现,下面是代码:

# 导入模块

import pygame

import random

# 初始化

pygame.init()

w = 720 #窗口宽度

h = 600 #窗口高度

ROW = 30 #行数

COL = 36 #列数

#将所有的坐标看作是一个个点,定义点类

class Point:

row = 0

col = 0

def __init__(self, row, col):

self.row = row

self.col = col

def copy(self):

return Point(row = self.row,col = self.col)

#显示窗口和标题

size = (w, h)

window = pygame.display.set_mode(size)

pygame.display.set_caption('贪吃蛇')

#定义蛇头坐标

head = Point(row = ROW/2, col = COL/2)

#蛇身体

snake_list = [

Point(row = head.row,col = head.col+1),

Point(row = head.row,col = head.col+2),

Point(row = head.row,col = head.col+3)

]

#产生食物

def pro_food():

#食物不能与蛇重叠

while True:

pos = Point(row=random.randint(1,ROW-2), col=random.randint(1,COL-2))

is_coll = False

if head.row == pos.row and head.col == pos.col:

is_coll = True

for snake in snake_list:

if snake.col == pos.col and snake.row == pos.row:

is_coll = True

break

if not is_coll:

return pos

food = pro_food()

#定义颜色

bg_color = (255, 255, 255)

head_color = (0, 128, 128)

food_color = (255, 255, 0)

snake_color = (200,200,200)

#给定初始方向

dire = 'left'

def rect(point, color):

cell_width = w/COL

cell_height = h/ROW

left = point.col*cell_width

top = point.row*cell_height

pygame.draw.rect(

window, color,

(left,top,cell_width, cell_height, )

)

pass

# 游戏循环

quit = True

clock = pygame.time.Clock()

while quit:

for event in pygame.event.get():

#退出方式

if event.type == pygame.QUIT:

quit = False

elif event.type == pygame.KEYDOWN:

#键盘控制

if event.key == 273 or event.key == 119:

if dire == 'left' or dire == 'right':

dire = 'up'

elif event.key == 274 or event.key == 115:

if dire == 'left' or dire == 'right':

dire = 'down'

elif event.key == 276 or event.key == 97:

if dire == 'up' or dire == 'down':

dire = 'left'

elif event.key == 275 or event.key == 100:

if dire == 'up' or dire == 'down':

dire = 'right'

#吃

eat=(head.row == food.row and head.col == food.col)

if eat:

food = pro_food()

#处理身体

#1.原来的头换到身体最前端

snake_list.insert(0,head.copy())

#2.删除身体最后一个

if not eat:

snake_list.pop()

#移动

if dire == 'left':

head.col -= 1

elif dire == 'right':

head.col += 1

elif dire == 'up':

head.row -= 1

elif dire == 'down':

head.row += 1

#检测:

dead=False

#1.撞墙

if head.col < 0 or head.row< 0 or head.col >= COL or head.row >= ROW:

dead=True

#2.撞自己

for snake in snake_list:

if head.col == snake.col and head.row == snake.row:

dead=True

break

if dead:

print('dead')

quit = False

#绘制背景

pygame.draw.rect(window, bg_color, (0, 0, w, h))

#蛇头

rect(head, head_color)

#食物

rect(food,food_color)

#蛇身

for snake in snake_list:

rect(snake,snake_color)

pygame.display.flip()

#游戏帧数

clock.tick(20)

效果:

35yyhrddubd.png

总结

到此这篇关于使用Python第三方库pygame写个贪吃蛇小游戏的文章就介绍到这了,更多相关python 贪吃蛇游戏内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

本文标题: 使用Python第三方库pygame写个贪吃蛇小游戏

本文地址: http://www.cppcns.com/jiaoben/python/302305.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的贪吃蛇游戏代码: ```python import pygame import random # 初始化pygame pygame.init() # 定义一些常量 WIDTH = 600 HEIGHT = 480 GRID_SIZE = 20 SPEED = 10 # 创建游戏窗口 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("贪吃蛇") # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) # 定义贪吃蛇类 class Snake: def __init__(self): self.body = [(WIDTH / 2, HEIGHT / 2)] self.direction = random.choice(["up", "down", "left", "right"]) def move(self): if self.direction == "up": self.body.insert(0, (self.body[0][0], self.body[0][1] - GRID_SIZE)) elif self.direction == "down": self.body.insert(0, (self.body[0][0], self.body[0][1] + GRID_SIZE)) elif self.direction == "left": self.body.insert(0, (self.body[0][0] - GRID_SIZE, self.body[0][1])) elif self.direction == "right": self.body.insert(0, (self.body[0][0] + GRID_SIZE, self.body[0][1])) # 删除尾部 self.body.pop() def draw(self): for pos in self.body: pygame.draw.rect(screen, GREEN, (pos[0], pos[1], GRID_SIZE, GRID_SIZE)) def check_collision(self): # 检查是否碰到边界 if self.body[0][0] < 0 or self.body[0][0] >= WIDTH or self.body[0][1] < 0 or self.body[0][1] >= HEIGHT: return True # 检查是否碰到自己 for pos in self.body[1:]: if pos == self.body[0]: return True return False def change_direction(self, direction): if direction == "up" and self.direction != "down": self.direction = "up" elif direction == "down" and self.direction != "up": self.direction = "down" elif direction == "left" and self.direction != "right": self.direction = "left" elif direction == "right" and self.direction != "left": self.direction = "right" # 定义食物类 class Food: def __init__(self): self.pos = (random.randint(0, WIDTH // GRID_SIZE - 1) * GRID_SIZE, random.randint(0, HEIGHT // GRID_SIZE - 1) * GRID_SIZE) def draw(self): pygame.draw.rect(screen, RED, (self.pos[0], self.pos[1], GRID_SIZE, GRID_SIZE)) # 创建贪吃蛇和食物对象 snake = Snake() food = Food() # 游戏循环 clock = pygame.time.Clock() running = True while running: # 设置帧率 clock.tick(SPEED) # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: snake.change_direction("up") elif event.key == pygame.K_DOWN: snake.change_direction("down") elif event.key == pygame.K_LEFT: snake.change_direction("left") elif event.key == pygame.K_RIGHT: snake.change_direction("right") # 移动贪吃蛇 snake.move() # 检查是否碰撞 if snake.check_collision(): running = False # 检查是否吃到食物 if snake.body[0] == food.pos: snake.body.append(snake.body[-1]) food = Food() # 绘制游戏界面 screen.fill(BLACK) snake.draw() food.draw() pygame.display.update() # 退出游戏 pygame.quit() ``` 这个代码实现了一个基本的贪吃蛇游戏,可以通过上下左右键来控制贪吃蛇的移动方向,当贪吃蛇碰到边界或者自己时游戏结束。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值