标题:这段代码运行不起来……
只看楼主
Rocket_Pro
Rank: 2
等 级:论坛游民
帖 子:12
专家分:10
注 册:2022-12-17
 问题点数:0 回复次数:1 
这段代码运行不起来……
我尝试运行如 <typeGame.py> 的pygame程序,但是pygame窗口是黑屏的
如果有知道怎么改代码的人,请立即回复,如给出的代码可以在python3.8.x中运行,我将立即支付30可用分!!!
看谁先抢到这30分!!!!!

typeGame.py代码如下:

import sys,random,time,pygame
from pygame.locals import *

def print_text(font,x,y,text,color=(255,255,255)):
    imgText = font.render(text,True,color)
    screen.blit(imgText,(x,y))
   
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Type Test")
font1 = pygame.font.Font(None,24)
font2 = pygame.font.Font(None,200)
white = 255,255,255
yellow = 255,255,0

key_flag = False
correct_answer = 97
seconds = 11
score = 0
clock_start = 0
game_over = True

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            key_flag = True
        elif event.type == KEYUP:
            key_flag = False
            
    keys = pygame.key.get_pressed()
    if keys[K_RETURN]:
        if game_over:
            game_over = False
            score = 0
            seconds = 11
            
    if keys[K_ESCAPE]:
        pygame.quit()
        sys.exit()
        
clock_start = time.clock()
current = time.clock() - clock_start
speed = score * 6
if seconds-current < 0:
    game_over = True
elif current <= 10:
    if keys[correct_answer]:
        correct_answer = random.randint(97,122)
        score += 1
        
screen.fill((0,100,0))

print_text(font1,0,0,"Let's check how fast you can type!")
print_text(font1,0,20,"Try to keep up for 10 seconds...")

if key_flag:
    print_text(font1,500,0,"<Key>")
   
if not game_over:
    print_text(font1,0,80,"Time: " + str(int(seconds-current)))
   
print_text(font1,0,100,"Speed: " + str(speed) + " letters/min")

if game_over:
    print_text(font1,0,160,"Press Enter to try again...")
   
print_text(font2,0,240,chr(correct_answer-32),yellow)

pygame.display.update()


———————————————————————————————————————————————分割线———————————————————————————————————————————————————————————————
搜索更多相关主题的帖子: event 代码 if Type pygame 
2022-12-21 15:06
fall_bernana
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:17
帖 子:240
专家分:2086
注 册:2019-8-16
得分:0 
回复 楼主 Rocket_Pro
我这里测试是可以的
程序代码:
import sys,random,time,pygame
from pygame.locals import *

def print_text(font,x,y,text,color=(255,255,255)):
    imgText = font.render(text,True,color)
    screen.blit(imgText,(x,y))
   
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Type Test")
font1 = pygame.font.Font(None,24)
font2 = pygame.font.Font(None,200)
white = 255,255,255
yellow = 255,255,0

key_flag = False
correct_answer = 97
seconds = 11
score = 0
clock_start = 0
game_over = True
clock = pygame.time.Clock()  # 定时器
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            key_flag = True
        elif event.type == KEYUP:
            key_flag = False
            
    keys = pygame.key.get_pressed()
    if keys[K_RETURN]:
        if game_over:
            game_over = False
            score = 0
            seconds = 11
            
    if keys[K_ESCAPE]:
        pygame.quit()
        sys.exit()
        
    clock_start = time.clock()
    current = time.clock() - clock_start
    speed = score * 6
    if seconds-current < 0:
        game_over = True
    elif current <= 10:
        if keys[correct_answer]:
            correct_answer = random.randint(97,122)
            score += 1
            
    screen.fill((0,100,0))

    print_text(font1,0,0,"Let's check how fast you can type!")
    print_text(font1,0,20,"Try to keep up for 10 seconds...")

    if key_flag:
        print_text(font1,500,0,"<Key>")
    
    if not game_over:
        print_text(font1,0,80,"Time: " + str(int(seconds-current)))
    
    print_text(font1,0,100,"Speed: " + str(speed) + " letters/min")

    if game_over:
        print_text(font1,0,160,"Press Enter to try again...")
    
    print_text(font2,0,240,chr(correct_answer-32),yellow)

    pygame.display.update()
    clock.tick(60)
2023-01-09 16:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-510992-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.055562 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved