我制作了一个简单的平台游戏,我想学习如何制作基本的收集硬币系统在平台上产生硬币的地方,当我消费它时,我会通过一个点系统获得点数这是我的比赛票价:Game我已经在youtube上查看了如何执行此操作,但没有涵盖它的任何教程,但我发现了1,但他们并没有解释我如何执行此操作和我的脚本:
import pygame
import random
import time
pygame.init()
# screen
window = pygame.display.set_mode((500,500))
pygame.display.set_caption("hyeo")
playerx = 350
playery = 250
# player
class player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.isJump = False
self.JumpCount = 10
self.fall = 0
self.speed = 5
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window, self.color, self.rect)
# enemy class
class enemys:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window, self.color, self.rect)
# FPS
FPS = 60
clock = pygame.time.Clock()
# Colors
NiceBlue = (46, 196, 187)
NiceGreen = (48, 196, 46)
# define players by name
playerman = player(40,390,30,30, NiceBlue)
enemy1 = enemys(150,390,150,10, NiceGreen)
enemy2 = enemys(350,300,150,10, NiceGreen)
enemy3 = enemys(70,250,150,10, NiceGreen)
enemy4 = enemys(-1000,480,1900,60, NiceGreen)
# put them in a list so we can call all of them at the same time
enemies = [enemy1,enemy2,enemy3,enemy4]
# Coins my g
# main Loop
runninggame = True
while runninggame:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
runninggame = False
keys = pygame.key.get_pressed()
# Right and Left
if playerman.y < 250:
playerman.y += 1
for enemy in enemies:
enemy.y += playerman.speed
# FOR DOWN AND UP
if playerman.y > 450: #somewhere close to the bottom of the screen
playerman.y -= playerman.fall #reverse direction
for enemy in enemies:
enemy.y -= playerman.fall
if keys[pygame.K_LEFT]:
playerman.x -= playerman.speed
if playerman.x < 100:
playerman.x += playerman.speed
for enemy in enemies:
enemy.x += playerman.speed
if keys[pygame.K_RIGHT]:
playerman.x += playerman.speed
if playerman.x > 400:
playerman.x -= playerman.speed
for enemy in enemies:
enemy.x -= playerman.speed
if not playerman.isJump:
playerman.y += playerman.fall
playerman.fall += 1
collide = False
for enemy in enemies:
if playerman.rect.colliderect(enemy.rect):
collide = True
playerman.y = enemy.rect.top - playerman.height +1
if playerman.rect.right > enemy.rect.left and playerman.rect.left < enemy.rect.left - playerman.width:
playerman.x = enemy.rect.left - player.width
if playerman.rect.left < enemy.rect.right and playerman.rect.right > enemy.rect.right + playerman.width:
playerman.x = enemy.rect.right
break
if playerman.rect.bottom >= 500:
collide = True
playerman.isJump = False
playerman.JumpCount = 10
playerman.y = 500 - playerman.height
if collide:
if keys[pygame.K_SPACE]:
playerman.isJump = True
playerman.fall = 0
else:
if playerman.JumpCount > 0:
playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.4
playerman.JumpCount -= 1
else:
playerman.isJump = False
playerman.JumpCount = 10
window.fill((0,0,0))
playerman.draw()
enemy1.draw()
enemy2.draw()
enemy3.draw()
enemy4.draw()
pygame.display.update()
pygame.quit()
if playerman.rect.colliderect(coin.rect):
,如果它碰撞了,将1加到分数并将其从列表中删除,您可以使用del Coin_list[coin_index]
删除硬币(硬币索引在列表中的位置)。 先尝试一下,向我们展示您的尝试,我们将为您提供帮助
1)您需要使用滚动条移动硬币,这很容易,与敌人相同,但是带有硬币,请让您这样做
2)对于碰撞,您所做的与敌人相同,如果发生碰撞,请在顶部移动,这不是您想要的,您希望它消失并通过它
对于碰撞,您必须使用一种高级技术,即向后循环硬币
for i in range(len(Coins_list)-1,-1,-1):
if playerman.rect.colliderect(coin.rect):
del Coins_list[i]
score += 1
这是因为当您从列表中删除某些内容时,所有内容都会向下移动以填充它,例如如果删除第3个元素,则第4个元素将变为第3个元素。因此,如果您遍历所有这些元素,并且删除了第三个,则当您移至第4个时,实际上是第5个,因为第4个移至第3个。因此您最终跳过了一个,并且由于尝试获取最后一个减少了一个的元素而收到索引错误。因此,向后表示如果删除一个,则已经检查的对象将被移动,而hvanet的对象将保持不变。对于文本,使用变量
score
(如上),并将其设置为0。文本被硬编码为0,因此您希望它是得分,因此它被更改了
score = 0 text = font.render('Score = ' + str(score), True, NiceOlive) textRect = text.get_rect() textRect.center = (100, 40)
我也将它添加到与score+= 1
下面的硬币的碰撞中>现在您可能会注意到硬币不会消失,这是因为您单独绘制它们,只希望在列表中绘制它们
window.fill((0,0,0)) window.blit(text,textRect) for coin in Coins_list: coin.draw() playerman.draw() for enemey in enemies: enemy.draw()
这里是完整代码
import pygame import random import time pygame.init() # ------------------------------------------------------------------------------------------VV window screen size window = pygame.display.set_mode((500,500)) pygame.display.set_caption("DUDE RUFAIDA UGLY ASS HELL") # ------------------------------------------------------------------------------------------ # --------------------------------------------VV coins class class coins: def __init__(self,x,y,height,width,color): self.x = x self.y = y self.height = height self.width = width self.color = color self.rect = pygame.Rect(x,y,height,width) def draw(self): self.rect.topleft = (self.x,self.y) pygame.draw.rect(window, NiceOlive, self.rect) # ------------------------------------------------------------------------------------------ # --------------------------------------VVV player class # draw the player class player: def __init__(self,x,y,height,width,color): self.x = x self.y = y self.width = width self.height = height self.isJump = False self.JumpCount = 10 self.speed = 5 self.fall = 0 self.color = color self.rect = pygame.Rect(x,y,height,width) def draw(self): self.rect.topleft = (self.x,self.y) pygame.draw.rect(window, self.color, self.rect) # ------------------------------------------------------------------------------------------ # ------------------------------------------VV enemy class class enemys: def __init__(self,x,y,height,width,color): self.x = x self.y = y self.height = height self.width = width self.color = color self.rect = pygame.Rect(x,y,height,width) def draw(self): self.rect.topleft = (self.x,self.y) pygame.draw.rect(window, self.color, self.rect) # ------------------------------------------------------------------------------------------ # --------------------------VV frames per sec # FPS FPS = 60 clock = pygame.time.Clock() # ------------------------------------------------------------------------------------------ # ---------VV colors # COLORS NiceYellow = (255,255,0) NiceOlive = (0, 255, 0) # ------------------------------------------------------------------------------------------ # ----------------------------VV define enemy and players xx,y,height and colors # define enemy and player class playerman = player(40,390,30,30, NiceOlive) enemy1 = enemys(150,390,100,10, NiceYellow) enemy2 = enemys(300,300,100,10, NiceYellow) enemy3 = enemys(80,250,100,10, NiceYellow) enemy4 = enemys(-5000,490,100000,100, NiceYellow) enemies = [enemy1,enemy2,enemy3,enemy4] # ------------------------------------------------------------------------------------------ # --------------------------define coins colors and width,heights anD coins LIST coin1 = coins(250,250,20,20,NiceOlive) coin2 = coins(350,350,20,20,NiceOlive) coin3 = coins(300,300,20,20,NiceOlive) coin4 = coins(150,150,20,20,NiceOlive) coin5 = coins(50,390,20,20,NiceOlive) Coins_list = [coin1,coin2,coin3,coin4,coin5] # ------------------------------------------------------------------------------------------ # -----------VV scoring # display scoring font = pygame.font.Font('freesansbold.ttf', 32) score = 0 text = font.render('Score = ' + str(score), True, NiceOlive) textRect = text.get_rect() textRect.center = (100, 40) # ------------------------------------------------------------------------------------------ # main loop runninggame = True while runninggame: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: runninggame = False # -----------------------Draw the players and coins and enemys window.fill((0,0,0)) window.blit(text,textRect) for coin in Coins_list: coin.draw() playerman.draw() for enemy in enemies: enemy.draw() # ------------------------------------------------------------------------------------------ # --------------------------# VV screen movements if playerman.y < 250: playerman.y += 1 for enemy in enemies: enemy.y += playerman.speed for coin in Coins_list: coin.y += playerman.speed if playerman.y > 450: playerman.y -= playerman.fall for enemy in enemies: enemy.y -= playerman.fall for coin in Coins_list: coin.y -= playerman.fall # ------------------------------------------------------------------------------------------ # ----------------------------VV player keys and screen movements keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: playerman.x -= playerman.speed if playerman.x < 100: playerman.x += playerman.speed for enemy in enemies: enemy.x += playerman.speed for coin in Coins_list: coin.x += playerman.speed if keys[pygame.K_RIGHT]: playerman.x += playerman.speed if playerman.x > 400: playerman.x -= playerman.speed for enemy in enemies: enemy.x -= playerman.speed for coin in Coins_list: coin.x -= playerman.speed # ------------------------------------------------------------------------------------------ # ---------------------------collisions with player and enemy if not playerman.isJump: playerman.y += playerman.fall playerman.fall += 1 collide = False for enemy in enemies: if playerman.rect.colliderect(enemy.rect): collide = True playerman.y = enemy.rect.top - playerman.height + 1 if playerman.rect.right > enemy.rect.left and playerman.rect.left < enemy.rect.left - playerman.width: playerman.x = enemy.rect.left - playerman.width if playerman.rect.left < enemy.rect.right and playerman.rect.right > enemy.rect.right + playerman.width: playerman.x = enemy.rect.right # ------------------------------------------------------------------------------------------ # ---------------------------------collision with coins and player for i in range(len(Coins_list)-1,-1,-1): if playerman.rect.colliderect(Coins_list[i].rect): del Coins_list[i] score += 1 text = font.render('Score = ' + str(score), True, NiceOlive) textRect = text.get_rect() textRect.center = (100, 40) # ------------------------------- here is the problem I said if playerman.rect.colliderect coin1 it should then collide and delete the coin1 from coin list and then it should add it in to the score 1 point if playerman.rect.bottom >= 500: collide = True playerman.isJump = False playerman.JumpCount = 10 playerman.y = 500 - playerman.height if collide: if keys[pygame.K_SPACE]: playerman.isJump = True playerman.fall = 0 else: if playerman.JumpCount > 0: playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3 playerman.JumpCount -= 1 else: playerman.isJump = False playerman.JumpCount = 10 pygame.display.update() pygame.quit()