【问题标题】:Trying to call multiple sprites to the screen causes glitching?试图将多个精灵调用到屏幕上会导致故障?
【发布时间】:2019-11-23 08:03:27
【问题描述】:

我正在尝试制作一个游戏,其中石头从屏幕上掉下来,你必须躲避它们。我为我的岩石做了一个类,我使用一个列表来附加岩石。但是当我运行它时,岩石只是在屏幕周围出现故障,出现在某些帧中,而不是在其他帧中,而它们也没有从屏幕上掉下来。我试图为该运动创建一个功能,但这也不起作用。我需要帮助。谢谢 这是我的意思的视频: 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))

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    您需要在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() 基本相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 2019-12-16
      • 2014-04-22
      相关资源
      最近更新 更多