import pygame import sys import random from pygame.locals import * ''' 用背景色填充屏幕(清屏) ''' def clear(self): color = self._color_sub(self.COLOR_WHITE, self.gw_bgcol) self._game_window.fill(self.gw_bgcol) for x in range(self.maxx()+1): pygame.draw.line(self._game_window, color, (x*self.pnt_size, 0), (x*self.pnt_size, self.gw_height), 1) for y in range(self.maxy()+1): pygame.draw.line(self._game_window, color, (0, y * self.pnt_size), (self.gw_width, y*self.pnt_size), 1) #刷新屏幕 def update(self): pygame.display.update() '''在屏幕指定位置画一个正方形(相对坐标) Parameters :param x: 正方形左上角的x坐标 :param y: 正方形左上角的y坐标 :param color: 圆形填充颜色''' def rect(self, x, y, *color): pntcol = self.pnt_col if len(color) != 0: pntcol = color[0] if x < 0 or x > self.maxx() or y < 0 or y > self.maxy(): return self._rect(x*self.pnt_size, y*self.pnt_size, pntcol) '''在屏幕指定位置画一个圆形(相对坐标) Parameters :param x: 圆形外接正方形左上角的x坐标 :param y: 圆形外接正方形左上角的y坐标 :param color: 圆形填充颜色''' def circle(self, x, y, *color): pntcol = self.pnt_col if len(color) != 0: pntcol = color[0] if x < 0 or x > self.maxx() or y < 0 or y > self.maxy(): return x = x*self.pnt_size y = y*self.pnt_size self._circle(x, y, x+self.pnt_size, y+self.pnt_size, pntcol) #屏幕事件 def event(self): for event in pygame.event.get(): if event.type == pygame.QUIT: return self.EVENT_QUIT elif event.type == pygame.KEYDOWN: # KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_a: return self.EVENT_KLEFT elif event.key == pygame.K_RIGHT or event.key == pygame.K_d: return self.EVENT_KRIGHT elif event.key == pygame.K_UP or event.key == pygame.K_w: return self.EVENT_KUP elif event.key == pygame.K_DOWN or event.key == pygame.K_s: return self.EVENT_KDOWN elif event.key == pygame.K_SPACE: return self.EVENT_STOP elif event.key == pygame.K_F1: return self.EVENT_ADD elif event.key == pygame.K_F2: return self.EVENT_SUB elif event.key == pygame.K_ESCAPE: return self.EVENT_QUIT elif event.key == pygame.K_F3: return self.EVENT_KING return self.EVENT_NONE def __init__(self, s_len=5, s_width=40, s_height=20): # (640/20 - 1, 480/20 -1) self.s_width = s_width self.s_height = s_height self.s_life = self.SNAKE_LIFE self._dir = self.DIR_RIGHT self.s_king = False # 无敌模式 self.s_list = [] # 保存贪吃蛇蛇身坐标(s_list[0]保存食物,s_list[1]保存蛇头,其他保存蛇身) self.s_wall = [] # 保存墙的坐标 self._create_wall() # 创建一堵墙, 强的位置随机, 方向随机, 长度最大为5 self.s_map = self._map_create(self.BODY_NONE) # 保存贪吃蛇地图,所有的游戏元素都要填充到地图中,然后统一绘制到屏幕上 # create a food, food = list[0] _s_food = self._create_body() # 创建一个随机的坐标,标记为食物 self.s_list.append(_s_food) # creat a head, head = list[1] self._s_head = self._create_body() # 创建一个随机的坐标,标记为蛇头 self.s_list.append(self._s_head) # create body and add body to list for _ in range(s_len-1): # 蛇身的坐标通过蛇头的坐标和蛇的方向计算得来 self._s_head = (self._s_head[0]-1, self._s_head[1]) self.s_list.append(self._s_head) # print(self.s_list) self.s_score = 0 # len(self.s_list) # 游戏得分,吃一个食物得一分 '''绘制食物和蛇(把地图绘制到屏幕上) :param pen: window对象''' def show(self, pen): pen.clear() self.draw() for x in range(self.s_width): for y in range(self.s_height): if self.s_map[x][y] != self.BODY_NONE: if self.s_map[x][y] == self.BODY_FOOD: pen.circle(x, y, pen.COLOR_BLUE) # draw food if self.s_map[x][y] == self.BODY_HEAD: pen.rect(x, y, pen.COLOR_RED) # draw head if self.s_map[x][y] == self.BODY_SNAKE: pen.rect(x, y, pen.COLOR_GREEN) # draw snake if self.s_map[x][y] == self.BODY_WALL: pen.rect(x, y, pen.COLOR_BLACK) # draw snake pen.update() #把蛇和食物放在地图中 def draw(self): x = 0 y = 0 self._map_init(self.s_map, self.BODY_NONE) if len(self.s_list) != 0: x = self.s_list[0][0] y = self.s_list[0][1] if x >= 0 and x < self.s_width and y >= 0 and y < self.s_height: self.s_map[x][y] = self.BODY_FOOD # draw food x = self.s_list[1][0] y = self.s_list[1][1] if x >= 0 and x < self.s_width and y >= 0 and y < self.s_height: self.s_map[x][y] = self.BODY_HEAD # draw head for s in range(2, len(self.s_list)): # draw snake x = self.s_list[s][0] y = self.s_list[s][1] if x >= 0 and x < self.s_width and y >= 0 and y < self.s_height: self.s_map[x][y] = self.BODY_SNAKE if len(self.s_wall) != 0: for w in self.s_wall: x = w[0] y = w[1] if x >= 0 and x < self.s_width and y >= 0 and y < self.s_height: self.s_map[x][y] = self.BODY_WALL #移动蛇 #:param dir: 蛇移动方向 def move(self, dir): if self._check_dir(self._dir, dir): self._dir = dir head = self.s_list[1] # save head last = self.s_list[-1] # save tail # move the snake body fowward(copy list[n-1] to list[n]) for idx in range(len(self.s_list)-1, 1, -1): self.s_list[idx] = self.s_list[idx-1] head_t = self._add_xy(head, self._dir) # new head # check snake head(cross wall) if head_t[0] < 0: head_t[0] = self.s_width - 1 elif head_t[0] > self.s_width - 1: head_t[0] = 0 if head_t[1] < 0: head_t[1] = self.s_height - 1 elif head_t[1] > self.s_height - 1: head_t[1] = 0 chk, bd = self._check_body(head_t) # check the head # if bd != self.BODY_NONE: # print(chk, bd) if chk == True and bd != self.BODY_NONE: if bd == self.BODY_HEAD or bd == self.BODY_SNAKE or bd == self.BODY_WALL: # eat yourself or wall if self.s_king != True: # 无敌模式 self.s_life = self.SNAKE_DIE # die return self.s_life else: # eat food self.s_list.append(last) # body growth self.s_score = self.s_score + 1 # add score if self.s_score % 10 == 0: # 每吃10个食物增加一面墙 self._create_wall() food = self._create_body() # create food if food == None: # no space to create food self.s_life = self.SNAKE_WIN return self.s_life self.s_list[0] = food self.s_list[1] = head_t # update head if len(self.s_list) == ((self.s_width * self.s_height)): self.s_life = self.SNAKE_WIN return self.s_life #贪吃蛇运行线程 def game_run(snake): global dir global stop global speed delay = 1.5 while True: if stop != True: life = snake.move(dir) if life != snake.SNAKE_LIFE: break # die, exit snake.show(window) delay = 1 - speed * 0.05 if delay < 0.05: delay = 0.05 time.sleep(delay) def game_init(): if __name__ == '__main__': snake, window = game_init() # 创建新线程,在新线程中允许和绘制贪吃蛇 gt = threading.Thread(target=game_run, args=(snake,)) gt.start() # 主线程用于检测按键事件 while True: event = window.event() if event != window.EVENT_NONE: if event == window.EVENT_QUIT: # ESC退出 window.quit() elif event == window.EVENT_KUP or \ event == window.EVENT_KDOWN or \ event == window.EVENT_KLEFT or \ event == window.EVENT_KRIGHT: # 方向键控制贪吃蛇移动 dir = event elif event == window.EVENT_STOP:#空格键暂停和继续 if stop == False: stop = True else: stop = False #print(dir, snake.s_life) elif event == window.EVENT_ADD: # F1速度加 speed = speed + 1 elif event == window.EVENT_SUB: # F2速度减 speed = speed - 1 elif event == window.EVENT_KING: # F3无敌模式 if snake.s_king == True: snake.s_king = False else: snake.s_king = True if snake.s_life != snake.SNAKE_LIFE: # 如果贪吃蛇死亡则退出游戏 window.quit() if score != snake.s_score: # 每得10分速度增加一个等级 score = snake.s_score if(score % 10 == 0): speed = speed + 1
python贪吃蛇游戏无法运行_无法执行的贪吃蛇游戏代码,求大神帮忙改动!谢谢啦!!!
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- CSDN专家-kaily 2021-05-17 17:01关注
你肯定修改了人家原本的程序,或者复制的不完整,里面有一个函数叫__init__(),这至少应该是一个类,你可以发一下源码,而且你如果要运行的话,肯定是需要调用的啊,没有调用,当然不会有输出了,自然就无法运行了
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
- ¥15 vscode程序一直报同样的错,如何解决?
- ¥15 关于使用unity中遇到的问题
- ¥15 开放世界如何写线性关卡的用例(类似原神)
- ¥15 关于并联谐振电磁感应加热
- ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
- ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
- ¥66 如何制作支付宝扫码跳转到发红包界面
- ¥15 pnpm 下载element-plus
- ¥15 解决编写PyDracula时遇到的问题