Pygame开发贪吃蛇游戏时apple_group精灵组无法渲染问题
问题根因
苹果无法显示是坐标计算逻辑错误导致苹果被渲染到了屏幕可视区域外,具体问题点:
- 棋盘总尺寸为20格*30像素/格=600像素,和窗口尺寸完全匹配,但原代码给坐标额外加了300像素的x/y偏移,且遍历行时错误使用
boardY -=30(pygame坐标系y值越大位置越靠下,下一行应该累加y值),导致大部分棋盘格子被偏移到屏幕外 - 苹果所在的第11行第11列(索引从0开始为
[10][10])计算后的中心坐标为(600, 0),刚好落在屏幕右边界之外,自然无法显示;而初始蛇身的4个格子计算后刚好落在屏幕可视范围内,所以能正常渲染 - 两个精灵类的
rect初始化错误,使用了屏幕的全尺寸矩形而非精灵自身图像的矩形,虽然修改center后能勉强渲染蛇,但存在尺寸匹配隐患 - 代码开头遗漏
import pygame语句,直接运行会报模块未定义错误
修复方案
- 补上缺失的pygame导入语句
- 修改精灵类的rect初始化逻辑,使用精灵自身image生成对应尺寸的rect
- 移除多余的300像素坐标偏移,修正行遍历的y值累加逻辑,让棋盘刚好铺满整个窗口
- 精灵位置直接使用格子左上角坐标赋值,用
topleft属性定位更直观,避免中心定位需要额外计算半格偏移的问题
修复后完整可运行代码
import pygame import random pygame.init() clock = pygame.time.Clock() snakeBoard = [ [1,2,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] ] boardWidth = 20 boardHeight = 20 cell_size = 30 move = 0 direction = 1 background_color = (0,0,0) screen_width = 600 screen_height = 600 screen = pygame.display.set_mode((screen_width,screen_height)) pygame.display.set_caption('this is a game') class Snake(pygame.sprite.Sprite): def __init__(self,x,y,color): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface([cell_size,cell_size]) self.image.fill(color) self.rect = self.image.get_rect() self.rect.topleft = (x,y) class Apple(pygame.sprite.Sprite): def __init__(self,x,y,color): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface([cell_size,cell_size]) self.image.fill(color) self.rect = self.image.get_rect() self.rect.topleft = (x,y) snake_group = pygame.sprite.Group() apple_group = pygame.sprite.Group() running = True while running: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if (event.key == pygame.K_w or event.key == pygame.K_UP) and not (direction == 2): direction = 1 if (event.key == pygame.K_s or event.key == pygame.K_DOWN) and not (direction == 1): direction = 2 if (event.key == pygame.K_d or event.key == pygame.K_RIGHT) and not (direction == 4): direction = 3 if (event.key == pygame.K_a or event.key == pygame.K_LEFT) and not (direction == 3): direction = 4 if event.type == pygame.QUIT: running = False screen.fill(background_color) snake_group.empty() apple_group.empty() for row in range(boardHeight): for col in range(boardWidth): cell_val = snakeBoard[row][col] pos_x = col * cell_size pos_y = row * cell_size if cell_val > 0: snake = Snake(pos_x, pos_y, (255,255,255)) snake_group.add(snake) if cell_val == -1: apple = Apple(pos_x, pos_y, (255,0,0)) apple_group.add(apple) snake_group.draw(screen) apple_group.draw(screen) pygame.display.flip() clock.tick(60) pygame.quit()
运行后即可看到左上角的白色蛇身、屏幕正中间的红色苹果正常显示。
内容的提问来源于stack exchange,提问作者ShadowFlame45
相关产品推荐
相关产品推荐

