我正在使用 Thonny 用 pygame 制作游戏。我打算把它的不同部分分开写,写完后再把它们放在一起。一切都按预期进行,直到我将前两个部分结合起来,它们是:
#text
import pygame
pygame.init()
white = (255, 255, 255)
green = (100, 245, 180)
black = (0, 0, 0)
X = 700
Y = 500
screen = pygame.display.set_mode((X, Y))
pygame.display.set_caption('Show Text')
font1 = pygame.font.Font('freesansbold.ttf', 36)
text1 = font1.render('YOU DIED', True, green, black)
textRect1 = text1.get_rect()
textRect1.center = (X // 2, Y // 2 - 15)
font2 = pygame.font.Font('freesansbold.ttf', 20)
text2 = font2.render('- tap 1 to retry -', True, green, black)
textRect2 = text2.get_rect()
textRect2.center = (X // 2, Y // 2 + 15)
fail = False
while True:
if fail:
screen.fill(black)
screen.blit(text1, textRect1)
screen.blit(text2, textRect2)
for event in pygame.event.get():
#just for test
if event.type == pygame.KEYUP:
fail = True
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
和
import random
class Teacher():
def __init__(self, score_if_drink, score_if_book, score_if_sleep):
self.score_if_drink = score_if_drink
self.score_if_book = score_if_book
self.score_if_sleep = score_if_sleep
class Action():
def __init__(self, bl, vscore, vmood):
self.bl = bl
self.vscore = vscore
self.vmood = vmood
def if_true(score, mood, vscore, vmood):
score += vscore
mood += vmood
return score, mood
#初始數值
current_score = 5
current_mood = 5
step = 0
#隨機數值
teachers = []
tcnum = 5
for i in range(tcnum):
score_if_drink = random.randint(-3, 1)
score_if_book = random.randint(0, 2)
score_if_sleep = random.randint(-2, 0)
tcx = Teacher(score_if_drink = score_if_drink, score_if_book = score_if_book, score_if_sleep = score_if_sleep)
teachers.append(tcx)
next_class = random.randint(0, tcnum - 1)
#行動設定
DRINK = Action(bl = False, vscore = teachers[next_class].score_if_drink, vmood = 2)
BOOK = Action(bl = False, vscore = teachers[next_class].score_if_book, vmood = -2)
SLEEP = Action(bl = False, vscore = teachers[next_class].score_if_sleep, vmood = 3)
action = [DRINK, BOOK, SLEEP]
print("drink: mood+2, score ???;\ntake out your book: mood-2, score+0~2;\nsleep: mood+3, score-0~2")
#主程式
while True:
if current_score > 0 and current_mood > 0:
a = input("press 0 to drink, press 1 to take out your book, press 2 to sleep:")
if a == "0":
DRINK.bl = True
else:
DRINK.bl = False
if a == "1":
BOOK.bl = True
else:
BOOK.bl = False
if a == "2":
SLEEP.bl = True
else:
SLEEP.bl = False
#結果
if DRINK.bl == True:
current_score, current_mood = Action.if_true(current_score, current_mood, DRINK.vscore, DRINK.vmood)
DRINK.bl == False
step += 1
elif BOOK.bl == True:
current_score, current_mood = Action.if_true(current_score, current_mood, BOOK.vscore, BOOK.vmood)
BOOK.bl == False
step += 1
elif SLEEP.bl == True:
current_score, current_mood = Action.if_true(current_score, current_mood, SLEEP.vscore, SLEEP.vmood)
SLEEP.bl == False
step += 1
print(current_score, current_mood)
if step == 2:
next_class = random.randint(0, tcnum - 1)
print("next class")
step = 0
#失敗
else:
print("you died:)")
b = input("tap 1 to replay:")
if b == "1":
current_score = 5
current_mood = 5
这两个程序本身有问题吗?还是我把它们放在一起时出了什么问题?
import pygame
import random
class Teacher():
def __init__(self, score_if_drink, score_if_book, score_if_sleep):
self.score_if_drink = score_if_drink
self.score_if_book = score_if_book
self.score_if_sleep = score_if_sleep
class Action():
def __init__(self, bl, vscore, vmood):
self.bl = bl
self.vscore = vscore
self.vmood = vmood
def if_true(score, mood, vscore, vmood):
score += vscore
mood += vmood
return score, mood
pygame.init()
#視窗
X = 700
Y = 500
screen = pygame.display.set_mode((X, Y))
pygame.display.set_caption("School")
#文字
green = (100, 245, 180)
black = (0, 0, 0)
font1 = pygame.font.Font('freesansbold.ttf', 36)
text1 = font1.render('YOU DIED', True, green, black)
textRect1 = text1.get_rect()
textRect1.center = (X // 2, Y // 2 - 15)
font2 = pygame.font.Font('freesansbold.ttf', 20)
text2 = font2.render('- tap 1 to retry -', True, green, black)
textRect2 = text2.get_rect()
textRect2.center = (X // 2, Y // 2 + 15)
#初始數值
current_score = 5
current_mood = 5
step = 0
#隨機數值
teachers = []
tcnum = 5
for i in range(tcnum):
score_if_drink = random.randint(-3, 1)
score_if_book = random.randint(0, 2)
score_if_sleep = random.randint(-2, 0)
tcx = Teacher(score_if_drink = score_if_drink, score_if_book = score_if_book, score_if_sleep = score_if_sleep)
teachers.append(tcx)
next_class = random.randint(0, tcnum - 1)
#行動設定
DRINK = Action(bl = False, vscore = teachers[next_class].score_if_drink, vmood = 2)
BOOK = Action(bl = False, vscore = teachers[next_class].score_if_book, vmood = -2)
SLEEP = Action(bl = False, vscore = teachers[next_class].score_if_sleep, vmood = 3)
action = [DRINK, BOOK, SLEEP]
print("drink: mood+2, score ???;\ntake out your book: mood-2, score+0~2;\nsleep: mood+3, score-0~2")
#主程式
while True:
screen.fill(black)
screen.blit(text1, textRect1)
screen.blit(text2, textRect2)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#行動
if current_score > 0 and current_mood > 0:
a = input("press 0 to drink, press 1 to take out your book, press 2 to sleep:")
if a == "0":
DRINK.bl = True
else:
DRINK.bl = False
if a == "1":
BOOK.bl = True
else:
BOOK.bl = False
if a == "2":
SLEEP.bl = True
else:
SLEEP.bl = False
#結果
if DRINK.bl == True:
current_score, current_mood = Action.if_true(current_score, current_mood, DRINK.vscore, DRINK.vmood)
DRINK.bl == False
step += 1
elif BOOK.bl == True:
current_score, current_mood = Action.if_true(current_score, current_mood, BOOK.vscore, BOOK.vmood)
BOOK.bl == False
step += 1
elif SLEEP.bl == True:
current_score, current_mood = Action.if_true(current_score, current_mood, SLEEP.vscore, SLEEP.vmood)
SLEEP.bl == False
step += 1
print(current_score, current_mood)
if step == 2:
next_class = random.randint(0, tcnum - 1)
print("next class")
step = 0
#失敗
else:
print("you died:)")
b = input("tap 1 to replay:")
if b == "1":
current_score = 5
current_mood = 5