为什么我的代码运行速度真的很慢,这不是前几天的广告,现在它的运行速度非常慢。
它是我大学游戏的一部分,没有人能弄清楚为什么它这么慢,我真的不想重新开始。我想不通的是为什么它的速度这么慢。我不太懂代码,但我试图通过修复,但无论我做什么都无济于事,它仍然无法工作,请帮助
import random
from time import sleep
import time
import pygame
seconds = 0
class CarRacing:
def __init__(self):
pygame.init()
self.display_width = 800
self.display_height = 600
self.black = (0, 0, 0)
self.white = (255, 255, 255)
self.clock = pygame.time.Clock()
self.gameDisplay = None
self.initialize()
def initialize(self):
self.crashed = False
self.carImg = pygame.image.load('img/bojo.png')
self.car_x_coordinate = (self.display_width * 0.45)
self.car_y_coordinate = (self.display_height * 0.8)
self.car_width = 49
# enemy_car
self.enemy_car = pygame.image.load('img/Coal_JE1.png')
self.enemy_car_startx = random.randrange(310, 450)
self.enemy_car_starty = 600
self.enemy_car_speed = 5
self.enemy_car_width = 10
self.enemy_car_height = 1
# Background
self.bgImg1 = pygame.image.load("img/Grassy background.png")
self.bgImg1 = pygame.transform.scale(self.bgImg1, (800,600))
self.bg1_x1 = (self.display_width/2) - 425
self.bg1_x2 = (self.display_width/2) - 425
print (str(self.bg1_x1), str(self.bg1_x2))
self.bg1_y1 = 0
self.bg1_y2 = -600
self.bg_speed = 3
self.count = 0
def car(self, car_x_coordinate, car_y_coordinate):
self.gameDisplay.blit(self.carImg, (car_x_coordinate, car_y_coordinate))
def racing_window(self):
self.gameDisplay = pygame.display.set_mode((self.display_width, self.display_height))
pygame.display.set_caption('Car Race -- :))')
self.run_car()
def run_car(self):
while not self.crashed:
seconds = 0
seconds +=1
time.sleep(1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.crashed = True
# print(event)
if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_LEFT):
self.car_x_coordinate -= 50
if (event.key == pygame.K_RIGHT):
self.car_x_coordinate += 50
self.gameDisplay.fill(self.black)
self.back_ground_raod1()
self.run_enemy_car(self.enemy_car_startx, self.enemy_car_starty)
self.enemy_car_starty += self.enemy_car_speed
if self.enemy_car_starty > self.display_height:
self.enemy_car_starty = 0 - self.enemy_car_height
self.enemy_car_startx = random.randrange(310, 450)
# speed values below
self.car(self.car_x_coordinate, self.car_y_coordinate)
self.highscore(self.count)
self.count += 0
if (self.count % 100 == 0):
self.enemy_car_speed += 1
self.bg_speed += -0
if self.car_y_coordinate < (self.enemy_car_starty + self.enemy_car_height):
if self.car_x_coordinate > self.enemy_car_startx and self.car_x_coordinate < self.enemy_car_startx + self.enemy_car_width or self.car_x_coordinate + self.car_width > self.enemy_car_startx and self.car_x_coordinate + self.car_width < self.enemy_car_startx + self.enemy_car_width:
self.crashed = True
self.display_message("Your the epitome of failure !!!")
if self.car_x_coordinate < 250 or self.car_x_coordinate > 460:
self.crashed = True
self.display_message("You suck!!!")
pygame.display.update(
)
self.clock.tick(60)
def display_message(self, msg):
font = pygame.font.SysFont("comicsansms", 72, True)
text = font.render(msg, True, (255, 255, 255))
self.gameDisplay.blit(text, (400 - text.get_width() // 2, 240 - text.get_height() // 2))
self.display_credit()
pygame.display.update()
self.clock.tick(60)
sleep(1)
car_racing.initialize()
car_racing.racing_window()
def back_ground_raod1(self):
self.gameDisplay.blit(self.bgImg1, (self.bg1_x1, self.bg1_y1))
self.gameDisplay.blit(self.bgImg1, (self.bg1_x2, self.bg1_y2))
self.bg1_y1 += self.bg_speed
self.bg1_y2 += self.bg_speed
if self.bg1_y1 >= self.display_height:
self.bg1_y1 = -600
if self.bg1_y2 >= self.display_height:
self.bg1_y2 = -600
def run_enemy_car(self, thingx, thingy):
self.gameDisplay.blit(self.enemy_car, (thingx, thingy))
def highscore(self, count):
font = pygame.font.SysFont("JakeConsole", 20)
text = font.render("Score : " + str(count), True, self.white)
self.gameDisplay.blit(text, (0, 0))
def display_credit(self):
font = pygame.font.SysFont("JakeConsole", 14)
text = font.render("Thanks to me,", True, self.white)
self.gameDisplay.blit(text, (600, 520))
text = font.render("Jake Edge", True, self.white)
self.gameDisplay.blit(text, (600, 540))
text = font.render("B00", True, self.white)
self.gameDisplay.blit(text, (600, 560))
if __name__ == '__main__':
car_racing = CarRacing()
car_racing.racing_window()
#audio.play_file('audio/mp3')
"