#-*- 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.Font('freesansbold.ttf', 50)
redtomato = pygame.image.load('tomatotomato.png')
happytomato = pygame.image.load('redtomato.png')
sadtomato = pygame.image.load('sadtomato.png')
mansay = pygame.image.load('tomato.png')
base = pygame.image.load('base.png')
base = pygame.transform.scale(base, (800, 70))
icon = pygame.image.load('tomato.png')
pygame.display.set_icon(icon)
WHITE = (255,255,255)
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
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 = 100
self.rect.bottom = WINDOW_HEIGHT - 30
# ?ڵ????? ??ũ???? ????
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:
self.rect.bottom = WINDOW_HEIGHT
self.isJump = 0
self.v = VELOCITY
def map(self):
if self.map_num == 0:
if self.rect.x < 0:
self.rect.x = 0
elif 0 < self.rect.x < 100:
score = font.render("Hi :)", True, (0,0,0))
SCREEN.blit(score, (self.rect.x, 300))
elif 100 < self.rect.x < 500:
score = font.render("Hi :)", True, (0,0,0))
SCREEN.blit(score, (self.rect.x, 300))
score = font.render("I'm Tomato", True, (0,0,0))
SCREEN.blit(score, (self.rect.x + 100, 300))
elif 500 < self.rect.x < 1000:
score = font.render("Welcome to", True, (0,0,0))
SCREEN.blit(score, (self.rect.x - 300, self.rect.bottom - 500))
score = font.render("Math world!", True, (0,0,0))
SCREEN.blit(score, (self.rect.x - 400, self.rect.bottom - 300))
if self.rect.x > 700:
self.subimage = pygame.image.load('love.png')
self.subimage = pygame.transform.scale(self.subimage, (100,100) )
SCREEN.blit(self.subimage, (50, 50))
elif self.rect.x == 1000:
self.rect.x = 0
self.map_num += 1
if self.map_num == 1:
if self.rect.x < 0:
self.rect.x = 0
elif 0 < self.rect.x < 100:
score = font.render("Hi :)", True, (0,0,0))
SCREEN.blit(score, (self.rect.x, 300))
elif 100 < self.rect.x < 500:
score = font.render("Hi :)", True, (0,0,0))
SCREEN.blit(score, (self.rect.x, 300))
score = font.render("I'm Tomato", True, (0,0,0))
SCREEN.blit(score, (self.rect.x + 100, 300))
elif 500 < self.rect.x < 1000:
score = font.render("Welcome to", True, (0,0,0))
SCREEN.blit(score, (self.rect.x - 300, self.rect.bottom - 500))
score = font.render("Math world!", True, (0,0,0))
SCREEN.blit(score, (self.rect.x - 400, self.rect.bottom - 300))
elif self.rect.x == 1000:
self.rect.x = 0
self.map_num += 1
def conversation(self):
if space == 1:
score = font.render("Hi", True, (0,0,0))
SCREEN.blit(score, (self.rect.x, self.rect.bottom - 300))
def change(self):
if space %4 == 0:
self.image = pygame.image.load("tomatotomato.png")
# ũ?? ????
self.image = pygame.transform.scale(self.image, (100, 100))
elif space % 4 == 1:
self.image = pygame.image.load("tomato.png")
# ũ?? ????
self.image = pygame.transform.scale(self.image, (100, 100))
elif space % 4 == 2:
self.image = pygame.image.load("sadtomato.png")
# ũ?? ????
self.image = pygame.transform.scale(self.image, (100, 100))
elif space % 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, space, map_num
space = 0
map_num = 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:
space += 1
if player.isJump == 0:
player.jump(1)
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(WHITE)
player.draw_car()
player.conversation()
player.move_x()
player.update()
player.check_screen()
player.map()
player.change()
pygame.display.flip()
# ?ʴ? ?????? ????
clock.tick(60)
if __name__ == '__main__':
main()
'Computer Science > 알고리즘' 카테고리의 다른 글
[8.11] 파이썬 pygame - 토마토 수학게임 (0) | 2021.08.11 |
---|---|
[8.11] 파이썬 조합 문제 (0) | 2021.08.11 |
[4.17] 문제 18. 최대 수익 알고리즘 (0) | 2021.04.17 |
[4.17] 문제 17. 가짜 동전 찾기 알고리즘 (0) | 2021.04.17 |
[4.17] 응용 문제 1. 미로 찾기 알고리즘 (0) | 2021.04.17 |