#찐막
import pygame
import sys
import random
import time
from pygame.locals import *
# Pygame 초기화
pygame.init()
# 화면 크기 설정
WIDTH = 1000
HEIGHT = 600
# 디스플레이 생성 및 설정
display = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Title")
clock = pygame.time.Clock()
# 색상 배열 (사용되지 않음)
col_arr = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
col = 0
# 게임 실행 여부 플래그
run = True
# 카메라 위치 (사용되지 않음)
cam_pos = [0, 0]
# 공의 초기 위치, 속도, 가속도, 크기 설정
ball_pos = [100, 300]
ball_v = 0
ball_a = 0
ball_size = 10
# 시간 및 점수 초기화 (사용되지 않거나 수정됨)
t = 0
tJump = 0
point = 0
# 벽 배열 초기화
wall_arr = []
# 메인 게임 루프
while run:
# 프레임 레이트 설정 (60 FPS)
clock.tick(60)
# 화면 초기화
display.fill((0, 0, 0))
# 이벤트 처리
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False # 게임 종료
#_________
elif event.type == pygame.KEYDOWN:
ball_a = -9.8 # 키를 누르면 공을 위로 가속
elif event.type == pygame.KEYUP:
ball_a = 9.8 # 키를 떼면 중력 적용
# 공의 속도 및 위치 업데이트, 0.17은 60프레임, 즉 0.017초를 10배로 늘린 것
ball_v += ball_a * 0.17 # 가속도를 시간에 따라 적용하여 속도 업데이트
ball_pos[1] += ball_v * 0.17 # 속도를 시간에 따라 적용하여 위치 업데이트
# 공이 화면 밖으로 나갔을 때 처리
if ball_pos[1] > HEIGHT - ball_size:
ball_pos[1] = ball_size
elif ball_pos[1] < ball_size:
ball_pos[1] = HEIGHT - ball_size
# 공 그리기
pygame.draw.circle(display, (255, 0, 0), (int(ball_pos[0]), int(ball_pos[1])), ball_size)
#___________
# 일정 시간마다 벽 생성
if point % 30 == 0:
# 벽은 화면 오른쪽 끝에서 생성되어 왼쪽으로 이동
wall_x = WIDTH
wall_y = random.randint(0, HEIGHT - 90)
wall_size = random.randint(10, 90)
wall_arr.append([wall_x, wall_y, wall_size])
# 삭제할 벽의 인덱스를 저장할 리스트
del_arr = []
# 벽 이동 및 그리기
for i in range(len(wall_arr)):
wall = wall_arr[i]
wall_rect = pygame.Rect(int(wall[0]), int(wall[1]), wall[2], wall[2])
pygame.draw.rect(display, (255, 255, 255), wall_rect)
wall[0] -= 10 # 벽을 왼쪽으로 이동
# 공과 벽의 충돌 감지
ball_rect = pygame.Rect(int(ball_pos[0] - ball_size), int(ball_pos[1] - ball_size), ball_size * 2, ball_size * 2)
if ball_rect.colliderect(wall_rect):
time.sleep(1) # 충돌 시 잠시 대기
run = False # 게임 종료
# 벽이 화면 밖으로 나가면 삭제 목록에 추가
if wall[0] + wall[2] <= 0:
del_arr.append(i)
# 화면 밖으로 나간 벽 삭제
for i in reversed(del_arr):
del wall_arr[i]
# 점수 증가 (현재는 프레임당 1점 증가)
point += 1
# 점수 표시
font = pygame.font.SysFont(None, 30)
score_text = font.render(str(point), True, (255, 255, 255))
display.blit(score_text, (100, 100))
# 화면 업데이트
pygame.display.update()
# 게임 종료 처리
pygame.quit()
sys.exit()
import pygame, sys
from pygame.locals import *
import random
import time
pygame.init()
WIDTH = 1000
HEIGHT = 600
display = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Title")
clock = pygame.time.Clock()
run = True
ball_pos = [100,300]
ball_v = 0
ball_a = 0
t = 0
tJump = 0
point = 0
wall_arr = []
while run:
clock.tick(900)
display.fill((0,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
ball_a = -9.8 # 아무 키나 눌렀을 때 가속도 변경
elif event.type == pygame.KEYUP:
ball_a = 9.8 # 키를 떼면 가속도를 원래 값으로 복원
ball_v += ball_a * 0.017
ball_pos[1] += ball_v * 0.017
if ball_pos[1] >600:
ball_pos[1] = 0
elif ball_pos[1] < 0:
ball_pos[1] = 600
pygame.draw.circle(display, (255,0,0), ball_pos, 10)
if pygame.time.get_ticks()%400==0:
wall_arr.append([1000,random.randint(0,600),random.randint(10,90)])
del_arr = []
for i in range(len(wall_arr)):
pygame.draw.rect(display, (255,255,255), (wall_arr[i][0],wall_arr[i][1],wall_arr[i][2],wall_arr[i][2]))
wall_arr[i][0] -= 1
if 100 >= wall_arr[i][0] >=0:
if ball_pos[1]-5<wall_arr[i][1]<ball_pos[1]+5:
time.sleep(5)
run = False
if wall_arr[i][0] <= 0:
del_arr.append(i)
for i in reversed(del_arr):
del wall_arr[i]
point += 1
font = pygame.font.SysFont(None,30)
myText = font.render(str(point), True, (255,255,255)) #(Text,anti-alias, color)
display.blit(myText, (100,100)) #(글자변수, 위치)
pygame.display.update()
pygame.quit()
sys.exit()
게임을 만들기 위해 필요한 파이썬 모듈을 임포트하고 Pygame을 초기화합니다.
import pygame
import sys
import random
import time
from pygame.locals import *
# Pygame 초기화
pygame.init()
pygame
: 게임 개발을 위한 라이브러리입니다.