python代码贪吃蛇以及有成绩排行榜用类写代码
时间: 2023-07-10 15:03:30 浏览: 47
以下是一个简单的贪吃蛇游戏和成绩排行榜的 Python 代码,使用了类来组织代码:
```python
import curses
import time
import random
class SnakeGame:
def __init__(self):
self.score = 0
self.game_over = False
self.snake = [(4,10), (4,9), (4,8)]
self.food = (10,20)
self.direction = curses.KEY_RIGHT
self.height = 20
self.width = 60
self.win = curses.newwin(self.height, self.width, 0, 0)
self.win.keypad(True)
curses.curs_set(0)
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
def display_score(self):
self.win.addstr(0, 2, "Score: " + str(self.score))
def display_game_over(self):
self.win.addstr(self.height//2, self.width//2-5, "Game Over", curses.A_BOLD)
self.win.addstr(self.height//2+1, self.width//2-10, "Press q to quit", curses.A_BOLD)
self.win.nodelay(0)
while True:
key = self.win.getch()
if key == ord('q'):
return True
def display(self):
self.win.clear()
self.display_score()
for x,y in self.snake:
self.win.addstr(x, y, "#")
self.win.addstr(self.food[0], self.food[1], "*", curses.color_pair(1))
self.win.border(0)
self.win.refresh()
def move_snake(self):
head = self.snake[0]
if self.direction == curses.KEY_RIGHT:
new_head = (head[0], head[1]+1)
elif self.direction == curses.KEY_LEFT:
new_head = (head[0], head[1]-1)
elif self.direction == curses.KEY_UP:
new_head = (head[0]-1, head[1])
elif self.direction == curses.KEY_DOWN:
new_head = (head[0]+1, head[1])
self.snake.insert(0, new_head)
if self.food == new_head:
self.score += 1
self.food = None
while self.food is None:
nf = (random.randint(1, self.height-2), random.randint(1, self.width-2))
self.food = nf if nf not in self.snake else None
self.win.addstr(self.food[0], self.food[1], "*", curses.color_pair(1))
else:
tail = self.snake.pop()
self.win.addstr(tail[0], tail[1], " ")
if (new_head[0] in [0, self.height-1] or new_head[1] in [0, self.width-1] or new_head in self.snake):
self.game_over = True
def get_high_scores(self):
high_scores = []
with open("high_scores.txt", "r") as f:
for line in f:
high_scores.append(int(line))
return high_scores
def save_high_score(self):
high_scores = self.get_high_scores()
high_scores.append(self.score)
high_scores.sort(reverse=True)
high_scores = high_scores[:5]
with open("high_scores.txt", "w") as f:
for score in high_scores:
f.write(str(score) + "\n")
def show_high_scores(self):
high_scores = self.get_high_scores()
self.win.erase()
self.win.addstr(0, 2, "High Scores:")
for i, score in enumerate(high_scores):
self.win.addstr(i+2, 4, str(i+1) + ". " + str(score))
self.win.addstr(self.height-1, 2, "Press any key to continue")
self.win.getch()
def reset(self):
self.score = 0
self.game_over = False
self.snake = [(4,10), (4,9), (4,8)]
self.food = (10,20)
self.direction = curses.KEY_RIGHT
def run(self):
while True:
self.display()
self.win.timeout(100)
key = self.win.getch()
if key in [curses.KEY_RIGHT, curses.KEY_LEFT, curses.KEY_UP, curses.KEY_DOWN]:
if (key == curses.KEY_RIGHT and self.direction != curses.KEY_LEFT or
key == curses.KEY_LEFT and self.direction != curses.KEY_RIGHT or
key == curses.KEY_UP and self.direction != curses.KEY_DOWN or
key == curses.KEY_DOWN and self.direction != curses.KEY_UP):
self.direction = key
self.move_snake()
if self.game_over:
self.save_high_score()
if self.display_game_over():
break
self.reset()
elif key == ord('q'):
break
elif key == ord('s'):
self.show_high_scores()
if __name__ == "__main__":
game = SnakeGame()
game.run()
```
这个代码实现了一个简单的贪吃蛇游戏,使用了 curses 库来控制终端输出。游戏中的贪吃蛇和食物都以元组的形式存储,每次移动贪吃蛇时,将新的头部插入到蛇的列表中,并根据是否吃到食物来决定是否增加长度。游戏结束后,将分数保存到一个文件中,并显示成绩排行榜。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)