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