我一直在学习python,刚开始在Pygame中使用编码。我目前的节目是一个角色是苍蝇的游戏,必须避免被苍蝇拍杀死。游戏必须首先在屏幕上的任何位置随机显示半透明的黄色方块。这个方块表示苍蝇拍将在2秒内发射的位置,让玩家有时间飞出该罢工区域。当苍蝇拍时,我想在较暗的正方形的确切位置上对比赛进行不透明的方形。这个方块会在玩家出现时杀死玩家。虽然我计划显示消息并添加其他具有此性质的东西,但不透明的方块(或苍蝇拍)应终止游戏会话。我已经创建了两个正方形,但我需要不透明的正方形来到半透明的正方形之后,并且在同一个位置。每次打击后,我都希望方块消失,然后重复这个过程。如果有人有任何见解,我可以如何去做1)在随机的位置blit半透明的方块和2)在2秒内使较暗的方形“罢工”相同的位置,然后消失并重复,我将非常感激。我已经尝试使用time.sleep()函数,但它没有工作。 非常感谢!!
(请原谅我有点凌乱的代码!)
import pygame, random, time, sys, math
from pygame.locals import *
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Fly Dodger')
black = (0,0,0)
white = (255,255,255)
yellow = (255,255,0)
fly_width = 70
clock = pygame.time.Clock()
crashed = False
flyImg = pygame.image.load("Fly.png")
flyImg = pygame.transform.scale(flyImg, (80, 80))
#THe faintswatter is the faint image/square I want to appear first.
faintSwatter = pygame.image.load("faintswatter.png")
faintSwatter = pygame.transform.scale(faintSwatter, (400, 510))
#The realswatter is the square that appears after the fainter one, in the same position.
realswatter = pygame.image.load("swatter.png")
realswatter = pygame.transform.scale(realswatter, (400, 510))
windowImg = pygame.image.load("window.jpg")
windowImg = pygame.transform.scale(windowImg, (800,600))
pygame.display.update()
def fly(x,y):
gameDisplay.blit(flyImg, (x,y))
def border():
x_change = 0
y_change = 0
def swatter_Function(swatterx, swattery, swaterw, swaterh, color):
pygame.draw.rect(gameDisplay, color, [swatterx, swattery, swaterw, swaterh])
def things_dodged(count):
font = pygame.font.SysFont(None, 25)
text = font.render("Dodged: "+str(count), True, green)
gameDisplay.blit(text, (0,0))
def message_display(text):
largeText = pygame.font.Font("freesansbold.ttf",115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
def gameloop():
x = (display_width * .45)
y = (display_height * .4)
x_change = 0
y_change = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
elif event.key == pygame.K_UP:
y_change = -5
elif event.key == pygame.K_DOWN:
y_change = 5
x += x_change
y += y_change
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
gameDisplay.blit(windowImg, (0,0))
fly(x,y)
gameDisplay.blit(faintSwatter,(1,1))
gameDisplay.blit(realswatter, (2,2))
pygame.display.update()
if x > display_width - fly_width or x < 0:
gameExit = True
if y > display_height - fly_width or y < 0:
gameExit = True
pygame.display.update()
clock.tick(60)
gameloop()
pygame.quit()
quit()
答案 0 :(得分:1)
我做了一些重大改变..寻找#changes。
这些是我使用的图片:http://imgur.com/a/0MOkP
import pygame, random, time, sys, math
from pygame.locals import *
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Fly Dodger')
black = (0,0,0)
white = (255,255,255)
yellow = (255,255,0)
fly_width = 70
clock = pygame.time.Clock()
crashed = False
flyImg = pygame.image.load("Fly.png").convert() #changes
flyImg = pygame.transform.scale(flyImg, (80, 80))
#THe faintswatter is the faint image/square I want to appear first.
faintSwatter = pygame.image.load("faintswatter.png").convert() #changes
faintSwatter = pygame.transform.scale(faintSwatter, (400, 510))
faintSwatter.set_alpha(128) #changes
#The realswatter is the square that appears after the fainter one, in the same position.
realswatter = pygame.image.load("swatter.png").convert() #changes
realswatter = pygame.transform.scale(realswatter, (400, 510))
windowImg = pygame.image.load("window.jpg")
windowImg = pygame.transform.scale(windowImg, (800,600))
pygame.display.update()
def fly(x,y):
gameDisplay.blit(flyImg, (x,y))
def border():
x_change = 0
y_change = 0
def swatter_Function(swatterx, swattery, swaterw, swaterh, color):
pygame.draw.rect(gameDisplay, color, [swatterx, swattery, swaterw, swaterh])
def things_dodged(count):
font = pygame.font.SysFont(None, 25)
text = font.render("Dodged: "+str(count), True, green)
gameDisplay.blit(text, (0,0))
def message_display(text):
largeText = pygame.font.Font("freesansbold.ttf",115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
def gameloop():
x = (display_width * .45)
y = (display_height * .4)
x_change = 0
y_change = 0
#changes
totalPlaytime = 0.0
faintDisplay = True
solidDisplay = False
choosenCoords = False
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
#changes
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
elif event.key == pygame.K_UP:
y_change = -5
elif event.key == pygame.K_DOWN:
y_change = 5
#changes - moved in the loop
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
#changes - moved out of the loop at the end
x += x_change
y += y_change
gameDisplay.blit(windowImg, (0,0))
fly(x,y)
#changes
if faintDisplay and not choosenCoords:
xSwatter = random.randrange(0,display_width-400)
ySwatter = random.randrange(0,display_height-510)
choosenCoords = True
faintSwatterBeginTime = totalPlaytime
if faintDisplay:
if totalPlaytime - faintSwatterBeginTime < 2.0:
gameDisplay.blit(faintSwatter,(xSwatter,ySwatter))
else:
faintDisplay = False
solidDisplay = True
solidSwatterBeginTime = totalPlaytime
if solidDisplay:
if totalPlaytime - solidSwatterBeginTime < 0.5:
gameDisplay.blit(realswatter, (xSwatter,ySwatter))
if ((x+80 > xSwatter) and (x < xSwatter+400) and (y+80 > ySwatter) and (y < ySwatter+510)):
crashed = True
gameExit = True
else:
solidDisplay = False
faintDisplay = True
choosenCoords = False
#changes end
pygame.display.update()
if x > display_width - fly_width or x < 0:
gameExit = True
if y > display_height - fly_width or y < 0:
gameExit = True
#pygame.display.update()
#changes
milliseconds = clock.tick(60)
totalPlaytime += milliseconds/1000.0
gameloop()
pygame.quit()
quit()
我也是初学者..请原谅我这样详细的代码。我所做的改变是:增加时间计算,在一定程度上使更平坦的透明,增加碰撞功能(你也可以使用colliderect),添加算法让微弱的苍蝇拍停留2秒,固体拍摄0.5秒。
答案 1 :(得分:1)
我使用timer
变量并从中减去dt
(增量时间 - 自上次clock.tick
调用以来的时间)以进行倒计时。在下面的示例中,我为当前选定的图像使用变量(swatterImg
)。首先将swatterImg
设置为微弱的,然后如果定时器介于0和1.5s之间,我将其设置为realswatter
图像并执行碰撞检测,如果定时器低于0s,则重置计时器和图像,并为苍蝇拍生成新的位置。 fly
和swatter
是pygame.Rect
,用于存储位置,也用于碰撞检测。在绘图阶段,您只需将飞行图像和当前的苍蝇形图像在各自的位置进行blit即可。 (我已经删除了自动退出游戏的代码,因为这不是示例所必需的,而且对用户来说很烦人。)
import sys
import random
import pygame as pg
pg.init()
displayrect = pg.Rect(0, 0, 800, 600)
gameDisplay = pg.display.set_mode(displayrect.size)
pg.display.set_caption('Fly Dodger')
clock = pg.time.Clock()
flyImg = pg.Surface((80, 80))
flyImg.fill((220, 120, 20))
faintSwatter = pg.Surface((400, 510))
faintSwatter.fill((220, 250, 120))
faintSwatter.set_alpha(128)
realswatter = pg.Surface((400, 510))
realswatter.fill((220, 250, 120))
def gameloop():
fly = pg.Rect(100, 150, 70, 70)
x_change = 0
y_change = 0
swatter = pg.Rect(90, 50, 400, 510)
swatter_timer = 3
swatterImg = faintSwatter # Current swatter image.
dt = 0
gameExit = False
while not gameExit:
for event in pg.event.get():
if event.type == pg.QUIT:
gameExit = True
if event.type == pg.KEYDOWN:
if event.key == pg.K_LEFT:
x_change = -5
elif event.key == pg.K_RIGHT:
x_change = 5
elif event.key == pg.K_UP:
y_change = -5
elif event.key == pg.K_DOWN:
y_change = 5
if event.type == pg.KEYUP:
if event.key == pg.K_LEFT or event.key == pg.K_RIGHT:
x_change = 0
elif event.key == pg.K_UP or event.key == pg.K_DOWN:
y_change = 0
fly.x += x_change
fly.y += y_change
swatter_timer -= dt
# If the timer is below 1.5s and above 0s, change the current
# image and check for collisions with the swatter rect.
if 0 < swatter_timer < 1.5:
swatterImg = realswatter # Change current image.
if fly.colliderect(swatter):
print('Collision!')
# If timer is below 0s, reset timer and image and get a new pos.
elif swatter_timer <= 0:
swatterImg = faintSwatter # Reset current image.
swatter_timer = 3
swatter.topleft = (random.randrange(displayrect.w-400),
random.randrange(displayrect.h-510))
# Draw everything.
gameDisplay.fill((30, 30, 30))
gameDisplay.blit(flyImg, fly)
gameDisplay.blit(swatterImg, swatter)
pg.display.update()
dt = clock.tick(60) / 1000
if __name__ == '__main__':
gameloop()
pg.quit()
sys.exit()