雷达线在使用pygame时不出现在可视化界面中



当编码一组源自给定精灵的雷达时,代码似乎可以工作并且演示视觉加载正常,但雷达本身并未出现在视觉界面上。我如何重新编码它,使雷达出现在屏幕上,起源于精灵?

sprite.py:

import pygame as pg
from settings import *
from tilemap import *
import math
vec = pg.math.Vector2
show_debug = True
SCREEN = pg.surface.Surface((WIDTH, HEIGHT))
def collide_with_walls(sprite, group, dir):
if dir == 'x':
hits = pg.sprite.spritecollide(sprite, group, False, sprite.collide_hit_rect)
if hits:
if sprite.vel.x > 0:
sprite.pos.x = hits[0].rect.left - sprite.hit_rect.width / 2
if sprite.vel.x < 0:
sprite.pos.x = hits[0].rect.right + sprite.hit_rect.width / 2
sprite.vel.x = 0
sprite.hit_rect.centerx = sprite.pos.x
if dir == 'y':
hits = pg.sprite.spritecollide(sprite, group, False, sprite.collide_hit_rect)
if hits:
if sprite.vel.y > 0:
sprite.pos.y = hits[0].rect.top - sprite.hit_rect.height / 2
if sprite.vel.y < 0:
sprite.pos.y = hits[0].rect.bottom + sprite.hit_rect.height / 2
sprite.vel.y = 0
sprite.hit_rect.centery = sprite.pos.y
class Player(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.player_img
self.rect = self.image.get_rect()
self.hit_rect = PLAYER_HIT_RECT
self.hit_rect.center = self.rect.center
self.vel = vec(0, 0)
self.pos = vec(x, y) * TILESIZE
self.rot = 0
self.angle = 0
self.radars = []
def get_keys(self):
self.rot_speed = 0
self.vel = vec(0, 0)
self.vx, self.vy = 0, 0
keys = pg.key.get_pressed()
if keys[pg.K_LEFT] or keys[pg.K_a]:
self.rot_speed = PLAYER_ROT_SPEED
if keys[pg.K_RIGHT] or keys[pg.K_d]:
self.rot_speed = -PLAYER_ROT_SPEED
if keys[pg.K_UP] or keys[pg.K_w]:
self.vel = vec(PLAYER_SPEED, 0).rotate(-self.rot)
if keys[pg.K_DOWN] or keys[pg.K_s]:
self.vel = vec(-PLAYER_SPEED / 2, 0).rotate(-self.rot)
def collide_hit_rect(one, two):
return one.hit_rect.colliderect(two.rect)
def update(self):
self.get_keys()
self.rot = (self.rot + self.rot_speed * self.game.dt) % 360
self.image = pg.transform.rotate(self.game.player_img, self.rot)
self.rect = self.image.get_rect()
self.rect.center = self.pos
self.pos += self.vel * self.game.dt
self.hit_rect.centerx = self.pos.x
collide_with_walls(self, self.game.walls, 'x')
self.hit_rect.centery = self.pos.y
collide_with_walls(self, self.game.walls, 'y')
self.rect.center = self.hit_rect.center
self.radars.clear()
for radar_angle in (-60, -30, 0, 30, 60):
self.radar(radar_angle)
def radar(self, radar_angle):
length = 0
x = int(self.rect.center[0])
y = int(self.rect.center[1])
try:
while not SCREEN.get_at((x, y)) == pg.Color(2, 105, 31, 255) and length < 200:
length += 1
x = int(self.rect.center[0] +
math.cos(math.radians(self.angle + radar_angle)) * length)
y = int(self.rect.center[1] -
math.sin(math.radians(self.angle + radar_angle)) * length)

except IndexError:
pass
if show_debug:
pg.draw.line(SCREEN, (225, 225, 225, 225), self.rect.center,
(x, y), 1)
pg.draw.circle(SCREEN, (0, 225, 0, 0), (x, y), 3)
dist = int(
math.sqrt(
math.pow(self.rect.center[0] - x, 2) +
math.pow(self.rect.center[1] - y, 2)))
self.radars.append([radar_angle, dist])

tilemap.py:

import pygame as pg
import pytmx
from settings import *
def collide_hit_rect(one, two):
return one.hit_rect.colliderect(two.rect)
class Map:
def __init__(self, filename):
self.data = []
with open(filename, 'rt') as f:
for line in f:
self.data.append(line.strip())
self.tilewidth = len(self.data[0])
self.tileheight = len(self.data)
self.width = self.tilewidth * TILESIZE
self.height = self.tileheight * TILESIZE
class TiledMap:
def __init__(self, filename):
tm = pytmx.load_pygame(filename, pixelalpha=True)
self.width = tm.width * tm.tilewidth
self.height = tm.height * tm.tileheight
self.tmxdata = tm
def render(self, surface):
ti = self.tmxdata.get_tile_image_by_gid
for layer in self.tmxdata.visible_layers:
if isinstance(layer, pytmx.TiledTileLayer):
for x, y, gid, in layer:
tile = ti(gid)
if tile:
surface.blit(tile, (x * self.tmxdata.tilewidth,
y * self.tmxdata.tileheight))
def make_map(self):
temp_surface = pg.Surface((self.width, self.height))
self.render(temp_surface)
return temp_surface
class Camera:
def __init__(self, width, height):
self.camera = pg.Rect(0, 0, width, height)
self.width = width
self.height = height
def apply(self, entity):
return entity.rect.move(self.camera.topleft)
def apply_rect(self, rect):
return rect.move(self.camera.topleft)
def update(self, target):
x = -target.rect.centerx + int(WIDTH / 2)
y = -target.rect.centery + int(HEIGHT / 2)
# limit scrolling to map size
x = min(0, x)  # left
y = min(0, y)  # top
x = max(-(self.width - WIDTH), x)  # right
y = max(-(self.height - HEIGHT), y)  # bottom
self.camera = pg.Rect(x, y, self.width, self.height)

settings.py:

import pygame as pg
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
DARKGREY = (40, 40, 40)
LIGHTGREY = (100, 100, 100)
GREEN = (67, 163, 75)
RED = (255, 0, 0)
DARKTEAL = (59, 173, 154)
BLUE = (16, 87, 201)
BLUE2 = (16, 87, 225)
# game settings
WIDTH = 1024   # 16 * 64 or 32 * 32 or 64 * 16
HEIGHT = 768  # 16 * 48 or 32 * 24 or 64 * 12
FPS = 60
TITLE = "Tilemap Demo"
BGCOLOR = BLUE
TILESIZE = 16
GRIDWIDTH = WIDTH / TILESIZE
GRIDHEIGHT = HEIGHT / TILESIZE

#Player settings
PLAYER_SPEED = 100
PLAYER_IMG = 'Fletcher-class DD.png'
PLAYER_ROT_SPEED = 50
PLAYER_HIT_RECT = pg.Rect(0, 0, 35, 35)

我的猜测是问题在于sprite.py中的SCREEN = pg.surface.Surface((WIDTH, HEIGHT)),我不确定如何修复它。

在Pygame中,所有内容都被绘制到与显示相关的Surface对象。在你的例子中,这是SCREEN。如果您在与PyGame显示相关联的Surface上绘制,则在显示中不会立即可见。当使用pygame.display.update()pygame.display.flip()更新显示时,更改变得可见。通常,在每一帧中重新绘制整个场景。在帧开始时,清除显示或绘制背景。这使得之前画的东西都消失了。在帧结束时,显示被更新。因此,在清除显示之后和更新显示之前未绘制的任何内容在场景中都是不可见的。

最新更新