-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
207 lines (177 loc) · 6.36 KB
/
main.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from snake import Snake
from items import *
from tictactoe import *
import tkinter as tk
import tile
from tile import *
import color as c
import pygame
def gameSelection():
print("Annette Spielebibliothek:")
print()
print("1) Snake")
print("2) 2048")
print("3) Tic-Tac-Toe")
print()
while True:
selection = input("Choose a game (1-3): ")
if selection in {"1", "2", "3"}:
return int(selection)
else:
print("You must type in a number in range 1 to 3")
def gameReset():
global rowscolumns
global moving_time
global decreasingState
global scoreMultiplicator
global inputdx
global inputdy
global highScore
global mySnake
if mySnake.score > highScore:
highScore = mySnake.score
inputdx = 0
inputdy = 0
rowscolumns = 25
moving_time = 0
decreasingState = False
scoreMultiplicator = 1
mySnake.reset(rowscolumns - 1, 0)
myApple.reset(mySnake.head + mySnake.body,rowscolumns - 1)
myRosine.reset(mySnake.head + mySnake.body,rowscolumns - 1)
pygame.time.wait(400)
def snakeGame():
### INIT ###
# initialisieren von pygame
pygame.init()
pygame.font.init()
# genutzte Farbe
GREEN = ( 0, 214, 0)
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
GREY = (100, 100, 100)
# koennte wichtig sein wenn das Spielfeld NICHT bei 0,0 beginnt
offsetX = 40
offsetY = 40
global rowscolumns
global moving_time
global decreasingState
global scoreMultiplicator
global inputdx
global inputdy
global highScore
highScore = 0
decreasingState = False
moving_time = 0
blockSize = 40
rowscolumns = 25
scoreMultiplicator = 1
# Fenster öffnen
screen = pygame.display.set_mode((1920, 1080))
pygame.display.set_caption("Snake")
# instanz der Snake kreieren
global mySnake
global myApple
global myRosine
mySnake = Snake(rowscolumns - 1)
myApple = Apple()
myApple.reset(mySnake.head + mySnake.body,rowscolumns - 1)
myRosine = Rosine()
myRosine.reset(mySnake.head + mySnake.body,rowscolumns - 1)
# Font
myFont = pygame.font.SysFont("arial", 40)
# solange die Variable True ist, soll das Spiel laufen
spielaktiv = True
# Bildschirm Aktualisierungen einstellen
clock = pygame.time.Clock()
inputdx = 0
inputdy = 0
# GAMELOOP #
while(spielaktiv):
# Überprüfen, ob Nutzer eine Aktion durchgeführt hat
### INPUT ###
for event in pygame.event.get():
if event.type == pygame.QUIT:
spielaktiv = False
# uberpruefe tastatureingaben
# je nach eingabe aendere richtung der snake
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
if mySnake.dx != -1:
inputdx = 1
inputdy = 0
if event.key == pygame.K_a:
if mySnake.dx != 1:
inputdx = -1
inputdy = 0
if event.key == pygame.K_s:
if mySnake.dy != -1:
inputdx = 0
inputdy = 1
if event.key == pygame.K_w:
if mySnake.dy != 1:
inputdx = 0
inputdy = -1
### LOGIC ###
# die Schlange soll nur alle 4 Frames "ausgefuehrt" werden
if moving_time == 0:
mySnake.setDirection(inputdx, inputdy)
if mySnake.dx != 0 or mySnake.dy != 0:
if decreasingState == True:
command = myRosine.action(rowscolumns)
if command == True:
rowscolumns -= 1
if command == False:
deacreasingState = False
scoreMultiplicator = 1
# move the snake one block forward
mySnake.movement()
# ueberpruefe ob die Snake gestorben ist
if mySnake.checkCollisionWall(rowscolumns) == True:
gameReset()
if mySnake.checkCollisionSelf() == True:
gameReset()
# wenn die Snake (Kopf) auf ein Item stoesst wird es in Item gespeichert
Item = mySnake.checkCollisionItem([[myApple.x, myApple.y], [myRosine.x, myRosine.y]], scoreMultiplicator)
# ueberpruefe ob auf Apfel getroffen
if Item == [myApple.x, myApple.y]:
myApple.reset(mySnake.head + mySnake.body,rowscolumns - 1)
# ueberpruefe ob auf Rosine getroffen
if Item == [myRosine.x, myRosine.y]:
# dumb way to make the Rosine dissappear
myRosine.x = -2
myRosine.y = -2
decreasingState = True
scoreMultiplicator = 4
#
moving_time = 4
else:
moving_time -= 1
myApple.checkOutside(rowscolumns)
myRosine.checkOutside(rowscolumns)
### DRAW ###
# Spielfeld löschen
screen.fill(BLACK)
# Spielfeld/figuren zeichnen
for x in range(0, blockSize * rowscolumns, blockSize):
for y in range(0, blockSize * rowscolumns, blockSize):
rect = pygame.Rect(x + offsetX, y + offsetY, blockSize, blockSize)
pygame.draw.rect(screen, GREY, rect, 1)
mySnake.draw(40, screen, GREEN, [offsetX, offsetY])
myApple.draw(40, screen, [offsetX, offsetY])
myRosine.draw(40, screen, [offsetX, offsetY])
screen.blit(myFont.render("Score " + str(mySnake.score), '1', (255, 255, 255)), dest=(1200, 200))
screen.blit(myFont.render("Highscore " + str(highScore), '1', (255, 255, 255)), dest=(1200, 300))
# Fenster aktualisieren
pygame.display.flip()
# Refresh-Zeiten festlegen
clock.tick(60)
pygame.font.quit()
pygame.quit()
selection = gameSelection()
if selection == 1:
snakeGame()
if selection == 2:
tileGame()
if selection == 3:
tictactoeGame()