From c9a2982405f15e1043103d30d2bb7e8490995a13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=B8=8C=E6=97=AD?= <1640580836@qq.com> Date: Tue, 16 Jul 2024 17:31:02 +0800 Subject: [PATCH] A new bug and a solution --- .idea/.gitignore | 8 ++ .idea/.name | 1 + .idea/Snake.iml | 8 ++ .idea/inspectionProfiles/Project_Default.xml | 12 ++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 7 + .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 + main.py | 136 ++++++++++-------- 9 files changed, 132 insertions(+), 60 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/.name create mode 100644 .idea/Snake.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..11a5d8e --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +main.py \ No newline at end of file diff --git a/.idea/Snake.iml b/.idea/Snake.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/Snake.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..a16a69c --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,12 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a6218fe --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..814013f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/main.py b/main.py index 724fc84..1ba1b51 100644 --- a/main.py +++ b/main.py @@ -6,7 +6,6 @@ import random SCREEN_X = 600 SCREEN_Y = 600 - # 蛇类 # 点以25为单位 class Snake(object): @@ -16,13 +15,19 @@ class Snake(object): self.body = [] for x in range(5): self.addnode() - + ''' + # 测试代码 + for i in range(20): + for j in range(20): + self.body.insert(0, pygame.Rect(i * 25, j * 25, 25, 25)) + ''' + # 无论何时 都在前端增加蛇块 def addnode(self): - left,top = (0,0) + left, top = (0, 0) if self.body: - left,top = (self.body[0].left,self.body[0].top) - node = pygame.Rect(left,top,25,25) + left, top = (self.body[0].left, self.body[0].top) + node = pygame.Rect(left, top, 25, 25) if self.dirction == pygame.K_LEFT: node.left -= 25 elif self.dirction == pygame.K_RIGHT: @@ -31,88 +36,101 @@ class Snake(object): node.top -= 25 elif self.dirction == pygame.K_DOWN: node.top += 25 - self.body.insert(0,node) - + self.body.insert(0, node) + # 删除最后一个块 def delnode(self): self.body.pop() - + # 死亡判断 def isdead(self): # 撞墙 - if self.body[0].x not in range(SCREEN_X): + if self.body[0].x not in range(SCREEN_X): return True - if self.body[0].y not in range(SCREEN_Y): + if self.body[0].y not in range(SCREEN_Y): return True # 撞自己 if self.body[0] in self.body[1:]: return True return False - + # 移动! def move(self): self.addnode() self.delnode() - + # 改变方向 但是左右、上下不能被逆向改变 - def changedirection(self,curkey): - LR = [pygame.K_LEFT,pygame.K_RIGHT] - UD = [pygame.K_UP,pygame.K_DOWN] - if curkey in LR+UD: + def changedirection(self, curkey): + LR = [pygame.K_LEFT, pygame.K_RIGHT] + UD = [pygame.K_UP, pygame.K_DOWN] + if curkey in LR + UD: if (curkey in LR) and (self.dirction in LR): return if (curkey in UD) and (self.dirction in UD): return self.dirction = curkey - - + # 食物类 # 方法: 放置/移除 # 点以25为单位 class Food: - def __init__(self): - self.rect = pygame.Rect(-25,0,25,25) - + def __init__(self, snake): + # Snake类型属性,以便后续放置食物时获取snake.body数据用于判断 + self.snake = snake + self.rect = pygame.Rect(-25, 0, 25, 25) + def remove(self): - self.rect.x=-25 - + self.rect.x = -25 + def set(self): if self.rect.x == -25: allpos = [] # 不靠墙太近 25 ~ SCREEN_X-25 之间 - for pos in range(25,SCREEN_X-25,25): + for pos in range(25, SCREEN_X - 25, 25): allpos.append(pos) + ''' + # 原代码 self.rect.left = random.choice(allpos) - self.rect.top = random.choice(allpos) + self.rect.top = random.choice(allpos) + print(self.rect) + ''' + # 测试传入的参数是否同步 + # print(len(self.snake.body)) + # 修改后代码 + while True: + self.rect.left = random.choice(allpos) + self.rect.top = random.choice(allpos) + # 判断当前选中的食物生成位置是否为空 + if self.rect not in self.snake.body: + break print(self.rect) - - -def show_text(screen, pos, text, color, font_bold = False, font_size = 60, font_italic = False): - #获取系统字体,并设置文字大小 - cur_font = pygame.font.SysFont("宋体", font_size) - #设置是否加粗属性 - cur_font.set_bold(font_bold) - #设置是否斜体属性 - cur_font.set_italic(font_italic) - #设置文字内容 - text_fmt = cur_font.render(text, 1, color) - #绘制文字 + +def show_text(screen, pos, text, color, font_bold = False, font_size = 60, font_italic = False): + # 获取系统字体,并设置文字大小 + cur_font = pygame.font.SysFont("宋体", font_size) + # 设置是否加粗属性 + cur_font.set_bold(font_bold) + # 设置是否斜体属性 + cur_font.set_italic(font_italic) + # 设置文字内容 + text_fmt = cur_font.render(text, 1, color) + # 绘制文字 screen.blit(text_fmt, pos) - def main(): pygame.init() - screen_size = (SCREEN_X,SCREEN_Y) + screen_size = (SCREEN_X, SCREEN_Y) screen = pygame.display.set_mode(screen_size) pygame.display.set_caption('Snake') clock = pygame.time.Clock() scores = 0 isdead = False - + # 蛇/食物 snake = Snake() - food = Food() - + # 修改 + food = Food(snake) + while True: for event in pygame.event.get(): if event.type == pygame.QUIT: @@ -122,40 +140,38 @@ def main(): # 死后按space重新 if event.key == pygame.K_SPACE and isdead: return main() - - - screen.fill((255,255,255)) - + + screen.fill((255, 255, 255)) + # 画蛇身 / 每一步+1分 if not isdead: - scores+=1 + scores += 1 snake.move() for rect in snake.body: - pygame.draw.rect(screen,(20,220,39),rect,0) - + pygame.draw.rect(screen, (20, 220, 39), rect, 0) + # 显示死亡文字 isdead = snake.isdead() if isdead: - show_text(screen,(100,200),'YOU DEAD!',(227,29,18),False,100) - show_text(screen,(150,260),'press space to try again...',(0,0,22),False,30) - + show_text(screen, (100, 200), 'YOU DEAD!', (227, 29, 18), False, 100) + show_text(screen, (150, 260), 'press space to try again...', (0, 0, 22), False, 30) + # 食物处理 / 吃到+50分 # 当食物rect与蛇头重合,吃掉 -> Snake增加一个Node if food.rect == snake.body[0]: - scores+=50 + scores += 50 food.remove() snake.addnode() - + # 食物投递 food.set() - pygame.draw.rect(screen,(136,0,21),food.rect,0) - + pygame.draw.rect(screen, (136, 0, 21), food.rect, 0) + # 显示分数文字 - show_text(screen,(50,500),'Scores: '+str(scores),(223,223,223)) - + show_text(screen, (50, 500), 'Scores: ' + str(scores), (223, 223, 223)) + pygame.display.update() clock.tick(10) - - + if __name__ == '__main__': main() \ No newline at end of file -- Gitee