python运行游戏是否需要pygame_用python的pygame,但每次程序一运行,pygame window窗口能出现,但是下图这种状态。...

展开全部

event英文翻译是5261事件 get 获取 type是样式,类型的4102意思,所以他这个都是有规律的,但是有顺序事件在前1653,后面跟属性

同时也有父子(层级)关系

event.get() 获取到的事件

event.type() 事件类型

event.key() 按键事件

一般都是写在while里面,因为这是个不断循环的过程,不然,只执行一便达不到实际要求,这是Pygame里面设定好的

这是贪吃蛇游戏的一段代码(一部分):txt = font.render('GAME OVER', True, (255, 0, 0))

screen.blit(txt, (size[0]/6, size[1]*2/5-20)) # (0, 0) (size[0]/6, size[1]*2/5)100号字中心位置

direction = 'right' # 初始方向,向右

changeDirection = direction # 定义一个改变方向的变量,按键

running = True

while Trunning:

for event in pygame.event.get(): # 从队列中获取事件

if event.type == QUIT: # 判断事件类型是否为退出事件

# pygame.quit()

sys.exit()

elif event.type == KEYDOWN: # 如果按键被按下(事件)

if event.key == K_RIGHT or event.key == K_d: # 如果是右键头或者是d,蛇向右移动

changeDirection = 'right'

if event.key == K_LEFT or event.key == K_a: # 如果是左键头或者是a,蛇向左移动

changeDirection = 'left'

if event.key == K_UP or event.key == K_w:

changeDirection = 'up'

if event.key == K_DOWN or event.key == K_s:

changeDirection = 'down'

#这下面一行可以忽略,

if event.key == K_ESCAPE: # 对应键盘上的Esc键,表示退出

pygame.event.post(pygame.event.Event(QUIT))

# 绘制文本,刷新显示

screen.blit(txt, (20, 10))

screen.fill(blackColor) #放在for语句一列,属于while的(子)层级,循环不断的刷新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的扫雷游戏实现,使用Pythonpygame库: ```python import pygame import random # 初始化游戏 pygame.init() # 设置游戏窗口尺寸 WIDTH = 500 HEIGHT = 600 WINDOW_SIZE = (WIDTH, HEIGHT) # 设置游戏窗口标题 pygame.display.set_caption("扫雷游戏") # 定义常用颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) GRAY = (128, 128, 128) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) YELLOW = (255, 255, 0) # 定义游戏区域尺寸和格子数量 GRID_SIZE = 20 GRID_NUM = int(WIDTH / GRID_SIZE) # 定义雷区和雷数量 mines = [] mine_num = 50 # 创建游戏窗口 screen = pygame.display.set_mode(WINDOW_SIZE) # 创建字体对象 font = pygame.font.SysFont(None, 40) # 创建游戏结束标志 game_over = False # 生成随机雷区 def generate_mines(): global mines # 随机生成雷的位置 for i in range(mine_num): mine = (random.randint(0, GRID_NUM - 1), random.randint(0, GRID_NUM - 1)) while mine in mines: mine = (random.randint(0, GRID_NUM - 1), random.randint(0, GRID_NUM - 1)) mines.append(mine) # 计算每个格子周围的雷数 def count_mines(x, y): count = 0 for i in range(-1, 2): for j in range(-1, 2): if (x + i, y + j) in mines: count += 1 return count # 绘制游戏界面 def draw_game(): global game_over # 绘制游戏区域 screen.fill(WHITE) for i in range(GRID_NUM): for j in range(GRID_NUM): pygame.draw.rect(screen, GRAY, (i * GRID_SIZE, j * GRID_SIZE, GRID_SIZE, GRID_SIZE), 1) if (i, j) in mines and not game_over: pygame.draw.circle(screen, BLACK, (i * GRID_SIZE + GRID_SIZE // 2, j * GRID_SIZE + GRID_SIZE // 2), GRID_SIZE // 4) elif (i, j) not in mines and not game_over: count = count_mines(i, j) if count > 0: text = font.render(str(count), True, BLUE) screen.blit(text, (i * GRID_SIZE + GRID_SIZE // 2 - text.get_width() // 2, j * GRID_SIZE + GRID_SIZE // 2 - text.get_height() // 2)) # 绘制游戏结束信息 if game_over: text = font.render("Game Over", True, RED) screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 - text.get_height() // 2)) # 刷新屏幕 pygame.display.flip() # 主循环 generate_mines() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() elif event.type == pygame.MOUSEBUTTONDOWN: if not game_over: x, y = pygame.mouse.get_pos() i, j = x // GRID_SIZE, y // GRID_SIZE if (i, j) in mines: game_over = True else: count = count_mines(i, j) if count == 0: # 连续翻开空白格子 visited = set() queue = [(i, j)] while queue: x, y = queue.pop(0) visited.add((x, y)) for m in range(-1, 2): for n in range(-1, 2): if 0 <= x + m < GRID_NUM and 0 <= y + n < GRID_NUM and (x + m, y + n) not in visited: count = count_mines(x + m, y + n) if count == 0: queue.append((x + m, y + n)) elif count > 0: visited.add((x + m, y + n)) for x, y in visited: if (x, y) not in mines: count = count_mines(x, y) if count > 0: text = font.render(str(count), True, BLUE) screen.blit(text, (x * GRID_SIZE + GRID_SIZE // 2 - text.get_width() // 2, y * GRID_SIZE + GRID_SIZE // 2 - text.get_height() // 2)) pygame.draw.rect(screen, WHITE, (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE)) else: # 翻开数字格子 text = font.render(str(count), True, BLUE) screen.blit(text, (i * GRID_SIZE + GRID_SIZE // 2 - text.get_width() // 2, j * GRID_SIZE + GRID_SIZE // 2 - text.get_height() // 2)) pygame.draw.rect(screen, WHITE, (i * GRID_SIZE, j * GRID_SIZE, GRID_SIZE, GRID_SIZE)) else: game_over = False mines.clear() generate_mines() draw_game() ``` 这个程序使用了Pygame库来实现,可以直接运行,生成一个简单的扫雷游戏界面。其中,generate_mines()函数用于随机生成雷区,count_mines(x, y)函数用于计算每个格子周围的雷数,draw_game()函数用于绘制游戏界面,主循环监听鼠标事件,根据用户的操作来更新游戏状态和界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值