מהיר יותר עם הזמן
שיעור 18 מתוך 41 בקורס צרו משחקים עם Pygame של Coddy.
המשחק נעשה קשה יותר ככל שמתקדמים בו. כאן הבלוקים נופלים קצת מהר יותר כל 10 שניות.
pygame.time.set_timer(event_type, ms) שולחת אירוע מסוג זה כל ms אלפיות שנייה. צרו סוג אירוע משלכם באמצעות pygame.USEREVENT, שהוא מספר ש-pygame משאירה פנוי לשימושכם:
SPEED_UP = pygame.USEREVENT + 1
pygame.time.set_timer(SPEED_UP, 10000)
fall_speed = 3
for event in pygame.event.get():
if event.type == SPEED_UP:
fall_speed += 1האירוע מגיע ללולאת האירועים יחד עם שאר האירועים, פעם בכל 10 שניות. לאחר מכן הבלוק נופל באמצעות block.y += fall_speed במקום באמצעות הערך הקבוע 3. משחק חדש מתחיל שוב לאט, לכן גם ההפעלה מחדש קובעת fall_speed = 3.
אתגר
בינוניגרום לבלוקים ליפול מהר יותר בכל 10 שניות.
- מתחת לגופן, צור את
SPEED_UP = pygame.USEREVENT + 1והפעל עבורו טיימר כל10000אלפיות שנייה. - הגדר
fall_speed = 3לפני הלולאה והשתמש בו לנפילה. - הוסף 1 ל־
fall_speedבכל אירוע SPEED_UP, והגדר אותו בחזרה ל־3 כש־R מתחיל משחק חדש.
לחץ על "שחק" והמתן 10 שניות: הבלוקים נופלים מהר יותר. לאחר מכן לחץ על "בדיקה".
נסו בעצמכם
import random
import pygame
pygame.init()
screen = pygame.display.set_mode((480, 360))
pygame.display.set_caption("Catch the Blocks")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 32)
basket = pygame.Rect(200, 330, 80, 16)
block = pygame.Rect(220, 0, 20, 20)
score = 0
lives = 3
paused = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
paused = not paused
elif event.key == pygame.K_r and (lives == 0 or score == 10):
score = 0
lives = 3
basket.x = 200
block.y = -20
block.x = random.randint(0, 460)
if lives > 0 and score < 10 and not paused:
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
basket.x -= 8
if keys[pygame.K_RIGHT]:
basket.x += 8
basket.clamp_ip(screen.get_rect())
block.y += 3
if block.top > 360:
block.y = -20
block.x = random.randint(0, 460)
lives -= 1
if block.colliderect(basket):
block.y = -20
block.x = random.randint(0, 460)
score += 1
screen.fill((30, 30, 46))
pygame.draw.rect(screen, (80, 160, 255), basket)
pygame.draw.rect(screen, (235, 80, 80), block)
score_text = font.render(f"Score: {score}", True, (240, 240, 240))
screen.blit(score_text, (10, 10))
lives_text = font.render(f"Lives: {lives}", True, (240, 240, 240))
screen.blit(lives_text, (370, 10))
if lives == 0:
message = font.render("Game over", True, (240, 240, 240))
screen.blit(message, message.get_rect(center=(240, 160)))
elif score == 10:
message = font.render("You win!", True, (240, 240, 240))
screen.blit(message, message.get_rect(center=(240, 160)))
if lives == 0 or score == 10:
again = font.render("Press R to play again", True, (240, 240, 240))
screen.blit(again, again.get_rect(center=(240, 200)))
if paused:
message = font.render("Paused", True, (240, 240, 240))
screen.blit(message, message.get_rect(center=(240, 160)))
pygame.display.flip()
clock.tick(60)
pygame.quit()
השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.
כל השיעורים ביחידה צרו משחקים עם Pygame
2אירועים והתנגשויות
הסלבלוק נופלתרגלו בעצמכם: Pygame אונליין