import pygame
from pygame.rect import *
import random
###################################################################
###################################################################
def restart():
global isGameOver, score
isGameOver = False
score = 0
for i in range(len(star)):
recStar[i].y = -1
###################################################################
###################################################################
def eventProcess():
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
if event.key == pygame.K_LEFT:
move.x = -1
if event.key == pygame.K_RIGHT:
move.x = 1
if event.key == pygame.K_UP:
move.y = -1
if event.key == pygame.K_DOWN:
move.y = 1
if event.key == pygame.K_r:
restart()
###################################################################
###################################################################
def movePlayer():
if not isGameOver:
recPlayer.x += move.x
recPlayer.y += move.y
if recPlayer.x < 0:
recPlayer.x = 0
if recPlayer.x > SCREEN_WIDTH - recPlayer.width:
recPlayer.x = SCREEN_WIDTH - recPlayer.width
if recPlayer.y < 0:
recPlayer.y = 0
if recPlayer.y > SCREEN_HEIGHT - recPlayer.height:
recPlayer.y = SCREEN_HEIGHT - recPlayer.height
SCREEN.blit(player, recPlayer)
###################################################################
###################################################################
def timeDelay500ms():
global time_delay_500ms
if time_delay_500ms > 5:
time_delay_500ms = 0
return True
time_delay_500ms += 1
return False
def makeStar():
if isGameOver:
return
if timeDelay500ms():
idex = random.randint(0, len(star)-1)
if recStar[idex].y == -1:
recStar[idex].x = random.randint(0, SCREEN_WIDTH)
recStar[idex].y = 0
def moveStar():
makeStar()
for i in range(len(star)):
if recStar[i].y == -1:
continue
if not isGameOver:
recStar[i].y += 1
if recStar[i].y > SCREEN_HEIGHT:
recStar[i].y = 0
SCREEN.blit(star[i], recStar[i])
###################################################################
###################################################################
class rankscore :
def CheckCollision(self):
global score, isGameOver
if isGameOver:
return
for rec in recStar:
if rec.y == -1:
continue
if rec.top < recPlayer.bottom \
and recPlayer.top < rec.bottom \
and rec.left < recPlayer.right \
and recPlayer.left < rec.right:
isGameOver = True
break
score += 1
def rank_change(self) :
if m.rank == 'S':
elif m.rank == 'B' or m.rank == 'A':
star[i] = pygame.transform.scale(star[i], (30, 30))
recStar[i] = star[i].get_rect()
recStar[i].y = -1
elif m.rank == 'MASTER':
star[i] = pygame.transform.scale(star[i], (200, 200))
recStar[i] = star[i].get_rect()
recStar[i].y = -1
else:
global score
if 1000>=score :
self.rank='Z'
elif 2000>score>1000 :
self.rank='C'
elif 4000>=score>2000 :
self.rank='B'
elif 6000>=score>4000 :
self.rank='A'
elif 10000>score>6000 :
self.rank='S'
star[i] = pygame.transform.scale(star[i], (40, 40))
recStar[i] = star[i].get_rect()
recStar[i].y = -1
elif score>=10000 :
self.rank='MASTER'
def blinking():
global time_dealy_4sec, toggle
time_dealy_4sec += 1
if time_dealy_4sec > 40:
time_dealy_4sec = 0
toggle = ~toggle
return toggle
def setText():
global score
g=rankscore()
g.rank_change()
mFont = pygame.font.SysFont("arial",20, True, False)
SCREEN.blit(mFont.render(
f'score : {score}', True, 'green'), (10, 10, 0, 0))
SCREEN.blit(mFont.render(
f'rank : {g.rank}', True, 'green'), (10, 30, 0, 0))
if isGameOver and blinking():
SCREEN.blit(mFont.render(
'game over!!', True, 'red'), (SCREEN_WIDTH/2-80, SCREEN_HEIGHT/2-50, 0, 0))
SCREEN.blit(mFont.render(
'press R - restart', True, 'red'), (SCREEN_WIDTH/2-80, SCREEN_HEIGHT/2, 0, 0))
SCREEN.blit(mFont.render(
'press Esc - quit', True, 'red'), (SCREEN_WIDTH / 2 - 80, SCREEN_HEIGHT / 2 +20 , 0, 0))
SCREEN.blit(mFont.render(
f'rank : {g.rank}', True, 'red'), (SCREEN_WIDTH/2-80, SCREEN_HEIGHT/2-30, 0, 0))
###################################################################
###################################################################
# 1.변수초기화
isActive = True
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 1000
move = Rect(0, 0, 0, 0)
time_delay_500ms = 0
time_dealy_4sec = 0
toggle = False
score = 9000
m=rankscore()
m.rank_change()
isGameOver = False
# 2.스크린생성
pygame.init()
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('CodingNoew!!')
# 3. player 생성
player = pygame.image.load('player.png')
player = pygame.transform.scale(player, (20, 30))
recPlayer = player.get_rect()
recPlayer.centerx = (SCREEN_WIDTH / 2)
recPlayer.centery = (SCREEN_HEIGHT / 2)
# 4. 유성 생성
star = [pygame.image.load('star.png') for i in range(40)]
recStar = [None for i in range(len(star))]
for i in range(len(star)):
star[i] = pygame.transform.scale(star[i], (20, 20))
recStar[i] = star[i].get_rect()
recStar[i].y = -1
# 5. 기타
clock = pygame.time.Clock()
#####반복####
while isActive:
#1.화면 지움
SCREEN.fill((0,0,0))
#2.이벤트처리
eventProcess()
#3.플레이어 이동
movePlayer()
#4.유성 생성 및 이동
moveStar()
#5.충돌 확인
m.CheckCollision()
#6.text업데이트
setText()
#7.화면 갱신
pygame.display.flip()
clock.tick(100)
#####반복####
# https://ai-creator.tistory.com/529top of page

실제 작동 상태를 확인하려면 라이브 사이트로 이동하세요.
20250927
20250927
댓글 0개
좋아요
댓글(0)
더 이상 게시물에 대한 댓글 기능이 지원되지 않습니다. 자세한 사항은 사이트 소유자에게 문의하세요.
bottom of page


