能給我寫一個貪吃蛇游戲代碼嗎?
當然可以!以下是一個簡單的貪吃蛇游戲的 Python 代碼示例:
import pygame import random
游戲窗口大小
WINDOW_WIDTH = 800 WINDOW_HEIGHT = 600
蛇身和食物大小
CELL_SIZE = 20
顏色定義
BLACK = (0, 0, 0) GREEN = (0, 255, 0) RED = (255, 0, 0)
初始化 Pygame
pygame.init() window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption("貪吃蛇游戲")
蛇類
class Snake: def init(self): self.body = [(WINDOW_WIDTH/2, WINDOW_HEIGHT/2)] self.direction = random.choice(["UP", "DOWN", "LEFT", "RIGHT"])
python
復制代碼
def move(self): x, y = self.body[0] if self.direction == "UP": y -= CELL_SIZE elif self.direction == "DOWN": y += CELL_SIZE elif self.direction == "LEFT": x -= CELL_SIZE elif self.direction == "RIGHT": x += CELL_SIZE self.body.insert(0, (x, y)) self.body.pop() def change_direction(self, direction): if direction == "UP" and self.direction != "DOWN": self.direction = direction elif direction == "DOWN" and self.direction != "UP": self.direction = direction elif direction == "LEFT" and self.direction != "RIGHT": self.direction = direction elif direction == "RIGHT" and self.direction != "LEFT": self.direction = direction def draw(self): for x, y in self.body: pygame.draw.rect(window, GREEN, (x, y, CELL_SIZE, CELL_SIZE)) def check_collision(self): x, y = self.body[0] if x < 0 or x >= WINDOW_WIDTH or y < 0 or y >= WINDOW_HEIGHT: return True for body_part in self.body[1:]: if body_part == (x, y): return True return False def eat_food(self, food): if self.body[0] == food.position: self.body.append((food.position[0], food.position[1])) food.generate()
食物類
class Food: def init(self): self.position = ( random.randint(0, (WINDOW_WIDTH-CELL_SIZE) // CELL_SIZE) * CELL_SIZE, random.randint(0, (WINDOW_HEIGHT-CELL_SIZE) // CELL_SIZE) * CELL_SIZE )
ruby
復制代碼
def draw(self): pygame.draw.rect(window, RED, (self.position[0], self.position[1], CELL_SIZE, CELL_SIZE)) def generate(self): self.position = ( random.randint(0, (WINDOW_WIDTH-CELL_SIZE) // CELL_SIZE) * CELL_SIZE, random.randint(0, (WINDOW_HEIGHT-CELL_SIZE) // CELL_SIZE) * CELL_SIZE )
snake = Snake() food = Food()
clock = pygame.time.Clock()
游戲主循環
running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: snake.change_direction("UP") elif event.key == pygame.K_DOWN: snake.change_direction("DOWN") elif event.key == pygame.K_LEFT: snake.change_direction("LEFT") elif event.key == pygame.K_RIGHT: snake.change_direction("RIGHT")
scss
復制代碼
snake.move() if snake.check_collision(): running = False snake.eat_food(food) window.fill(BLACK) snake.draw() food.draw() pygame.display.flip() clock.tick(10)
pygame.quit()
這段代碼使用了 Pygame 庫來實現游戲窗口和圖形的繪制,以及捕獲鍵盤事件。貪吃蛇的移動、吃食物和碰撞檢測等邏輯也在代碼中實現。你可以將代碼復制到 Python 環境中執行,就可以玩這個簡單的貪吃蛇游戲了。(附帶圖片)
人工智能真的要改變我們未來的生活環境了,如還在排斥著不去接收新鮮事物。未來淘汰的就是我們這些不去主動學習,不去接收新鮮事物的一群人。
![wKgZomSyfCCAHmtGAAExM45ICrA768.png](https://file1.elecfans.com/web2/M00/8C/D3/wKgZomSyfCCAHmtGAAExM45ICrA768.png)
![wKgZomSyfC6AYEGkAAFzcUcqP-c745.png](https://file1.elecfans.com/web2/M00/8C/D3/wKgZomSyfC6AYEGkAAFzcUcqP-c745.png)
![wKgaomSyfDeAVFY_AAGGSzhlOqw413.png](https://file1.elecfans.com/web2/M00/8C/D4/wKgaomSyfDeAVFY_AAGGSzhlOqw413.png)
![wKgZomSyfECAbjOQAAF1OeMFdoU328.png](https://file1.elecfans.com/web2/M00/8C/D3/wKgZomSyfECAbjOQAAF1OeMFdoU328.png)
![wKgZomSyfEmAYwxEAAFr4r_KBEk984.png](https://file1.elecfans.com/web2/M00/8C/D3/wKgZomSyfEmAYwxEAAFr4r_KBEk984.png)
![wKgaomSyfFGAASarAAFE6ZyMx1A275.png](https://file1.elecfans.com/web2/M00/8C/D4/wKgaomSyfFGAASarAAFE6ZyMx1A275.png)
-
IT
+關注
關注
2文章
868瀏覽量
63633 -
人工智能
+關注
關注
1796文章
47683瀏覽量
240311 -
代碼
+關注
關注
30文章
4828瀏覽量
69058 -
python
+關注
關注
56文章
4807瀏覽量
85040 -
ChatGPT
+關注
關注
29文章
1570瀏覽量
8067
發布評論請先 登錄
相關推薦
ChatGPT的火爆,讓智能家居有了新的方向
ChatGPT等大模型的發展,對GPGPU有怎樣的性能要求?
【國產FPGA+OMAPL138開發板體驗】(原創)6.FPGA連接ChatGPT 4
在FPGA設計中是否可以應用ChatGPT生成想要的程序呢
![](https://file.elecfans.com/web2/M00/92/78/pYYBAGPvLDaAc8EQAAgE18pblX4162.jpg)
在IC設計/驗證中怎么應用ChatGPT呢?
科技大廠競逐AIGC,中國的ChatGPT在哪?
怎樣寫testbench-xilinx
ChatGPT的火爆,讓智能家居有了新的方向
如何利用ChatGPT幫你寫代碼?
使用ChatGPT來寫python代碼
![使用<b class='flag-5'>ChatGPT</b>來寫python<b class='flag-5'>代碼</b>](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
ChatGPT是什么?ChatGPT寫代碼的原理你知道嗎
用chatGPT寫MIG控制器
![用<b class='flag-5'>chatGPT</b><b class='flag-5'>寫</b>MIG控制器](https://file1.elecfans.com/web2/M00/8C/55/wKgZomSrW1WAEgfPAAA6HMPri0o993.png)
評論