これが私のコードです:
import pygame, sys
from pygame.locals import *
try:
import Android
except ImportError:
Android = None
try:
import pygame.mixer as mixer
except ImportError:
import android.mixer as mixer
pygame.init()
#Android to cross platform
if Android:
android.init()
android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE)
#Variables
score = 0
health = 100
#Colours
RED = (255 ,0 ,0)
BLACK = (0, 0, 0)
# Left Edge of rectangle, top, width, height
player_rect = pygame.Rect(540, 0, 32, 32)
enemy = pygame.image.load("monster.png")
size = width, height = 1276,650
screen = pygame.display.set_mode(size)
direction ="down"
# This is the score text
gamefont = pygame.font.Font(None, 30)
scoretext = gamefont.render('Player Score: '+ str(score), 2, [255,0,0])
boxsize=scoretext.get_rect()
scoreXpos=(width-boxsize[2])/2
scoreYpos=(height-boxsize[2])/2
screen.blit(scoretext, [scoreXpos,20])
# This is the health
gamefonts = pygame.font.Font(None, 30)
scoretexts = gamefonts.render('Player Health: '+ str(health), 2, [255,0,0])
boxsizes=scoretexts.get_rect()
scoreXposs=(width-boxsizes[2])/2
scoreYposs=(height-boxsizes[2])/2
screen.blit(scoretexts, [scoreXposs,20])
PAUSED = False
font = pygame.font.SysFont("serif", 72)
text_paused = font.render("PAUSED", True, (255, 0, 0))
sound = mixer.Sound('maintheme.ogg')
sound.play()
r = 0
bif = pygame.image.load("map5.png")
pygame.display.set_caption("Pygame 2D RPG !")
x,y=0,0
movex, movey=0,0
character="boy.png"
player=pygame.image.load(character).convert_alpha()
while True:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
elif event.key==K_q:
PAUSED = not PAUSED
if not PAUSED:
if event.type==KEYDOWN:
if event.key==K_a:
movex=-10
elif event.key==K_d:
movex=+10
elif event.key==K_w:
movey=-10
elif event.key==K_s:
movey=+10
if event.type==KEYUP:
if event.key==K_a:
movex=0
elif event.key==K_d:
movex=0
elif event.key==K_w:
movey=0
elif event.key==K_s:
movey=0
# Enemy
if direction == "down":
player_rect.centery += 1
elif direction == "up":
player_rect.centery -= 1
if player_rect.bottom > size[1]:
direction = "up"
if player_rect.top < 0:
direction = "down"
if not PAUSED:
x+=movex
y+=movey
print x,y
if x in range(680,702) and y in range(377,403): # This is the location of the tile.
bif = pygame.image.load("map6.png")
screen.fill((r,0,0))
screen.blit(bif,(0,0))
screen.blit(player,(x,y))
screen.blit(enemy, player_rect)
#pygame.draw.rect(screen, RED, player_rect)
screen.blit(scoretexts, [scoreYposs, 45])
scoretexts = gamefonts.render('Player Health: '+ str(health), 2, [25,25,112])
#health +=10
screen.blit(scoretext, [scoreYpos,70])
scoretext = gamefont.render('Player Score: '+ str(score), 2, [25,25,112])
#score +=10
if PAUSED:
screen.blit(text_paused, (100, 100))
pygame.display.update()
今私は誰かが衝突を行う方法を知っているかどうか疑問に思っていました. 助けてくれてありがとう!