Gra Snake (wąż)

Stronę tą wyświetlono już: 971 razy

Bardzo prosta gra w węża zrealizowana w Pythonie z wykorzystaniem modułu tkinter. Poniżej zamieszczam kod programu:

Listing 1
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # program created by owner of page obliczeniowo.jcom.pl
  4. # Licence GPL-3.0 www.gnu.org/licenses/gpl-3.0.en.html
  5. # This is a simple snake game program
  6. import tkinter as tk
  7. import random as rn
  8. def main():
  9. dt = 200 # snake speed
  10. window = tk.Tk() # create window
  11. window.title("Snake") # window title
  12. c_draw = tk.Canvas(window, width = 600, height = 400) # create canvas
  13. c_draw.pack() # pack canvas
  14. class Snake: # snake class
  15. def __init__(self, width, height):
  16. self.width = int(width) # area width
  17. self.height = int(height) # area height
  18. self.msnake = [[self.width / 2, self.height / 2], [self.width / 2 + 1, self.height / 2], [self.width / 2 + 2, self.height / 2]] # snake elements
  19. self.move = 0 # index of vector described direction of snake move
  20. self.tmove = [[0,1],[1,0],[0,-1],[-1,0]] # vectors of direacion of snake move
  21. self.size = 10
  22. self.col = False # określa, czy doszło do kolizji
  23. self.food = [rn.randint(1, self.width - 2), rn.randint(1, self.height - 2)]
  24. def drawBox(self,x, y, color = 'green'):
  25. c_draw.create_rectangle([x, y, x + self.size, y + self.size], fill=color)
  26. def draw(self):
  27. c_draw.delete("all")
  28. if self.col:
  29. c_draw.create_text([self.width / 2 * self.size, self.height / 2 * self.size], text = "Przegrałeś")
  30. else:
  31. for i in range(self.width):
  32. self.drawBox(i * self.size, 0)
  33. self.drawBox(i * self.size, (self.height - 1) * self.size)
  34. for i in range(1, self.height):
  35. self.drawBox(0, i * self.size)
  36. self.drawBox((self.width - 1) * self.size, i * self.size)
  37. for i in self.msnake:
  38. self.drawBox(i[0] * self.size, i[1] * self.size)
  39. self.drawBox(self.food[0] * self.size, self.food[1] * self.size, color = 'red')
  40. def eat(self): # snake must eat
  41. if self.msnake[0][0] == self.food[0] and self.msnake[0][1] == self.food[1]: # if head of snake is on food, then snake eat
  42. self.msnake.append([0,0]) # add snake element
  43. self.food = [rn.randint(1, self.width - 2), rn.randint(1, self.height - 2)] # random new food for snake
  44. def move_snake(self):
  45. for i in range(len(self.msnake) - 1,0,-1):
  46. self.msnake[i][0] = self.msnake[i-1][0]
  47. self.msnake[i][1] = self.msnake[i-1][1]
  48. self.msnake[0][0] += self.tmove[self.move][0]
  49. self.msnake[0][1] += self.tmove[self.move][1]
  50. self.colision()
  51. self.eat()
  52. self.draw()
  53. def turnLeft(self):
  54. self.move = (self.move + 1) % len(self.tmove)
  55. def turnRight(self):
  56. self.move = (self.move - 1) if self.move > 0 else len(self.tmove) - 1
  57. def colision(self):
  58. if self.msnake[0][0] == 0 or self.msnake[0][1] == 0 or self.msnake[0][0] == self.width - 1 or self.msnake[0][1] == self.height - 1:
  59. self.col = True
  60. for i in self.msnake[1:]: # colision snake - snake
  61. if self.msnake[0][0] == i[0] and self.msnake[0][1] == i[1]:
  62. self.col = True
  63. def reset(self):
  64. self.col = False
  65. self.msnake = [[self.width / 2, self.height / 2], [self.width / 2 + 1, self.height / 2], [self.width / 2 + 2, self.height / 2]] # snake elements
  66. self.move = 0 # index of vector described direction of snake move
  67. self.tmove = [[0,1],[1,0],[0,-1],[-1,0]] # vectors of direacion of snake move
  68. self.size = 10
  69. self.col = False # określa, czy doszło do kolizji
  70. self.food = [rn.randint(1, self.width - 2), rn.randint(1, self.height - 2)]
  71. window.after(dt, move)
  72. sn = Snake(600 / 10, 400 / 10)
  73. menubar = tk.Menu(window)
  74. menu = tk.Menu(menubar, tearoff=0)
  75. menubar.add_cascade(label="Program", menu=menu)
  76. menu.add_command(label="Od nowa", command = sn.reset)
  77. menu.add_command(label="Wyjście", command = window.quit)
  78. window.config(menu=menubar)
  79. sn.draw()
  80. def move():
  81. sn.move_snake()
  82. if not sn.col:
  83. window.after(dt, move)
  84. def turnLeft(event):
  85. sn.turnLeft()
  86. def turnRight(event):
  87. sn.turnRight()
  88. window.after(dt, move)
  89. window.bind_all("<KeyPress-Left>", turnLeft)
  90. window.bind_all("<KeyPress-Right>", turnRight)
  91. window.mainloop()
  92. return 0
  93. if __name__ == '__main__':
  94. main()

Sterowanie wężem odbywa się za pomocą strzałek w lewo i w prawo. Poniżej zamieszczony został screen programu.

Porgram Snake napisany w Pythonie 3+ z wykorzystaniem modułu tkinter
Rys. 1
Porgram Snake napisany w Pythonie 3+ z wykorzystaniem modułu tkinter

Załączniki:

Kod programu na GitHub.com

Komentarze