我为一个大学项目用 python 编写了一个小游戏。它处于早期阶段,但我在运行时遇到问题。当我运行 .py 文件时,会出现一个黑色窗口。当我按“X”按钮关闭窗口时,只有我的背景显示一秒钟,然后窗口关闭。简而言之,只有当我关闭窗口时,我的游戏才会运行。
import pygame
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (50, 50, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
player_speed = 10
class Player(pygame.sprite.Sprite):
change_x = 0
change_y = 0
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface([10, 10])
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
self.change_x = 0
self.change_y = 0
#self.walls = None
def changespeed(self, x, y):
self.change_x += x
self.change_y += y
def move(self, walls):
self.rect.x += self.change_x
block_hit_list = pygame.sprite.spritecollide(self, walls, False)
for block in block_hit_list:
if self.change_x > 0:
self.rect.right = block.rect.left
else:
self.rect.left = block.rect.right
self.rect.y += self.change_y
block_hit_list = pygame.sprite.spritecollide(self, walls, False)
for block in block_hit_list:
if self.change_y > 0:
self.rect.bottom = block.rect.top
else:
self.rect.top = block.rect.bottom
class Wall(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.set_alpha(0)
self.image.fill([255, 255, 255])
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
class Level(object):
wall_list = None
enemy_sprites = None
def __init__(self):
self.wall_list = pygame.sprite.Group()
self.enemy_sprites = pygame.sprite.Group()
MMTTMM
相关分类