동아리 작품 20517 임종구 |
좋아요:0 | ||||
---|---|---|---|---|---|
작성자 | 임종구 | 등록일 | 19.12.25 | 조회수 | 82 |
첨부파일 |
|
||||
import pygame import random from time import sleep
window_width = 480 window_height = 800
black = (0, 0, 0) white = (255, 255, 255) gray = (150, 150, 150) red = (255, 0, 0)
class Car: image_car = ['RacingCar01.png', 'RacingCar02.png', 'RacingCar03.png', 'RacingCar04.png', 'RacingCar05.png', 'RacingCar06.png', 'RacingCar07.png', 'RacingCar08.png', 'RacingCar09.png', 'RacingCar10.png', 'RacingCar11.png', 'RacingCar12.png', 'RacingCar13.png', 'RacingCar14.png', 'RacingCar15.png', 'RacingCar16.png', 'RacingCar17.png', 'RacingCar18.png', 'RacingCar19.png', 'RacingCar20.png']
def __init__(self, x=0, y=0, dx=0, dy=0): self.image = "" self.x = x self.y = y self.dx = dx self.dy = dy #값 초기화 def load_image(self): self.image = pygame.image.load(random.choice(self.image_car)) self.width = self.image.get_rect().size[0] self.height = self.image.get_rect().size[1] #이미지 불러오고 이미지 범위 설정 def draw_image(self): screen.blit(self.image, [self.x, self.y]) #이미지 그리기 def move_x(self): self.x += self.dx #x축 이동 def move_y(self): self.y += self.dy #y축이동 def check_out_of_screen(self): if self.x + self.width > window_width or self.x < 0: self.x -= self.dx #화면 밖으로 나갔는지 체크 def check_crash(self, car): if (self.x + self.width > car.x) and (self.x < car.x + car.width) and (self.y < car.y + car.height) and ( self.y + self.height > car.y): return True else: return False #플레이어가 차에 박았는지 측정
def draw_main_menu(): draw_x = int((window_width / 2) - 200) draw_y = int(window_height / 2) image_intro = pygame.image.load('PyCar.png') screen.blit(image_intro, [draw_x, draw_y - 280]) #시작이미지 불러오기 font_40 = pygame.font.SysFont("FixedSys", 40, True, False) font_30 = pygame.font.SysFont("FixedSys", 30, True, False) #폰트 설정 text_title = font_40.render("PyCar: Racing Car Game", True, black) screen.blit(text_title, [draw_x, draw_y]) score_text = font_40.render("Score:" + str(score), True, white) screen.blit(score_text, [draw_x, draw_y+70]) #게임제목과 점수 표시
text_start = font_30.render("Press Space Key to Start!", True, red) screen.blit(text_start, [draw_x, draw_y + 140]) pygame.display.flip()
def draw_score(): font_30 = pygame.font.SysFont("FixedSys", 30, True, False) text_score = font_30.render("Score:" + str(score), True, black) screen.blit(text_score, [15, 15])
if __name__ == '__main__': pygame.init()
screen = pygame.display.set_mode((window_width, window_height)) pygame.display.set_caption("Cross the road in eumseong") #제목과 게임의 크기설정 clock = pygame.time.Clock()
pygame.mixer.music.load('race.wav') sound_crash = pygame.mixer.Sound('crash.wav') sound_engine = pygame.mixer.Sound('engine.wav') #게임소리 불러오기 player = Car(window_width / 2, window_height - 150, 0, 0) player.load_image() #플레이어의 위치조절과 이미지 불러오기 cars = [] car_count = 5 for i in range(car_count): x = random.randrange(0, window_width - 55) car = Car(x, random.randrange(-150,-50), 0, random.randint(5, 10)) car.load_image() cars.append(car) #카운트수 만큼 화면에 나타날 차의 위치를 랜덤으로 저장하여 배열에 저장 lanes = [] lane_width = 10 lane_height = 80 lane_margin = 20 lane_count = 10 lane_x = (window_width - lane_width) / 2 lane_y = -10 for i in range(lane_count): lanes.append([lane_x, lane_y]) lane_y += lane_height + lane_margin #중간에 선그리기 score = 0 crash = True game_on = True while game_on: for event in pygame.event.get(): if event.type == pygame.QUIT: game_on = False #게임창 닫기 if crash: if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: crash = False #충돌하여 멈췄을때(시작할때) 스페이스바를 눌러 게임 재시작 for i in range(car_count): cars[i].x = random.randrange(0, window_width - cars[i].width) cars[i].y = random.randrange(-150, -50) cars[i].load_image()
player.load_image() player.x = window_width / 2 player.dx = 0 score = 0 pygame.mouse.set_visible(False) #마우스 안보이게 설정 sound_engine.play() sleep(5) pygame.mixer.music.play(-1)
if not crash: if event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: player.dx = 4 elif event.key == pygame.K_LEFT: player.dx = -4 #키를 눌렀을때 플레이어가 이동할 방향 설정 if event.type == pygame.KEYUP: if event.key == pygame.K_RIGHT: player.dx = 0 elif event.key == pygame.K_LEFT: player.dx = 0 #키를 떼면 값을 초기화 screen.fill(gray)
if not crash: for i in range(lane_count): pygame.draw.rect(screen, white, [lanes[i][0], lanes[i][1], lane_width, lane_height]) lanes[i][1] += 10 if lanes[i][1] > window_height: lanes[i][1] = -40 - lane_height
player.draw_image() player.move_x() player.check_out_of_screen()
for i in range(car_count): cars[i].draw_image() cars[i].y += cars[i].dy if cars[i].y > window_height: score += 10 cars[i].x = random.randrange(0, window_width - cars[i].width) cars[i].y = random.randrange(-150, -50) cars[i].dy = random.randint(5, 10) cars[i].load_image()
for i in range(car_count): if player.check_crash(cars[i]): crash = True pygame.mixer.music.stop() sound_crash.play() sleep(2) pygame.mouse.set_visible(True) break
draw_score() pygame.display.flip() else: draw_main_menu()
clock.tick(60)
pygame.quit()
|
이전글 | 10602 권윤민 파이썬 결과물(램수면 계산기) |
---|---|
다음글 | 10213 이재희 동아리 활동 보고서 |