您好,对很多问题感到抱歉,但我再次认识python,但我正在尝试学习pygame。在游戏中,我正在尝试使之进入游戏,因此当块/图像接触到精灵时,它会回到屏幕顶部。我看过许多教程,但似乎无法以一种很好的方式来理解它。有什么建议吗?这是代码,谢谢!!!:
import pygame
import sys
from random import randint
import os
x = 250
y = 30
os.environ["SDL_VIDEO_WINDOW_POS"] = "%d,%d" % (x, y)
width = 1024
height = 768
icon1 = pygame.image.load("Firstpygamegame/santa-claus.png")
pygame.display.set_icon(icon1)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Gift Catcher")
background_image = pygame.image.load("Firstpygamegame/wintervillage.png")
sprite1 = pygame.image.load("Firstpygamegame/santabag2.png")
spriterect = sprite1.get_rect()
speed = 2.5
# 442 or 467
spriterect.x = 442
icon2 = pygame.image.load("Firstpygamegame/present1.png")
icon3 = pygame.image.load("Firstpygamegame/present2.png")
icon4 = pygame.image.load("Firstpygamegame/present3.png")
icon5 = pygame.image.load("Firstpygamegame/present4.png")
cubes = [[
randint(1, 1000), # X coordinate
randint(-1500, -350)] # Y coordinate, -Y is above screen (top of screen is zero)
for x in range(2)] # 20 cubes
cubes1 = [[
randint(1, 1000), # X coordinate
randint(-1500, -150)] # Y coordinate, -Y is above screen (top of screen is zero)
for x in range(2)] # 20 cubes
cubes2 = [[
randint(1, 1000), # X coordinate
randint(-1500, -550)] # Y coordinate, -Y is above screen (top of screen is zero)
for x in range(2)] # 20 cubes
cubes3 = [[
randint(1, 1000), # X coordinate
randint(-1500, -450)] # Y coordinate, -Y is above screen (top of screen is zero)
for x in range(2)] # 20 cubes
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
spriterect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * speed
spriterect.y = 600
screen.blit(background_image, (0, 0))
screen.blit(sprite1, spriterect)
for cb in cubes:
cb[1] += .25 # cube moves down 2 pixels
screen.blit(icon2, cb) # draw cube
if cb[1] > 800: # if cube passed bottom of screen
cb[1] = -100 # move to above screen
cb[0] = randint(1, 1000)
for cb in cubes1:
cb[1] += .25 # cube moves down 2 pixels
screen.blit(icon3, cb) # draw cube
if cb[1] > 800: # if cube passed bottom of screen
cb[1] = -100 # move to above screen
cb[0] = randint(1, 1000) # random X position
for cb in cubes2:
cb[1] += .25 # cube moves down 2 pixels
screen.blit(icon4, cb) # draw cube
if cb[1] > 800: # if cube passed bottom of screen
cb[1] = -100 # move to above screen
cb[0] = randint(1, 1000) # random X position
for cb in cubes3:
cb[1] += .25 # cube moves down 2 pixels
screen.blit(icon5, cb) # draw cube
if cb[1] > 800: # if cube passed bottom of screen
cb[1] = -100 # move to above screen
cb[0] = randint(1, 1000) # random X position
pygame.display.flip()
使用pygame.Rect
对象并colliderect()
查找矩形和对象之间的碰撞。
pygame.Surface.get_rect.get_rect()
返回一个具有Surface对象大小的矩形,该矩形始终以(0,0)开始,因为Surface对象没有位置。矩形的位置可以通过关键字参数指定:
while running:
# [...]
for cb in cubes:
cb[1] += .25 # cube moves down 2 pixels
screen.blit(icon2, cb) # draw cube
icon_rect = icon2.get_rect(topleft = cb)
if cb[1] > 800 or icon_rect.colliderect(spriterect):
cb[1] = -100 # move to above screen
cb[0] = randint(1, 1000)
# [...]
但是,您可以简化代码:
icon_list = [icon2, icon3, icon4, icon5]
cube_lists = [[[
randrange(screen.get_width() - icon.get_width()),
randint(-1500, -350)]
for x in range(2)]
for icon in icon_list]
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
spriterect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * speed
spriterect.y = 600
screen.blit(background_image, (0, 0))
screen.blit(sprite1, spriterect)
for icon, cubes in zip(icon_list, cube_lists):
for cb in cubes:
cb[1] += .25 # cube moves down 2 pixels
screen.blit(icon, cb) # draw cube
icon_rect = icon.get_rect(topleft = cb)
if cb[1] > screen.get_height() or icon_rect.colliderect(spriterect):
cb[:] = randrange(screen.get_width() - icon.get_width()), -800
pygame.display.flip()
pygame.quit()
sys.exit()
请注意,如果running
为,则应用程序循环终止False
。设置running = False
和调用pygame.quit()
以及sys.exit()
事件循环没有意义。让运行到结束和呼叫循环pygame.quit()
和sys.exit()
事件循环后。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句