-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPyqt5_Snake.py
More file actions
151 lines (127 loc) · 5.07 KB
/
Pyqt5_Snake.py
File metadata and controls
151 lines (127 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# __Author__ __Lencof__
# Pyqt5_Snake.py
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class SnakeGame(QMainWindow):
def __init__(self):
super(SnakeGame, self).__init__()
self.sboard = Board(self)
self.statusbar = self.statusBar()
self.sboard.msg2statusbar[str].connect(self.statusbar.showMessage)
self.setCentralWidget(self.sboard)
self.setWindowTitle('Snake game')
self.resize(600, 400)
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)
self.sboard.start()
self.show()
class Board(QFrame):
msg2statusbar = pyqtSignal(str)
SPEED = 70
WIDTHINBLOCKS = 60
HEIGHTINBLOCKS = 30
def __init__(self, parent):
super(Board, self).__init__(parent)
self.timer = QBasicTimer()
self.snake = [[5, 10], [5, 11]]
self.current_x_head = self.snake[0][0]
self.current_y_head = self.snake[0][1]
self.food = []
self.grow_snake = False
self.board = []
self.direction = 1
self.drop_food()
self.setFocusPolicy(Qt.StrongFocus)
def square_width(self):
return self.contentsRect().width() / Board.WIDTHINBLOCKS
def square_height(self):
return self.contentsRect().height() / Board.HEIGHTINBLOCKS
def start(self):
self.msg2statusbar.emit(str(len(self.snake) - 2))
self.timer.start(Board.SPEED, self)
def paintEvent(self, event):
painter = QPainter(self)
rect = self.contentsRect()
boardtop = rect.bottom() - Board.HEIGHTINBLOCKS * self.square_height()
for pos in self.snake:
self.draw_square(painter, rect.left() + pos[0] * self.square_width(),
boardtop + pos[1] * self.square_height())
for pos in self.food:
self.draw_square(painter, rect.left() + pos[0] * self.square_width(),
boardtop + pos[1] * self.square_height())
def draw_square(self, painter, x, y):
color = QColor(0xCC66CC)
painter.fillRect(x + 1, y + 1, self.square_width() - 2,
self.square_height() - 2, color)
def keyPressEvent(self, event):
key = event.key()
if key == Qt.Key_Left:
if self.direction != 2:
self.direction = 1
elif key == Qt.Key_Right:
if self.direction != 1:
self.direction = 2
elif key == Qt.Key_Down:
if self.direction != 4:
self.direction = 3
elif key == Qt.Key_Up:
if self.direction != 3:
self.direction = 4
def move_snake(self):
if self.direction == 1:
self.current_x_head, self.current_y_head = self.current_x_head - 1, self.current_y_head
if self.current_x_head < 0:
self.current_x_head = Board.WIDTHINBLOCKS - 1
if self.direction == 2:
self.current_x_head, self.current_y_head = self.current_x_head + 1, self.current_y_head
if self.current_x_head == Board.WIDTHINBLOCKS:
self.current_x_head = 0
if self.direction == 3:
self.current_x_head, self.current_y_head = self.current_x_head, self.current_y_head + 1
if self.current_y_head == Board.HEIGHTINBLOCKS:
self.current_y_head = 0
if self.direction == 4:
self.current_x_head, self.current_y_head = self.current_x_head, self.current_y_head - 1
if self.current_y_head < 0:
self.current_y_head = Board.HEIGHTINBLOCKS
head = [self.current_x_head, self.current_y_head]
self.snake.insert(0, head)
if not self.grow_snake:
self.snake.pop()
else:
self.msg2statusbar.emit(str(len(self.snake)-2))
self.grow_snake = False
def timerEvent(self, event):
if event.timerId() == self.timer.timerId():
self.move_snake()
self.is_food_collision()
self.is_suicide()
self.update()
def is_suicide(self): # If snake collides with itself, game is over
for i in range(1, len(self.snake)):
if self.snake[i] == self.snake[0]:
self.msg2statusbar.emit(str("TRUP"))
self.snake = [[x, y] for x in range(0, 61) for y in range(0, 41)]
self.timer.stop()
self.update()
def is_food_collision(self):
for pos in self.food:
if pos == self.snake[0]:
self.food.remove(pos)
self.drop_food()
self.grow_snake = True
def drop_food(self):
x = random.randint(3, 58)
y = random.randint(3, 38)
for pos in self.snake: # Do not drop food on snake
if pos == [x, y]:
self.drop_food()
self.food.append([x, y])
def main():
app = QApplication([])
launch_game = SnakeGame()
sys.exit(app.exec_())
if __name__ == '__main__':
main()