- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我最近开始使用 pygame,但遇到了麻烦。我的生命计数器在我的程序中不起作用。
import pygame, time, random
pygame.init() # Initializes Pygame
display_width = 1280
display_height = 720
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Cube Game')
black = (0,0,0)
white = (255,255,255)
clock = pygame.time.Clock()
def life(lives):
font = pygame.font.SysFont(None, 25);
text = font.render("Lives: " + str(lives), True, white)
gameDisplay.blit(text,(1200,5))
def score(count):
font = pygame.font.SysFont(None, 25);
text = font.render("Score: " + str(count), True, white)
gameDisplay.blit(text,(5,5))
def enemyBall(ballx, bally, ballr, color):
pygame.draw.circle(gameDisplay, color, [ballx, bally], ballr)
# Creates Location for the Cube
def cube(x,y,side):
pygame.draw.rect(gameDisplay, white, [x, y, side, side])
def text_objects(text, font):
textSurface = font.render(text, True, white)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf', 50)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
__init__()
def dead():
gameDisplay.fill(black)
message_display('You have fallen out of the castle and died!')
# Game Loop
def __init__():
x = ((display_width/2) - 30)
y = (display_height - 70)
side = 60
x_change = 0
ballx_change = 25
bally_change = 25
ball_radius = 50
ball_startx = random.randrange(26, (display_width-ball_radius-1))
ball_starty = random.randrange((ball_radius+1), 100)
death = False
goodbye = False
myLives = 3
myScore = 0
# If Player has not Died
while not death:
for event in pygame.event.get():
if event.type == pygame.QUIT:
death = True
pygame.display.quit()
pygame.quit()
quit()
# Key is still Pressed
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -10
if event.key == pygame.K_RIGHT:
x_change = 10
# Key is no longer Pressed
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(black)
# Defines the enemy ball
enemyBall(ball_startx, ball_starty, ball_radius, white)
ball_startx += ballx_change
ball_starty += bally_change
# Wall Collision
if ball_startx - ball_radius <= 0 or ball_startx + ball_radius >= display_width:
ballx_change = -ballx_change
myScore += 1
if ball_starty - ball_radius <= 0 or ball_starty + ball_radius >= display_height:
bally_change = -bally_change
myScore += 1
# Determines Player Position
cube(x,y,side)
# Score and Life Counter
score(myScore)
life(myLives)
# Death by Abyss
if x > display_width or x < -50:
myLives -= 1
dead()
# Death by Ball
if ball_startx + ball_radius >= x and ball_startx - ball_radius <= x + side:
if ball_starty + ball_radius >= y and ball_starty - ball_radius <= y + side:
myLives -= 1
dead()
pygame.display.update()
clock.tick(60)
time.sleep(2)
__init__()
pygame.display.quit()
pygame.quit()
quit()
我有一个声明,当玩家死亡时,生命计数器会减少一。然而,生命计数器似乎重置为 3(原始数量)。我需要改变什么来修复生命计数器?谢谢!
最佳答案
dead() 调用 message_display(),它再次调用您的 init()。在每次调用 init() 时,您都会再次设置 myLives = 3。
关于python - 生命计数器不断重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59494003/
我在leetcode上看到这段代码,是一道求众数的题,下面是题目描述: 给定一个大小为 n 的数组,找到多数元素。众数元素是出现次数超过 ⌊ n/2 ⌋ 次的元素。 你可以假设数组是非空的并且多数元素
每次在 JavaScript 中执行特定操作时,例如: $(function() { $('#typing').keyup(function () { switch($(this)
我一直在为网页设计一个计数器,但我一直被这个我无法解决的功能所困扰。 我有一个 4 个 div 的计数器,因为其中两个是小数字,另外两个是大数字,所以第一个运行得很快,我看不到它们的功能。 有人知道如
我已经在文档中进行了一些搜索,并在网上花了一段时间,但找不到解决方案!我希望警报告诉我单击 .thumb 时它处于each() 的哪一次迭代。 EG:有六个.thumb,我点击数字3,浏览器弹出3!
在 Handlebars 中,假设我有 names 的集合.我能怎么做 {{#each names}} {{position}} {{name}} {{/each}} 在哪里 {{position}}
这个问题在这里已经有了答案: Numbering rows within groups in a data frame (9 个回答) 4年前关闭。 我们如何在数据帧的每组中生成唯一的 ID 号?以下
我正在努力解决以下问题。我希望为给定的“一”序列创建一个计数器。例如,我有以下内容: 1 1 1 1 0 0 1 1 1 0 0 1 1 1 1 鉴于该序列,我希望为 1 的每个序列设置一个计数器直到
我正在努力解决以下问题。我希望为给定的“一”序列创建一个计数器。例如,我有以下内容: 1 1 1 1 0 0 1 1 1 0 0 1 1 1 1 鉴于该序列,我希望为 1 的每个序列设置一个计数器直到
我有一个jsfiddle here 这是一个简单的 JavaScript 函数,可以计算出设定的数字。 是否可以进行这种计数,但也保留一位小数 所以它算 1.1、1.2、1.3 等。 func
我正在构建一个计数器,当我按下鼠标时,它应该增加到 maxValue 并且减少不超过 0。我还可以选择将计数器重置为其初始值:0。另外,如果 maxValue 是偶数,它应该计数到该数字。但是,如果
所以我成功地为字母和单词构建了其他计数器,但现在我只能用这个来计算句子。我的代码如下,当我运行它时,它会返回很多错误消息: #include #include #include int main
Closed. This question is off-topic。它当前不接受答案。
我需要一个计数器,它会随着某些任务的完成而递增。我们只需要最后一小时的值,即窗口将移动而不是静态时间。 解决此问题的最佳方法是什么?我能想到的一种方法是拥有一个大小为 60 的数组,每分钟一个,并更新
我希望使用计数器来为我提供独特的引用系统。我想单击一个按钮,然后检查一个字段/文件中的最后一个数字,然后简单地向其添加 1,然后将其插入到屏幕上的字段中? 不确定执行此操作的最佳方法或具体如何执行此操
我有一个用 php 制作的表格,在该表格内我显示了数据库中的一些内容。我在每个 td 中创建了一个简单的按钮(类似于 Like),我希望每次点击它都会增加 1。这是带有按钮的行: echo "
如何将数据库中的值转换为可用于 if else 函数的 int 值? 例如:在我的数据库“armnumber = 3”中,如何在 if else 函数中使用它? 代码 string myConnect
我需要生成唯一的“ids”,问题是,它只能在 1 - 99999 之间。 “好”的是,它仅在与另一列组合时必须是唯一的。 我们有组,每个组都有自己的“group_id”,每个组都需要类似 unique
有这个简单的代码: UPDATE counter SET c= c +1 where id = 1; 并且它在开头的 c 字段中为 null 的情况下不起作用。它只有在已经输入了一些数字时才有效,也就
我正在尝试在 python 中构建一个具有闭包属性的计数器。以下工作中的代码: def generate_counter(): CNT = [0] def add_one():
我使用 CSS 来计算 HTML 文档中的部分: body {counter-reset: sect;} section:before { counter-increment: sect;
我是一名优秀的程序员,十分优秀!