Python Pygame得分/吃苹果得分的问题
所以基本上我是在尝试制作一个基本的计分系统和苹果,我可以通过吃苹果来获得分数,但这种情况一直在发生——它不允许我吃苹果,它一直在我的屏幕上弹来弹去。请帮助我标记计分的位置,以及代码上的所有内容Python Pygame得分/吃苹果得分的问题,python,pygame,new-operator,Python,Pygame,New Operator,所以基本上我是在尝试制作一个基本的计分系统和苹果,我可以通过吃苹果来获得分数,但这种情况一直在发生——它不允许我吃苹果,它一直在我的屏幕上弹来弹去。请帮助我标记计分的位置,以及代码上的所有内容 import pygame import time import random pygame.init() window = pygame.display.set_mode((500,500)) pygame.display.set_caption("I am a hacker") # playe
import pygame
import time
import random
pygame.init()
window = pygame.display.set_mode((500,500))
pygame.display.set_caption("I am a hacker")
# player class
class players(object):
def __init__(self,x,y,height,width):
self.x = x
self.y = y
self.height = height
self.width = width
self.isJump = False
self.JumpCount = 10
self.fall = 0
self.speed = 5
# enemy class
class enemys(object):
def __init__(self,cordx,cordy,heights,widths):
self.cordx = cordx
self.cordy = cordy
self.heights = heights
self.widths = widths
# color blue for player
blue = (32,207,173)
red = (255,0,0)
orange = (207,135,32)
# FPS
FPS = 60
clock = pygame.time.Clock()
display_width = 50
display_height = 50
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
# -----------------------------------------------------
# scoring and apple varabiles etc
snake_block = 10
snake_speed = 15
def Your_score(score):
value = score_font.render("Your Score: " + str(score), True, red)
window.blit(value, [0, 0])
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(window, red, [x[0], x[1], snake_block, snake_block])
def message(msg, color):
mesg = font_style.render(msg, True, color)
window.blit(mesg, [500 / 6, 500 / 3])
game_over = False
game_close = False
x1 = 500 / 2
y1 = 500 / 2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
foodx = round(random.randrange(0, 500 - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, 500 - snake_block) / 10.0) * 10.0
# ------------------------------------------------------------------------------------------
# Main Loop
playerman = players(50,390,50,50)
enemyman = enemys(190,390,150,10)
runninggame = True
while runninggame:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
runninggame = False
# ------------------------------------------------------------------------
# Scoring and Apple System
if x1 >= 500 or x1 < 0 or y1 >= 500 or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
window.fill(red)
pygame.draw.rect(window, red, [foodx, foody, snake_block, snake_block])
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List) > Length_of_snake:
del snake_List[0]
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
our_snake(snake_block, snake_List)
Your_score(Length_of_snake - 1)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, 500 - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, 500 - snake_block) / 10.0) * 10.0
Length_of_snake += 1
# ------------------------------------------------------------------------------
window.fill((0,0,0))
player = pygame.draw.rect(window,(blue),(playerman.x,playerman.y,playerman.height,playerman.width))
enemy = pygame.draw.rect(window,(orange),(enemyman.cordx,enemyman.cordy,enemyman.heights,enemyman.widths))
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and playerman.x > playerman.speed:
playerman.x -= playerman.speed
if keys[pygame.K_RIGHT] and playerman.x < 500 - playerman.width - playerman.speed:
playerman.x += playerman.speed
if not playerman.isJump:
playerman.y += playerman.fall
playerman.fall += 1
# ----------------------------------------------------- # enem1 collisio
# both of my 2 enemy squares collisions push me back when ever I Jump on the top of them on there sides but when I jump on the middle of of both of them it seems to work if I just want it so when I jump on both of my squares I just don't get pushed back
player.topleft = (playerman.x, playerman.y)
collide = False
playerman.isJump = False
if player.colliderect(enemy):
collide = True
playerman.isJump = False
playerman.y = enemy.top - player.height
if player.right > enemy.left and player.left < enemy.left - player.width:
playerman.x = enemy.left - player.width
if player.left < enemy.right and player.right > enemy.right + player.width:
playerman.x = enemy.right
if player.bottom >= 500:
collide = True
playerman.isJump = False
playerman.JumpCount = 10
playerman.y = 500 - player.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.5
playerman.JumpCount -= 1
else:
playerman.JumpCount = 10
playerman.isJump = False
pygame.display.update()
pygame.quit()
``
你的节目有点乱。你有一条蛇和食物,但是蛇不移动来获取食物,这是第一个问题。你也说苹果,但是只有一个苹果,另一个是你的蛇。但是你也有一个玩家和敌人,而且玩家不是蛇,所以不确定这里的意图是什么,所以不确定如何在没有更多信息的情况下提供帮助 但是,为了修复闪烁,您需要pygame.display.update两次,并且有一个窗口。在它们之间填充0,0,0,因此所发生的是您使用苹果更新屏幕,然后清除屏幕,然后再次更新 要修复: 在中删除pygame.display.update 并移动窗口。将0,0,0填充到循环中的第一行
runninggame = True
while runninggame:
window.fill((0,0,0))
...
您可能会考虑构建一个MRE来更好地解决这两个问题。尽量简化你的代码,同时仍然复制任何一个错误。对不起,伙计,我知道这很混乱,但我是一个初学者程序员,我正在尝试用一个简单的对象制作一个平板游戏,我将吃掉它,并在我的屏幕上获得分数sorry@HabibIsmail别难过,我们都得从某个地方开始,当我试图用pygame制作一个文本编辑器时,你应该看到我的代码是多么糟糕。但是你的程序中基本上有蛇游戏。你是否复制并粘贴了蛇游戏作为食物而不想要蛇游戏?您检查蛇的食物是否发生碰撞,但蛇仅在x1_变化始终为0的情况下移动。你需要把它换成食物,而不是蛇
runninggame = True
while runninggame:
window.fill((0,0,0))
...