我的代码:
import pygame
from sys import exit
from random import randint
def display_score():
white = (255, 255, 255)
font = pygame.font.Font('Pixeboy-z8XGD.ttf', 64)
score = int(pygame.time.get_ticks() / 1000) - start_time
score_surface = font.render(f'score: {score}', False, white)
score_rect = score_surface.get_rect(topright=(1354, 12))
screen.blit(score_surface, score_rect)
return score
def obstacle_movement(obstacle_list):
if obstacle_list:
for obstacle_rect in obstacle_list:
obstacle_rect.x += 5
screen.blit(bomb_surface, obstacle_rect)
return obstacle_list
# score_rect = score_surface.get_rect(topright=(1354, 12))
pygame.init()
screen = pygame.display.set_mode((1366, 768))
pygame.display.set_caption('Cake Bomb')
clock = pygame.time.Clock()
game_active = False
start_time = 0
score = 0
black = (0, 0, 0)
white = (255, 255, 255)
midnight_blue = (30, 27, 79)
# bg and factory
bg_surface = pygame.image.load('bg4.png')
factory_surface = pygame.image.load('factoryupdate.png')
# fonts
font = pygame.font.Font('Pixeboy-z8XGD.ttf', 64)
font_game = pygame.font.Font('Pixeboy-z8XGD.ttf', 290)
font_tiny = pygame.font.Font('Pixeboy-z8XGD.ttf', 32)
font_tiny_two = pygame.font.Font('Pixeboy-z8XGD.ttf', 40)
welcome_font = pygame.font.Font('Elfboyclassic-PKZgZ.ttf', 290)
# start game
welcome_surface = font_tiny_two.render('Welcome to...', True, white)
welcome_rect = welcome_surface.get_rect(topright=(785, 89))
game_s_surface = font_game.render('CAKE', True, white)
start_surface = font_game.render('BOMB', True, white)
game_s_rect = game_s_surface.get_rect(topright=(960, 110))
start_rect = start_surface.get_rect(topright=(960, 270))
press_start_surface = font.render('Press the space', True, white)
press_start_rect = press_start_surface.get_rect(topright=(897, 438))
to_start_surface = font.render('to start', True, white)
to_start_rect = to_start_surface.get_rect(topright=(785, 478))
# game over
game_surface = font_game.render('GAME', True, midnight_blue)
over_surface = font_game.render('OVER', True, midnight_blue)
game_rect = game_surface.get_rect(topright=(960, 110))
over_rect = over_surface.get_rect(topright=(960, 270))
press_try_surface = font.render('Press the space', True, midnight_blue)
press_try_rect = press_try_surface.get_rect(topright=(897, 438))
try_surface = font.render('to try again', True, midnight_blue)
try_rect = try_surface.get_rect(topright=(847, 478))
bomb_x_pos = -700
cupcake_x_pos = -50
# bomb
bomb_surface = pygame.image.load('bomb2.png')
bomb_rect = bomb_surface.get_rect(bottomright=(bomb_x_pos, 615))
# cupcake
cupcake_surface = pygame.image.load('cupcake.png')
cupcake_rect = cupcake_surface.get_rect(bottomright=(cupcake_x_pos, 668))
obstacle_rect_list = []
obstacle_timer = pygame.USEREVENT + 1
pygame.time.set_timer(obstacle_timer, 900)
meow1_surface = pygame.image.load('meow1.png')
meow1_rect = meow1_surface.get_rect(bottomright=(945, 667.5))
meow3_surface = pygame.image.load('meow3.png')
meow3_rect = meow3_surface.get_rect(bottomright=(914, 677.5))
explosion_surface = pygame.image.load('explosion.png')
explosion_rect = explosion_surface.get_rect(bottomright=(945, 700.5))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == obstacle_timer and game_active:
obstacle_rect_list.append(bomb_surface.get_rect(bottomright+(randint(900, 1100), 615)))
if game_active:
score = display_score()
keys = pygame.key.get_pressed()
screen.blit(bg_surface, (0, 0))
screen.blit(meow1_surface, meow1_rect)
screen.blit(factory_surface, (0, 0))
obstacle_rect_list = obstacle_movement(obstacle_rect_list)
#screen.blit(bomb_surface, bomb_rect)
#bomb_rect.left += 12
#if bomb_rect.left > 1366:
# bomb_rect.left = -170
screen.blit(cupcake_surface, cupcake_rect)
cupcake_rect.left += 12
if cupcake_rect.left > 1366:
cupcake_rect.left = -150
display_score()
if game_active:
if keys[pygame.K_RETURN]:
screen.blit(bg_surface, (0, 0))
screen.blit(meow3_surface, meow3_rect)
screen.blit(factory_surface, (0, 0))
screen.blit(bomb_surface, bomb_rect)
screen.blit(cupcake_surface, cupcake_rect)
display_score()
if keys[pygame.K_RETURN] and meow3_rect.colliderect(cupcake_rect):
screen.blit(bg_surface, (0, 0))
screen.blit(meow3_surface, meow3_rect)
screen.blit(factory_surface, (0, 0))
screen.blit(bomb_surface, bomb_rect)
display_score()
cupcake_rect.right = -900
if keys[pygame.K_RETURN] and meow3_rect.colliderect(bomb_rect):
screen.blit(bg_surface, (0, 0))
screen.blit(meow3_surface, meow3_rect)
screen.blit(factory_surface, (0, 0))
screen.blit(bomb_surface, bomb_rect)
screen.blit(cupcake_surface, cupcake_rect)
screen.blit(explosion_surface, explosion_rect)
display_score()
game_active = False
else:
keys_start = pygame.key.get_pressed()
if score == 0:
screen.blit(bg_surface, (0, 0))
screen.blit(factory_surface, (0, 0))
screen.blit(welcome_surface, welcome_rect)
screen.blit(game_s_surface, game_s_rect)
screen.blit(start_surface, start_rect)
screen.blit(press_start_surface, press_start_rect)
screen.blit(to_start_surface, to_start_rect)
if keys_start[pygame.K_SPACE]:
game_active = True
start_time = int(pygame.time.get_ticks() / 1000)
else:
screen.blit(explosion_surface, explosion_rect)
pygame.time.wait(200)
screen.blit(bg_surface, (0, 0))
screen.blit(factory_surface, (0, 0))
screen.blit(game_surface, game_rect)
screen.blit(over_surface, over_rect)
screen.blit(press_try_surface, press_try_rect)
screen.blit(try_surface, try_rect)
final_score_surface = font_tiny.render(f'score: {score}', False, midnight_blue)
final_score_rect = final_score_surface.get_rect(topright=(740, 520))
screen.blit(final_score_surface, final_score_rect)
keys_enter = pygame.key.get_pressed()
if keys_enter[pygame.K_SPACE]:
game_active = True
bomb_rect.right = -700
cupcake_rect.right = -50
start_time = int(pygame.time.get_ticks() / 1000)
pygame.display.update()
clock.tick(60)
错误: 第 103 行,在 障碍物矩形列表.append(bomb_surface.get_rect(bottomright=(randint(900, 1100), 615))) AttributeError:“NoneType”对象没有属性“append”
我尝试更改特定代码块的位置,因为这之前对我有用,但没有用。我只是一个初学者,所以对我要放轻松。抱歉有点长。