White Paper
Please understand my incorrect and poor korean expression
green 뱀
blue 먹이
red 폭탄
import pygame
import time
import random
# 초기 설정
pygame.init()
width, height = 600, 400
dis = pygame.display.set_mode((width, height))
pygame.display.set_caption('Snake Game - Blue = Grow, Red = Shrink')
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 15
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 20)
# 색상
white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (0, 255, 0)
# 점수 출력
def your_score(score):
value = score_font.render("Score: " + str(score), True, black)
dis.blit(value, [0, 0])
# 뱀 그리기
def draw_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(dis, green, [x[0], x[1], snake_block, snake_block])
# 메시지 출력
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [width / 6, height / 3])
# 게임 루프
def game_loop():
game_over = False
game_close = False
x1 = width / 2
y1 = height / 2
x1_change = 0
y1_change = 0
snake_list = []
length_of_snake = 1
foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
bombx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
bomby = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
while not game_over:
while game_close:
dis.fill(white)
message("Game Over! Press Q-Quit or C-Play Again", red)
your_score(length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
game_loop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
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
x1 += x1_change
y1 += y1_change
if x1 >= width or x1 < 0 or y1 >= height or y1 < 0:
game_close = True
dis.fill(white)
pygame.draw.rect(dis, blue, [foodx, foody, snake_block, snake_block]) # 파란 먹이
pygame.draw.rect(dis, red, [bombx, bomby, snake_block, snake_block]) # 빨간 폭탄
snake_head = [x1, y1]
snake_list.append(snake_head)
if len(snake_list) > length_of_snake:
del snake_list[0]
for x in snake_list[:-1]:
if x == snake_head:
game_close = True
draw_snake(snake_block, snake_list)
your_score(length_of_snake - 1)
pygame.display.update()
# 파란 먹이 먹음
if x1 == foodx and y1 == foody:
length_of_snake += 1
foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
# 빨간 폭탄 먹음
if x1 == bombx and y1 == bomby:
if length_of_snake > 1:
length_of_snake -= 1
del snake_list[0] # 가장 오래된 꼬리 제거
bombx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
bomby = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
clock.tick(snake_speed)
pygame.quit()
quit()
game_loop()
댓글 0
번호 | 제목 | 조회 수 |
---|---|---|
» |
python 스네이크바이트
![]() | 0 |
55 | 제로보드 서버ip 바뀌면 접속불가 해결... | 22 |
54 |
sdr sma connector 종류
![]() | 158 |
53 |
centos 트래픽 서버 만들기 (darkstat)
![]() | 204 |
52 |
Centos Linux 를 MSwindows나 MAC에서 원격데스크탑으로 보기 (XRDP,RDC,REMOTE DESKTOP CONNECT)
![]() | 594 |
51 |
천체망원경 반사식 전용 삼각대
![]() | 142 |
50 |
MSWindows 정품인증 상태확인방법 windows 윈도우즈승인
![]() | 146 |
49 | LINUX 백업 복구방법 CENTOS5.X 제로보드xe사용시 | 180 |
48 |
라즈베리파이2 라즈베리파이3 스피드비교.
![]() | 141 |
47 |
야르코프스키 효과(Yarkovsky effect) 소행성 궤도가 바뀐다
![]() | 157 |
46 |
거버파일을 gcode로 만드는 방법 (FlatCAM이용)
![]() | 167 |
45 | 500 oops: vsftpd: refusing to run with writable root inside chroot()..FTP | 1820 |
44 | ssh telnet 정해진 사용자만 접근/FTP 다른 폴더 접근 금지 | 210 |
43 |
원자력발전소 경수로에 대해
![]() | 136 |
42 | LINUX CENTOS8 APM설치 Apache, PHP, MySQL | 825 |
41 |
onvif 카메라 시놀로지에서 감시카메라로 사용하려고 구입
![]() | 134 |
40 | 비밀번호변경 passwd | 138 |
39 | 맥북 부팅할때 실행되는 앱 정의하는곳 autoexec | 153 |
38 | 맥북에서 계속 목소리가 나옵니다. 글씨를 계속 읽어 줍니다. | 148 |
37 |
윈도우즈 테스크 위로 올리면 테스크 커지는것 방지방법
![]() | 146 |