我正在尝试制作一款游戏,其中岩石从屏幕上掉落,你必须躲避它们。我为我的岩石创建了一个类,并使用一个列表来附加岩石。但是当我运行它时,岩石只会在屏幕周围出现故障,出现在某些帧中,而不会出现在其他帧中,同时它们也不会从屏幕上掉下来。我尝试为该运动创建一个功能,但也没有成功。我需要帮助。谢谢 这是我的意思的视频: https://streamable.com/xd26w
import pygame
import random
import math
pygame.init()
GOLD = (255, 215, 0)
def text_objects(text, font):
textSurface = font.render(text, True, GOLD)
return textSurface, textSurface.get_rect()
screenwidth = 500
screenheight = 500
win = pygame.display.set_mode((screenwidth, screenheight))
pygame.display.set_caption('First Game')
bkg = pygame.image.load('2.jpg')
bkg = pygame.transform.scale(bkg, (500,500))
char1 = pygame.image.load('guy.png')
char1 = pygame.transform.scale(char1, (100, 100))
walkRight = []
walkLeft = []
HitAnim = []
Howmany = 4
for i in range(1, 13):
walkRight.append(pygame.transform.scale(pygame.image.load('R' + str(i) + '.png'), (100, 100)))
for i in range(1, 13):
walkLeft.append(pygame.transform.scale(pygame.image.load('L' + str(i) + '.png'), (100, 100)))
rockboom = pygame.image.load('b1.png')
rockboom = pygame.transform.scale(rockboom, (100,100))
GameO = pygame.image.load('Gameover.jpg')
rock = pygame.image.load('b.png')
rock = pygame.transform.scale(rock, (100, 100))
clock = pygame.time.Clock()
class player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.GameOver = False
self.vel = 10
self.walkCount = 0
self.left = False
self.right = False
self.lives = 3
self.hit = 0
self.hit1 = False
def draw(self, win):
if self.walkCount + 1 >= 27:
self.walkCount = 0
if self.left == True:
win.blit(walkLeft[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
pygame.display.update()
elif self.right == True :
win.blit(walkRight[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
else:
win.blit(char1, (self.x, self.y))
class rocky():
def __init__(self):
self.x = random.randrange(0, 430)
self.y = -100
def draw(self, win):
win.blit(rock, (self.x, self.y))
if man.hit1 == True:
win.blit (rockboom, (self.x, self.y))
def move(self):
rockk.y += 1
if rockk.y >= 500:
rockk.x = random.randrange(0, 430)
rockk.y = -100
if man.hit1 == True:
rockk.x = random.randrange(0, 430)
rockk.y = -100
def redrawgamewindow():
win.blit(bkg, (0, 0))
man.draw(win)
largeText = pygame.font.Font('freesansbold.ttf', 30)
TextSurf, TextRect = text_objects("Lives: " + str(man.lives), largeText)
TextRect.center = ((100), (470))
win.blit(TextSurf, TextRect)
pygame.display.flip()
def collided (rockx, rocky, manx, many):
man.hit = 0
distance = math.sqrt((math.pow(rockx-manx, 2) + (math.pow(rocky-many, 2))))
if distance < 75:
man.hit += 1
print("we be touched")
man.hit1 = True
else:
man.hit1 = False
my_list= []
for number in range(Howmany):
my_object = rocky()
my_list.append(my_object)
print ("")
drawing = True
man = player(250, 350, 100, 100)
run = True
while run:
# Setting fps
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
# Getting keys pressed
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and man.x > 0:
man.x -= man.vel
man.left = True
man.right = False
elif keys[pygame.K_RIGHT] and man.x < 500 - man.vel - man.width:
man.x += man.vel
man.left = False
man.right = True
else:
man.left = False
man.right = False
man.walkCount = 0
for rockk in my_list:
rockk.draw(win)
rockk.move()
pygame.display.update()
if man.hit == 1:
man.lives -=1
elif man.hit ==2:
man.lives -=1
elif man.lives <= 0:
print("so uh yeah it worked cool ")
drawing = False
win.blit (GameO, (0, 0))
pygame.display.update()
collided(rockk.x, rockk.y, man.x, man.y)
if drawing:
redrawgamewindow()
win.blit(pygame.image.load('R1.png').convert_alpha(), (200, 200))
最佳答案
您需要在 redrawgamewindow
函数中将所有内容绘制在一起。目前,您正在循环期间绘制岩石,当调用 redrawgamewindow 函数时,它们会消失(该函数所做的第一件事是绘制背景,删除所有内容)。从这里开始闪烁效果。
你的函数应该是:
def redrawgamewindow():
win.blit(bkg, (0, 0))
man.draw(win)
largeText = pygame.font.Font('freesansbold.ttf', 30)
TextSurf, TextRect = text_objects("Lives: " + str(man.lives), largeText)
TextRect.center = ((100), (470))
win.blit(TextSurf, TextRect)
#drawing the rocks
for rockk in my_list:
rockk.draw(win)
pygame.display.flip()
基本上,将所有绘图内容移动到 redrawgamewindow
中就应该可以工作。在主循环中,限制移动岩石,但不绘制它们。此外,无需多次调用 pygame.display.update() 。调用 pygame.display.flip()
(它基本上与函数中的 pygame.display.update()
做同样的事情就足够了。
关于python - 尝试在屏幕上调用多个 Sprite 会导致故障?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59005698/