파이썬 파이게임을 이용해서 만든 간단한 프로그램이다!
pygame 모듈을 설치한 후에
첨부파일을 다운받으면 실행시킬 수 있다 :D
#-*- coding: utf-8 -*-#
import random
import sys
import math
import pygame
from pygame import mixer
import time
from time import sleep
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 500
#한글 폰트 불러오기
pygame.font.init()
font = pygame.font.SysFont('malgungothic', 20)
#토마토 불러오기
redtomato = pygame.image.load('tomatotomato.png')
happytomato = pygame.image.load('redtomato.png')
sadtomato = pygame.image.load('sadtomato.png')
mansaytomato = pygame.image.load('tomato.png')
# 배경 불러오기
basicbackground = pygame.image.load('basicbackground.png')
basicbackground = pygame.transform.scale(basicbackground, (800, 500))
studying = pygame.image.load('studying.png')
studying = pygame.transform.scale(studying, (800, 500))
wheretogo = pygame.image.load('wheretogo.png')
wheretogo = pygame.transform.scale(wheretogo, (800, 500))
worldview = pygame.image.load('worldview.png')
worldview = pygame.transform.scale(worldview, (800, 500))
pop = pygame.image.load('pop.png')
pop = pygame.transform.scale(pop, (800, 500))
pop1 = pygame.image.load('pop1.png')
pop1 = pygame.transform.scale(pop1, (800, 500))
pop2 = pygame.image.load('pop2.png')
pop2 = pygame.transform.scale(pop2, (800, 500))
pop3 = pygame.image.load('pop3.png')
pop3 = pygame.transform.scale(pop3, (800, 500))
pop4 = pygame.image.load('pop4.png')
pop4 = pygame.transform.scale(pop4, (800, 500))
#아이콘 불러오기
icon = pygame.image.load('tomato.png')
pygame.display.set_icon(icon)
VELOCITY = 7
MASS = 2
class Car:
def __init__(self):
self.image = ""
self.subimage = ""
self.dx = 0
self.dy = 0
self.rect = ""
self.isJump = 0
self.v = VELOCITY # ?ӵ?
self.m = MASS # ????
self.map_num = 0
self.tomato = 0
self.one = 0
self.two = 0
self.three = 0
self.four = 0
self.space = 0
def load_car(self):
# ?÷??̾? ????
self.image = pygame.image.load("tomato.png")
# ũ?? ????
self.image = pygame.transform.scale(self.image, (100, 100))
self.rect = self.image.get_rect()
self.rect.centerx = round(WINDOW_WIDTH / 2)
self.rect.bottom = WINDOW_HEIGHT - 57
# ?ڵ????? ??ũ???? ????
def draw_car(self):
SCREEN.blit(self.image, [self.rect.x, self.rect.y])
# x ??ǥ ?̵? - ?÷??̾? ?ڵ????? ?????? ?????? ?? ?ʿ?
def move_x(self):
self.rect.x += self.dx
# ȭ?? ?????? ?? ?????? ????
def check_screen(self):
if self.rect.bottom > WINDOW_HEIGHT or self.rect.y < 0:
self.rect.y -= self.dy
#???? ???¸? üũ
def jump(self, j):
self.isJump = j
def update(self):
if self.isJump > 0:
if self.isJump == 2:
self.v = VELOCITY
if self.v > 0:
F = (0.5*self.m*(self.v*self.v))
else:
F = -(0.5*self.m*(self.v*self.v))
self.rect.y -=round(F)
self.v -= 1
if self.rect.bottom > WINDOW_HEIGHT - 57:
self.rect.bottom = WINDOW_HEIGHT - 57
self.isJump = 0
self.v = VELOCITY
def map(self):
# x값에 반응하는 곳
#basic
if self.map_num == 0:
SCREEN.blit(basicbackground, (0,0))
if self.rect.x == 800:
self.rect.x = 0
self.space = 0
self.map_num += 1
elif self.rect.x < 0:
self.rect.x = 0
self.space = 0
#worldview
elif self.map_num == 1:
SCREEN.blit(worldview, (0,0))
if self.rect.x == 800:
self.rect.x = 0
self.space = 0
self.map_num += 1
elif self.rect.x < 0:
self.map_num -= 1
self.rect.x += 800
self.space = 0
#최상위 + 최상위
elif self.map_num == 2:
SCREEN.blit(wheretogo, (0,0))
if self.rect.x == 800:
self.rect.x = 0
self.space = 0
self.map_num += 1
elif self.rect.x < 0:
self.map_num -= 1
self.rect.x += 800
self.space = 0
#뽑기
elif self.map_num == 3:
SCREEN.blit(pop, (0,0))
if self.rect.x == 800:
self.rect.x = 0
self.space = 0
self.map_num += 1
elif self.rect.x < 0:
self.map_num -= 1
self.rect.x += 800
self.space = 0
#studying - 열공 중
elif self.map_num == 4:
SCREEN.blit(studying, (0,0))
if self.rect.x >= 700:
self.rect.x = 700
self.space = 0
elif self.rect.x < 0:
self.map_num -= 1
self.rect.x += 800
self.space = 0
self.one = 0
self.two = 0
self.three = 0
self.four = 0
def conversation(self):
# space에 반응하는 곳
if self.map_num == 0:
if self.space == 0:
score = font.render("space바를 눌러주세요", True, (0,0,0))
SCREEN.blit(score, (self.rect.x -30, 200))
if self.space == 1:
score = font.render("안녕?", True, (0,0,0))
SCREEN.blit(score, (self.rect.x +30, 200))
score = font.render("반가워", True, (0,0,0))
SCREEN.blit(score, (self.rect.x +30, 240))
elif self.space == 3:
score = font.render("나는 토마토야", True, (0,0,0))
SCREEN.blit(score, (self.rect.x-30 , 200))
score = font.render("수학나라에 온 것을 환영해!", True, (0,0,0))
SCREEN.blit(score, (self.rect.x -30, 240))
elif self.space == 5:
score = font.render("오늘부터 여기서 수학을 공부해볼거야!", True, (0,0,0))
SCREEN.blit(score, (self.rect.x-60 , 200))
score = font.render("어때?", True, (0,0,0))
SCREEN.blit(score, (self.rect.x , 240))
elif self.space == 7:
score = font.render("그럼 시작해보자!", True, (0,0,0))
SCREEN.blit(score, (self.rect.x-20 , 200))
elif self.space == 9:
SCREEN.fill((0,0,0))
elif self.space == 11:
score = font.render("우선 키보드 조작법을 알려줄게", True, (0,0,0))
SCREEN.blit(score, (self.rect.x-60 , 150))
elif self.space == 13:
score = font.render("키보드 방향키를 누르면", True, (0,0,0))
SCREEN.blit(score, (self.rect.x-60 , 200))
score = font.render("오른쪽, 왼쪽으로 이동할 수 있어", True, (0,0,0))
SCREEN.blit(score, (self.rect.x-70 , 240))
elif self.space == 15:
score = font.render("잘했어!", True, (0,0,0))
SCREEN.blit(score, (self.rect.x , 150))
elif self.space == 17:
score = font.render("이번엔, 키보드 t를 눌러볼래?", True, (0,0,0))
SCREEN.blit(score, (self.rect.x-60 , 150))
elif self.space == 19:
score = font.render("좋았어!", True, (0,0,0))
SCREEN.blit(score, (self.rect.x , 150))
elif self.space == 21:
score = font.render("이렇게 tomatoworld를 돌아다니면서,", True, (255,0,255))
SCREEN.blit(score, (self.rect.x -40, 150))
score = font.render("수학 공부를 할거야 :)", True, (255,0,255))
SCREEN.blit(score, (self.rect.x -40, 200))
elif self.space == 23:
score = font.render("그럼 다음 장소로 이동해보자!", True, (0,0,0))
SCREEN.blit(score, (self.rect.x -40, 150))
#worldview
elif self.map_num == 1:
if self.space == 3:
score = font.render("우와~", True, (0,0,0))
SCREEN.blit(score, (self.rect.x+10, 300))
#최상위 + 최상위
elif self.map_num == 2:
if self.space == 1:
score = font.render("?", True, (255,0,0))
SCREEN.blit(score, (self.rect.x+10, 300))
elif self.space == 3:
score = font.render("???????????????", True, (255,0,0))
SCREEN.blit(score, (self.rect.x-15, 300))
elif self.space == 5:
score = font.render("최상위밖에 없잖아??!!??", True, (255,0,0))
SCREEN.blit(score, (self.rect.x-15, 300))
self.image = pygame.image.load("tomatotomato.png")
self.image = pygame.transform.scale(self.image, (120, 120))
elif self.space == 7:
pass
#뽑기
elif self.map_num == 3:
if self.space == 1:
score = font.render("뽑아보자!", True, (0,0,0))
SCREEN.blit(score, (self.rect.x-15, 300))
if self.one != 0:
SCREEN.blit(pop1, (0,0))
#self.one = 0
elif self.two != 0:
SCREEN.blit(pop2, (0,0))
#self.two = 0
elif self.three != 0:
SCREEN.blit(pop3, (0,0))
#self.three = 0
elif self.four != 0:
SCREEN.blit(pop4, (0,0))
#self.four = 0
if self.space == 3:
score = font.render("풀어보자!", True, (0,0,0))
SCREEN.blit(score, (self.rect.x-15, 300))
#studying - 열공 중
elif self.map_num == 4:
if self.space == 1:
score = font.render("열공열공!", True, (255,0,0))
SCREEN.blit(score, (self.rect.x-15, 300))
elif self.space % 2 == 1:
score = font.render("열공중", True, (255,0,0))
SCREEN.blit(score, (self.rect.x-15, 300))
def change(self):
# 토마토값을 바꾸는 곳
if 1<=tkey < 5:
score = font.render("토마토의 모양을 바꿀 수 있어요!", True, (255,0,255))
SCREEN.blit(score, (150,300))
if self.tomato %4 == 0:
self.image = pygame.image.load("tomato.png")
# ũ?? ????
self.image = pygame.transform.scale(self.image, (100, 100))
elif self.tomato % 4 == 1:
self.image = pygame.image.load("tomatotomato.png")
# ũ?? ????
self.image = pygame.transform.scale(self.image, (100, 100))
elif self.tomato % 4 == 2:
self.image = pygame.image.load("sadtomato.png")
# ũ?? ????
self.image = pygame.transform.scale(self.image, (100, 100))
elif self.tomato % 4 == 3:
self.image = pygame.image.load("redtomato.png")
# ũ?? ????
self.image = pygame.transform.scale(self.image, (100, 100))
def main():
global SCREEN, WINDOW_WIDTH, WINDOW_HEIGHT, base, map_num, tkey
map_num = 0
tkey = 0
# pygame ?ʱ?ȭ ?? ??ũ?? ????
pygame.init()
SCREEN = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("happytomato")
#SCREEN.blit(base, (800,430))
clock = pygame.time.Clock()
# ?÷??̾? ?ڵ??? ????
player = Car()
player.load_car()
playing = True
while playing:
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
playing = False
pygame.quit()
sys.exit()
# ȭ??ǥ Ű?? ?̿??ؼ? ?÷??̾??? ?????? ?Ÿ??? ???????ش?.
# Ű?? ???? ?????? ?Ÿ??? 0???? ?Ѵ?.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
player.dx = 5
elif event.key == pygame.K_LEFT:
player.dx = -5
if event.key == pygame.K_SPACE:
player.space += 1
if player.isJump == 0:
player.jump(1)
if event.key == pygame.K_t:
player.tomato += 1
tkey += 1
if event.key == pygame.K_1:
player.one = 1
elif event.key == pygame.K_2:
player.two = 1
elif event.key == pygame.K_3:
player.three = 1
elif event.key == pygame.K_4:
player.four = 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
player.dx = 0
elif event.key == pygame.K_LEFT:
player.dx = 0
elif event.key == pygame.K_t:
pass
# ?????? ???????
player.map()
player.conversation()
player.draw_car()
player.move_x()
player.update()
player.check_screen()
player.change()
pygame.display.flip()
# ?ʴ? ?????? ????
clock.tick(60)
if __name__ == '__main__':
main()
'Computer Science > 알고리즘' 카테고리의 다른 글
[8.21] pygame_ 토마토 수학게임 (0) | 2021.08.21 |
---|---|
[8.13] 파이썬 pygame - 토마토 수학교실 (0) | 2021.08.13 |
[8.11] 파이썬 조합 문제 (0) | 2021.08.11 |
[8.10] 파이썬 pygame 예시 (0) | 2021.08.10 |
[4.17] 문제 18. 최대 수익 알고리즘 (0) | 2021.04.17 |