Snake game v1.1
This commit is contained in:
commit
e642bcaee0
3
.idea/.gitignore
vendored
Normal file
3
.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
4
.idea/misc.xml
Normal file
4
.idea/misc.xml
Normal file
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10" project-jdk-type="Python SDK" />
|
||||
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/vd_snake_game_ikt_project.iml" filepath="$PROJECT_DIR$/.idea/vd_snake_game_ikt_project.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
8
.idea/vd_snake_game_ikt_project.iml
Normal file
8
.idea/vd_snake_game_ikt_project.iml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
BIN
Felhasználói dokumentáció vd snake game.docx
Normal file
BIN
Felhasználói dokumentáció vd snake game.docx
Normal file
Binary file not shown.
204
VD_snake.py
Normal file
204
VD_snake.py
Normal file
|
@ -0,0 +1,204 @@
|
|||
|
||||
import pygame
|
||||
from pygame.locals import *
|
||||
import time
|
||||
import random
|
||||
|
||||
SIZE = 40
|
||||
BACKGROUND_COLOR = (110, 110, 5)
|
||||
|
||||
#alma létrehozása
|
||||
class Apple:
|
||||
def __init__(self, parent_screen):
|
||||
self.parent_screen = parent_screen
|
||||
self.image = pygame.image.load("resources/apple.jpg").convert()
|
||||
self.x = 120
|
||||
self.y = 120
|
||||
|
||||
def draw(self):
|
||||
self.parent_screen.blit(self.image, (self.x, self.y))
|
||||
pygame.display.flip()
|
||||
#alma elhelyezése
|
||||
def move(self):
|
||||
self.x = random.randint(1,24)*SIZE
|
||||
self.y = random.randint(1,19)*SIZE
|
||||
#kígyó és mozgásának definiálása
|
||||
class Snake:
|
||||
def __init__(self, parent_screen):
|
||||
self.parent_screen = parent_screen
|
||||
self.image = pygame.image.load("resources/block.jpg").convert()
|
||||
self.direction = 'down'
|
||||
|
||||
self.length = 1
|
||||
self.x = [40]
|
||||
self.y = [40]
|
||||
|
||||
def move_left(self):
|
||||
self.direction = 'left'
|
||||
|
||||
def move_right(self):
|
||||
self.direction = 'right'
|
||||
|
||||
def move_up(self):
|
||||
self.direction = 'up'
|
||||
|
||||
def move_down(self):
|
||||
self.direction = 'down'
|
||||
|
||||
# kígyó mozgásának a logikája
|
||||
def walk(self):
|
||||
# tesnek a mozgása
|
||||
for i in range(self.length-1,0,-1):
|
||||
self.x[i] = self.x[i-1]
|
||||
self.y[i] = self.y[i-1]
|
||||
|
||||
# fej mozgása
|
||||
if self.direction == 'left':
|
||||
self.x[0] -= SIZE
|
||||
if self.direction == 'right':
|
||||
self.x[0] += SIZE
|
||||
if self.direction == 'up':
|
||||
self.y[0] -= SIZE
|
||||
if self.direction == 'down':
|
||||
self.y[0] += SIZE
|
||||
|
||||
self.draw()
|
||||
|
||||
def draw(self):
|
||||
for i in range(self.length):
|
||||
self.parent_screen.blit(self.image, (self.x[i], self.y[i]))
|
||||
|
||||
pygame.display.flip()
|
||||
#kígyó hosszabbítása
|
||||
def increase_length(self):
|
||||
self.length += 1
|
||||
self.x.append(-1)
|
||||
self.y.append(-1)
|
||||
#játékablak létrehozása
|
||||
class Game:
|
||||
def __init__(self):
|
||||
pygame.init()
|
||||
pygame.display.set_caption("Snake játék")
|
||||
|
||||
pygame.mixer.init()
|
||||
self.play_background_music()
|
||||
|
||||
self.surface = pygame.display.set_mode((1000, 800))
|
||||
self.snake = Snake(self.surface)
|
||||
self.snake.draw()
|
||||
self.apple = Apple(self.surface)
|
||||
self.apple.draw()
|
||||
#háttérzene
|
||||
def play_background_music(self):
|
||||
pygame.mixer.music.load('resources/bg_music_1.mp3')
|
||||
pygame.mixer.music.play(-1, 0)
|
||||
#hangok
|
||||
def play_sound(self, sound_name):
|
||||
if sound_name == "crash":
|
||||
sound = pygame.mixer.Sound("resources/crash.mp3")
|
||||
elif sound_name == 'ding':
|
||||
sound = pygame.mixer.Sound("resources/ding.mp3")
|
||||
|
||||
pygame.mixer.Sound.play(sound)
|
||||
|
||||
|
||||
#újrakezdés definiálása
|
||||
def reset(self):
|
||||
self.snake = Snake(self.surface)
|
||||
self.apple = Apple(self.surface)
|
||||
|
||||
# űtközés definiálása
|
||||
def is_collision(self, x1, y1, x2, y2):
|
||||
if x1 >= x2 and x1 < x2 + SIZE:
|
||||
if y1 >= y2 and y1 < y2 + SIZE:
|
||||
return True
|
||||
return False
|
||||
#háttérkép
|
||||
def render_background(self):
|
||||
bg = pygame.image.load("resources/background.jpg")
|
||||
self.surface.blit(bg, (0,0))
|
||||
#játékmenet
|
||||
def play(self):
|
||||
self.render_background()
|
||||
self.snake.walk()
|
||||
self.apple.draw()
|
||||
self.display_score()
|
||||
pygame.display.flip()
|
||||
|
||||
# a kígyó megeszik egy almát
|
||||
for i in range(self.snake.length):
|
||||
if self.is_collision(self.snake.x[i], self.snake.y[i], self.apple.x, self.apple.y):
|
||||
self.play_sound("ding")
|
||||
self.snake.increase_length()
|
||||
self.apple.move()
|
||||
|
||||
# a kígyó ütkőzik magával
|
||||
for i in range(3, self.snake.length):
|
||||
if self.is_collision(self.snake.x[0], self.snake.y[0], self.snake.x[i], self.snake.y[i]):
|
||||
self.play_sound('crash')
|
||||
raise "Collision Occurred"
|
||||
|
||||
# a kígyó nekimegy a pálya szélének
|
||||
if not (0 <= self.snake.x[0] <= 1000 and 0 <= self.snake.y[0] <= 800):
|
||||
self.play_sound('crash')
|
||||
raise "Hit the boundry error"
|
||||
#Pontok kiírattatása
|
||||
def display_score(self):
|
||||
font = pygame.font.SysFont('arial',30)
|
||||
score = font.render(f"Pont: {self.snake.length}",True,(200,200,200))
|
||||
self.surface.blit(score,(850,10))
|
||||
#játék vége
|
||||
def show_game_over(self):
|
||||
self.render_background()
|
||||
font = pygame.font.SysFont('arial', 30)
|
||||
line1 = font.render(f"Vesztettél! Elért pontok: {self.snake.length}", True, (255, 255, 255))
|
||||
self.surface.blit(line1, (200, 300))
|
||||
line2 = font.render("Az újrakezdéshez nyomd meg a Enter-t és a kilépéshez az Escape-t", True, (255, 255, 255))
|
||||
self.surface.blit(line2, (200, 350))
|
||||
pygame.mixer.music.pause()
|
||||
pygame.display.flip()
|
||||
#újrakezdés
|
||||
def run(self):
|
||||
running = True
|
||||
pause = False
|
||||
|
||||
while running:
|
||||
for event in pygame.event.get():
|
||||
if event.type == KEYDOWN:
|
||||
if event.key == K_ESCAPE:
|
||||
running = False
|
||||
|
||||
if event.key == K_RETURN:
|
||||
pygame.mixer.music.unpause()
|
||||
pause = False
|
||||
|
||||
if not pause:
|
||||
if event.key == K_LEFT:
|
||||
self.snake.move_left()
|
||||
|
||||
if event.key == K_RIGHT:
|
||||
self.snake.move_right()
|
||||
|
||||
if event.key == K_UP:
|
||||
self.snake.move_up()
|
||||
|
||||
if event.key == K_DOWN:
|
||||
self.snake.move_down()
|
||||
|
||||
elif event.type == QUIT:
|
||||
running = False
|
||||
try:
|
||||
|
||||
if not pause:
|
||||
self.play()
|
||||
|
||||
except Exception as e:
|
||||
self.show_game_over()
|
||||
pause = True
|
||||
self.reset()
|
||||
|
||||
time.sleep(.1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
game = Game()
|
||||
game.run()
|
BIN
resources/apple.jpg
Normal file
BIN
resources/apple.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.3 KiB |
BIN
resources/background.jpg
Normal file
BIN
resources/background.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 585 KiB |
BIN
resources/bg_music_1.mp3
Normal file
BIN
resources/bg_music_1.mp3
Normal file
Binary file not shown.
BIN
resources/block.jpg
Normal file
BIN
resources/block.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
BIN
resources/crash.mp3
Normal file
BIN
resources/crash.mp3
Normal file
Binary file not shown.
BIN
resources/ding.mp3
Normal file
BIN
resources/ding.mp3
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user