久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

200行python代碼實(shí)現(xiàn)貪吃蛇游戲

瀏覽:87日期:2022-07-29 10:21:16

本文實(shí)例為大家分享了python實(shí)現(xiàn)貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下

這次我們來寫一個(gè)貪吃蛇游戲

下面貼出具體代碼

import pygameimport timeimport numpy as np# 此模塊包含游戲所需的常量from pygame.locals import *# 設(shè)置棋盤的長寬BOARDWIDTH = 48BOARDHEIGHT = 28# 分?jǐn)?shù)score = 0class Food(object): def __init__(self): self.item = (4, 5) # 畫出食物 def _draw(self, screen, i, j): color = 255, 0, 255 radius = 10 width = 10 # i:1---34 j:1---25 position = 10 + 20 * i, 10 + 20 * j # 畫出半徑為 10 的粉色實(shí)心圓 pygame.draw.circle(screen, color, position, radius, width) # 隨機(jī)產(chǎn)生食物 def update(self, screen, enlarge, snack): if enlarge: self.item = np.random.randint(1, BOARDWIDTH - 2), np.random.randint(1, BOARDHEIGHT - 2) while self.item in snack.item: self.item = np.random.randint(1, BOARDWIDTH - 2), np.random.randint(1, BOARDHEIGHT - 2) self._draw(screen, self.item[0], self.item[1])# 貪吃蛇class Snack(object): def __init__(self): # self.item = [(3, 25), (2, 25), (1, 25), (1,24), (1,23), # (1,22), (1,21), (1,20), (1,19), (1,18), (1,17), (1,16)] # x 水平方向 y 豎直方向 # 初始方向豎直向上 self.item = [(3, 25), (2, 25), (1, 25), (1, 24), ] self.x = 0 self.y = -1 def move(self, enlarge): # enlarge 標(biāo)記貪吃蛇有沒有吃到食物 if not enlarge: # 吃到食物刪除尾部元素 self.item.pop() # 新蛇頭的坐標(biāo)為舊蛇頭坐標(biāo)加上移動(dòng)方向的位移 head = (self.item[0][0] + self.x, self.item[0][1] + self.y) # 將新的蛇頭坐標(biāo)插入在 list 最前面 self.item.insert(0, head) def eat_food(self, food): global score # snack_x,snack_y 蛇頭坐標(biāo) # food_x, food_y 食物坐標(biāo) snack_x, snack_y = self.item[0] food_x, food_y = food.item # 比較蛇頭坐標(biāo)與食物坐標(biāo) if (food_x == snack_x) and (food_y == snack_y): score += 100 return 1 else: return 0 def toward(self, x, y): # 改變蛇頭朝向 if self.x * x >= 0 and self.y * y >= 0: self.x = x self.y = y def get_head(self): # 獲取蛇頭坐標(biāo) return self.item[0] def draw(self, screen): # 畫出貪吃蛇 # 蛇頭為半徑為 15 的紅色實(shí)心圓 radius = 15 width = 15 # i:1---34 j:1---25 color = 255, 0, 0 # position 為圖形的坐標(biāo) position = 10 + 20 * self.item[0][0], 10 + 20 * self.item[0][1] pygame.draw.circle(screen, color, position, radius, width) # 蛇身為半徑為 10 的黃色實(shí)心圓 radius = 10 width = 10 color = 255, 255, 0 for i, j in self.item[1:]: position = 10 + 20 * i, 10 + 20 * j pygame.draw.circle(screen, color, position, radius, width)# 初始界面def init_board(screen): board_width = BOARDWIDTH board_height = BOARDHEIGHT color = 10, 255, 255 width = 0 # width:x, height:y # 左右邊框占用了 X: 0 35*20 for i in range(board_width): pos = i * 20, 0, 20, 20 pygame.draw.rect(screen, color, pos, width) pos = i * 20, (board_height - 1) * 20, 20, 20 pygame.draw.rect(screen, color, pos, width) # 上下邊框占用了 Y: 0 26*20 for i in range(board_height - 1): pos = 0, 20 + i * 20, 20, 20 pygame.draw.rect(screen, color, pos, width) pos = (board_width - 1) * 20, 20 + i * 20, 20, 20 pygame.draw.rect(screen, color, pos, width)# 游戲失敗def game_over(snack): broad_x, broad_y = snack.get_head() flag = 0 old = len(snack.item) new = len(set(snack.item)) # 游戲失敗的兩種可能 # 咬到自身 if new < old: flag = 1 # 撞到邊框 if broad_x == 0 or broad_x == BOARDWIDTH - 1: flag = 1 if broad_y == 0 or broad_y == BOARDHEIGHT - 1: flag = 1 if flag: return True else: return False# 打印字符def print_text(screen, font, x, y, text, color=(255, 0, 0)): # 在屏幕上打印字符 # text是需要打印的文本,color為字體顏色 # (x,y)是文本在屏幕上的位置 imgText = font.render(text, True, color) screen.blit(imgText, (x, y))# 按鍵def press(keys, snack): global score # K_w 為 pygame.locals 中的常量 # keys[K_w] 返回 True or False # 上移 if keys[K_w] or keys[K_UP]: snack.toward(0, -1) # 下移 elif keys[K_s] or keys[K_DOWN]: snack.toward(0, 1) # 左移 elif keys[K_a] or keys[K_LEFT]: snack.toward(-1, 0) # 右移 elif keys[K_d] or keys[K_RIGHT]: snack.toward(1, 0) # 重置游戲 elif keys[K_r]: score = 0 main() # 退出游戲 elif keys[K_ESCAPE]: exit()# 游戲初始化def game_init(): # pygame 初始化 pygame.init() # 設(shè)置游戲界面大小 screen = pygame.display.set_mode((BOARDWIDTH * 20, BOARDHEIGHT * 20)) # 設(shè)置游戲標(biāo)題 pygame.display.set_caption(’貪吃蛇游戲’) # sound = pygame.mixer.Sound(AUDIONAME) # channel = pygame.mixer.find_channel(True) # channel.play(sound) return screen# 開始游戲def game(screen): snack = Snack() food = Food() # 設(shè)置中文字體和大小 font = pygame.font.SysFont(’SimHei’, 20) is_fail = 0 while True: for event in pygame.event.get(): if event.type == QUIT: exit() # 填充屏幕 screen.fill((0, 0, 100)) init_board(screen=screen) # 獲得用戶按鍵命令 keys = pygame.key.get_pressed() press(keys, snack) # 游戲失敗打印提示 if is_fail: font2 = pygame.font.Font(None, 40) print_text(screen, font2, 400, 200, 'GAME OVER') # 游戲主進(jìn)程 if not is_fail: enlarge = snack.eat_food(food) food.update(screen, enlarge, snack) snack.move(enlarge) is_fail = game_over(snack=snack) snack.draw(screen) # 游戲刷新 pygame.display.update() time.sleep(0.1)# 主程序def main(): screen = game_init() game(screen)if __name__ == ’__main__’: main()

