gpt4 book ai didi

python - 在 Pygame 中将开始屏幕切换到游戏

转载 作者:太空宇宙 更新时间:2023-11-03 19:51:00 24 4
gpt4 key购买 nike

我想将开始屏幕切换到游戏。当我按下回车键时应该会发生这种情况。但是当我按下回车键时发生的事情实际上是退出游戏。

这是代码:

import random
from time import sleep

import pygame

screen = pygame.display.set_mode([800,600], 0 , 32)
startscreen = pygame.image.load(".\\img\\startscreen.png")

while True:
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
quit()
sys.exit()

if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_RETURN):
self.initialize()

screen.blit(startscreen,(0,0))
pygame.display.update()


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


def initialize(self):

self.crashed = False

self.carImg = pygame.image.load('.\\img\\police.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\\enemy_car_1.png')
self.enemy_car_startx = random.randrange(200, 600)
self.enemy_car_starty = -600
self.enemy_car_speed = 5
self.enemy_car_width = 49
self.enemy_car_height = 100

# Background
self.bgImg = pygame.image.load('.\\img\\newroad.jpg')
self.bg_x1 = (self.display_width / 4) - (360 / 4)
self.bg_x2 = (self.display_width / 4) - (360 / 4)
self.bg_y1 = 0
self.bg_y2 = -600
self.bg_speed = 3
self.count = 0

#Music
self.back_music = pygame.mixer.music.load(".\\music\\spy_hunter_nes_music.mp3")
self.back_music = pygame.mixer.music.play(-1)

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 Dodge')
self.run_car()

def run_car(self):

while not self.crashed:

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_road()

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(200, 600)

self.car(self.car_x_coordinate, self.car_y_coordinate)
self.highscore(self.count)
self.count += 1
if (self.count % 100 == 0):
self.enemy_car_speed += 1
self.bg_speed += 1

if self.car_y_coordinate < self.enemy_car_starty + self.enemy_car_height:
if self.car_x_coordinate -15 > self.enemy_car_startx -15 and self.car_x_coordinate -15 < self.enemy_car_startx -15 + self.enemy_car_width -15 or self.car_x_coordinate -15 + self.car_width -15 > self.enemy_car_startx -15 and self.car_x_coordinate -15 + self.car_width -15 < self.enemy_car_startx -15 + self.enemy_car_width -15:
self.crashed = True
self.display_message("YOU LOSE !!!")

if self.car_x_coordinate < 150 or self.car_x_coordinate > 650:
self.crashed = True
self.display_message("YOU LOSE !!!")

pygame.display.update()
self.clock.tick(60)

def display_message(self, msg):
font = pygame.font.SysFont("luckiestguy", 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_road(self):
self.gameDisplay.blit(self.bgImg, (self.bg_x1, self.bg_y1))
self.gameDisplay.blit(self.bgImg, (self.bg_x2, self.bg_y2))

self.bg_y1 += self.bg_speed
self.bg_y2 += self.bg_speed

if self.bg_y1 >= self.display_height:
self.bg_y1 = -600

if self.bg_y2 >= self.display_height:
self.bg_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("luckiestguy", 20)
text = font.render("Score : " + str(count), True, self.white)
self.gameDisplay.blit(text, (0, 0))

def display_credit(self):
font = pygame.font.SysFont("luckiestguy", 14)

if __name__ == '__main__':
car_racing = CarRacing()
car_racing.racing_window()

这是主要问题:

screen = pygame.display.set_mode([800,600], 0 , 32)
startscreen = pygame.image.load(".\\img\\startscreen.png")

while True:
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
quit()
sys.exit()

if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_RETURN):
self.initialize()

screen.blit(startscreen,(0,0))
pygame.display.update()

希望大家能帮帮我。顺便说一句,这是我测试的一个项目。我已经尝试了很多改变,但没有任何改变。

enter image description here

enter image description here

enter image description here

最佳答案

您在 while 循环中使用 self.initialize(),但 self. 只能在 class 内部使用>

您应该使用running = True/False退出while循环,然后使用car_racing.initialize()

if __name__ == '__main__':
screen = pygame.display.set_mode([800,600], 0 , 32)
startscreen = pygame.image.load(".\\img\\startscreen.png")

running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
sys.exit()

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
#self.initialize()
running = False

screen.blit(startscreen,(0,0))
pygame.display.update()

car_racing = CarRacing()
car_racing.initialize()
car_racing.racing_window()

关于python - 在 Pygame 中将开始屏幕切换到游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59845199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com