我正在使用 Pygame 和 Python 3.2 制作游戏。我必须弄清楚如何使用碰撞检测,以便玩家 ( ballpic
) 可以捡起物品。
这是我的代码:
from pygame import * #import pygame
bg = image.load('BG.png')
ballpic = image.load('PlayerForward1.png') #Set the variable "ballpic" to ball.png
seed = image.load('Sunflowerseed.png')
ballpic.set_colorkey((0,0,0))
from random import randint
numballs = 10
delay = 5
red = ( 0, 0, 255)
done=False
ballx = 0 #Ball Coordinates
bally=0
ballxmove = 0
ballymove = 0 #Set the direction of the ball
init() #Start Pygame
key.set_repeat(10,10)
screen = display.set_mode((640, 480)) #Set the size of the window
display.set_caption("RPG game") #Set the window name
event.set_grab(1)
if ballx > 600:
ballx =ballx-4
if ballx < 0:
ballx =ballx+4
if bally > 440:
bally=bally-4
if bally < 0:
bally=bally+4
mixer.music.load('bgm.flac')
#mixer.music.play(-1, 0.0)
while done == False:
screen.fill(0) #Fill the screen with black
screen.blit(bg, (ballx,bally))
screen.blit(ballpic, (320,240))#Draw ball
screen.blit(seed, (ballx,240))
display.update() #Update the screen
time.delay(9) #Slow it down
ballx=ballx + ballxmove #Update ball position
bally=bally + ballymove
for e in event.get(): #Check for ESC pressed
if e.type == KEYDOWN:
if e.key ==K_LEFT:
ballpic=image.load('PlayerReversed.png')
print(ballx,bally)
ballx= (ballx+4)
if e.key ==K_RIGHT:
ballpic=image.load('PlayerForward1.png')
print(ballx,bally)
ballx=ballx-4
if e.key == K_ESCAPE:
quit()
我的背景(BG)在ballx
变量上运行,因此屏幕似乎滚动。我需要检测是否ballpic
发生seed
碰撞。