程序運(yùn)行效果

簡(jiǎn)單截圖了一下可以按住方向鍵移動(dòng)蛇的運(yùn)動(dòng)方向

200行python代碼實(shí)現(xiàn)貪吃蛇游戲

更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:

C++經(jīng)典小游戲匯總

python經(jīng)典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經(jīng)典游戲 玩不停

java經(jīng)典小游戲匯總

javascript經(jīng)典小游戲匯總

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 日日干日日操 | 亚洲精品wwww | 国产精品精品视频一区二区三区 | 国产视频第一页 | 免费成人在线网站 | 成年人精品视频在线观看 | 人妖 丝袜 另类 亚洲 | 山岸逢花在线观看 | 国产日韩一区二区三区 | www.成人在线视频 | 亚洲视频在线播放 | 国产乱码精品一区二区三区av | 欧美日韩啪啪 | 国产日韩免费视频 | 亚洲一级黄色 | 免费看国产片在线观看 | 亚洲午夜电影 | 久在线视频 | 中文字幕毛片 | 国产精品久久久久久久久久久久冷 | 欧美小电影 | 欧美日韩综合视频 | 99精品欧美一区二区三区综合在线 | 综合久久网 | 超碰av在线| 精品在线一区 | 一区二区三区四区精品 | 国产精品爱久久久久久久 | 成人精品一区 | av先锋资源 | 成人免费淫片aa视频免费 | 日韩欧美在线一区 | 一级毛片久久久 | 亚洲视频在线播放 | 天天曰夜夜操 | 日韩一区二区在线视频 | 欧美日韩成人一区 | 91视频免费观看 | 男女网站在线观看 | 日日骚| 一区二区三区在线 |