Skip to content

Commit

Permalink
Merge pull request barryclark#26 from chaelimee/main
Browse files Browse the repository at this point in the history
G_snake.py 수정
  • Loading branch information
chaelimee authored Nov 25, 2023
2 parents d5508f2 + 4a3074c commit 39e7b35
Showing 1 changed file with 171 additions and 62 deletions.
233 changes: 171 additions & 62 deletions G_snake.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,179 @@
# Make G-snake.py
# importing libraries
import pygame

#게임창 생성
import time
import random

snake_speed = 15

# Window size
window_x = 720
window_y = 480

# defining colors
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)

# Initialising pygame
pygame.init()

white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)

dis_width=800
dis_height=600
dis=pygame.display.set_mode((dis_width,dis_height))
pygame.display.set_caption('Snake game')

game_over=False

x1=dis_width/2
y1=dis_height/2

snake_block=10

x1_change = 0
y1_change = 0

clock = pygame.time.Clock()
snake_speed=30

font_style=pygame.font.SysFont(None, 50)

def message(msg,color):
mesg=font_style.render(msg, True, color)
dis.blit(mesg, [dis_width/2, dis_height/2])

while not game_over:

# Initialise game window
pygame.display.set_caption('GeeksforGeeks Snakes')
game_window = pygame.display.set_mode((window_x, window_y))

# FPS (frames per second) controller
fps = pygame.time.Clock()

# defining snake default position
snake_position = [100, 50]

# defining first 4 blocks of snake body
snake_body = [[100, 50],
[90, 50],
[80, 50],
[70, 50]
]
# fruit position
fruit_position = [random.randrange(1, (window_x//10)) * 10,
random.randrange(1, (window_y//10)) * 10]

fruit_spawn = True

# setting default snake direction towards
# right
direction = 'RIGHT'
change_to = direction

# initial score
score = 0

# displaying Score function
def show_score(choice, color, font, size):

# creating font object score_font
score_font = pygame.font.SysFont(font, size)

# create the display surface object
# score_surface
score_surface = score_font.render('Score : ' + str(score), True, color)

# create a rectangular object for the text
# surface object
score_rect = score_surface.get_rect()

# displaying text
game_window.blit(score_surface, score_rect)

# game over function
def game_over():

# creating font object my_font
my_font = pygame.font.SysFont('times new roman', 50)

# creating a text surface on which text
# will be drawn
game_over_surface = my_font.render(
'Your Score is : ' + str(score), True, red)

# create a rectangular object for the text
# surface object
game_over_rect = game_over_surface.get_rect()

# setting position of the text
game_over_rect.midtop = (window_x/2, window_y/4)

# blit will draw the text on screen
game_window.blit(game_over_surface, game_over_rect)
pygame.display.flip()

# after 2 seconds we will quit the program
time.sleep(2)

# deactivating pygame library
pygame.quit()

# quit the program
quit()


# Main Function
while True:

# handling key events
for event in pygame.event.get():
if event.type==pygame.QUIT:
game_over=True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
change_to = 'UP'
if event.key == pygame.K_DOWN:
change_to = 'DOWN'
if event.key == pygame.K_LEFT:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0

if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
game_close = True

x1 += x1_change
y1 += y1_change
dis.fill(white)
pygame.draw.rect(dis, black, [x1, y1, snake_block,snake_block])
change_to = 'LEFT'
if event.key == pygame.K_RIGHT:
change_to = 'RIGHT'

pygame.display.update()
# If two keys pressed simultaneously
# we don't want snake to move into two
# directions simultaneously
if change_to == 'UP' and direction != 'DOWN':
direction = 'UP'
if change_to == 'DOWN' and direction != 'UP':
direction = 'DOWN'
if change_to == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
if change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'

# Moving the snake
if direction == 'UP':
snake_position[1] -= 10
if direction == 'DOWN':
snake_position[1] += 10
if direction == 'LEFT':
snake_position[0] -= 10
if direction == 'RIGHT':
snake_position[0] += 10

clock.tick(snake_speed)
# Snake body growing mechanism
# if fruits and snakes collide then scores
# will be incremented by 10
snake_body.insert(0, list(snake_position))
if snake_position[0] == fruit_position[0] and snake_position[1] == fruit_position[1]:
score += 10
fruit_spawn = False
else:
snake_body.pop()

if not fruit_spawn:
fruit_position = [random.randrange(1, (window_x//10)) * 10,
random.randrange(1, (window_y//10)) * 10]

fruit_spawn = True
game_window.fill(black)

for pos in snake_body:
pygame.draw.rect(game_window, green,
pygame.Rect(pos[0], pos[1], 10, 10))
pygame.draw.rect(game_window, white, pygame.Rect(
fruit_position[0], fruit_position[1], 10, 10))

# Game Over conditions
if snake_position[0] < 0 or snake_position[0] > window_x-10:
game_over()
if snake_position[1] < 0 or snake_position[1] > window_y-10:
game_over()

# Touching the snake body
for block in snake_body[1:]:
if snake_position[0] == block[0] and snake_position[1] == block[1]:
game_over()

# displaying score continuously
show_score(1, white, 'times new roman', 20)

# Refresh game screen
pygame.display.update()

message("You lost", red)
pygame.display.update()
time.sleep(2)

pygame.quit()
quit()
# Frame Per Second /Refresh Rate
fps.tick(snake_speed)

0 comments on commit 39e7b35

Please sign in to comment.