
Lv.1
原名:编程中手,有事加Q:3524304608。随便吧
签名:做我想做的
在 编程猫丨第一届Python选拔赛(非官方举办) 中回复
import pgzrun
import random
import math
WIDTH = 450
HEIGHT = 570
score = 0
game_state = 'start'
speed = 6
shoot = False
t = 0
attack = False
life = 3
spaceship = Actor('spaceship',(200, 500))
diamond = Actor('diamond', (100, -200))
enermy = Actor('enermy', (300, -200))
spaceship_bullet = Actor('bullet1', (200, -100))
enermy_bullet = Actor('bullet2', (200, -100))
heart = Actor('heart', (300, -5000))
def draw():
global game_state, shoot, score, life
if game_state == 'start':
screen.blit('space', (-20, 0))
screen.draw.text('Spacecraft', (115, 260), fontsize=60)
screen.draw.text('Press SPACE to start!',
(85, 320), fontsize=40)
if keyboard.space:
game_state = 'play'
elif game_state == 'play':
screen.blit('space',(0, 0))
screen.draw.text('score:' + str(score),
(10, 10), fontsize=30)
screen.draw.text('life:' + str(life),
(370, 10), fontsize=30)
spaceship.draw()
diamond.draw()
enermy.draw()
spaceship_bullet.draw()
enermy_bullet.draw()
heart.draw()
else:
screen.blit('space', (0, 0))
spaceship.draw()
screen.draw.text('Game Over!', (90, 230),
color='red', fontsize=60)
screen.draw.text('Your score:' + str(score),
(125, 280), fontsize=40)
screen.draw.text('Press SPACE to restart!',
(70, 320), fontsize=40)
if keyboard.space:
game_state = 'play'
place_object(enermy)
place_object(diamond)
score = 0
Health = 0
life = 3
def place_object(obj):
obj.x = random.randint(20, WIDTH - 20)
obj.y = random.randint(-200, -40)
def update():
global score, game_state, shoot, t, attack, life
if game_state == 'play':
enermy.y += speed - 2
enermy.x = WIDTH / 2 * (1 + math.sin(1.2 * t))
t += 0.02
diamond.y += speed
heart.y += speed
if enermy.y > HEIGHT + 100:
place_object(enermy)
if diamond.y > HEIGHT + 100:
place_object(diamond)
if heart.y > HEIGHT + 100:
place_object(heart)
if keyboard.left:
spaceship.x -= 5
elif keyboard.right:
spaceship.x +=5
if spaceship.x < 0:
spaceship.x = 0
elif spaceship.x > WIDTH:
spaceship.x = WIDTH
if keyboard.a:
shoot = True
init_pos = spaceship.x, spaceship.y - 30
spaceship_bullet.pos = init_pos[0], init_pos[1]
if shoot:
if spaceship_bullet.y > -20:
spaceship_bullet.y -= 10
else:
shoot = False
spaceship_bullet.x = -160
if enermy.y > 0 and attack == False:
if enermy.x - spaceship.x < 20 and enermy.x - spaceship.x > -20:
enermy_bullet.pos = enermy.x, enermy.y + 20
attack = True
if attack:
if enermy_bullet.y < HEIGHT + 500:
enermy_bullet.y += speed + 2
else:
attack = False
if enermy.collidepoint(spaceship_bullet.pos):
place_object(enermy)
score += 10
if spaceship.collidepoint(diamond.pos):
place_object(diamond)
score += 10
spaceship_collide(enermy)
spaceship_collide(enermy_bullet)
if heart.y > HEIGHT + 100:
heart.x = random.randint(50, WIDTH - 50)
heart.y = random.randint(-10000, -5000)
if spaceship.collidepoint(heart.pos):
life += 1
heart.x = random.randint(50, WIDTH - 50)
heart.y = random.randint(-10000, -5000)
def spaceship_collide(obj):
global life, game_state
if spaceship.collidepoint(obj.pos):
place_object(obj)
life -= 1
if life == 0:
game_state = 'game over'
pgzrun.go()2020-07-02T20:50:07 点赞:0
在 shyfcka,尹子,本尊,墨水,还有更多的代码大佬,我要挑战你们的权威! 中回复
第一个我这个萌新都会:
if (name = '刺客'){
entity.player.a = 50
entity.player.b = 10
entity.player.c = 30
}
else if (name = '战士'){
entity.player.a = 30
entity.player.b = 20
entity.player.c = 50
}
else{
entity.player.a = 40
entity.player.b = 15
entity.player.c = 40
}
(感觉好多错的呀【汗】)
2020-07-06T21:07:42 点赞:1
在 I only speak English! ! !(我只说英文!!!) 中回复
OK.But……You speak Chinese next time!?
2020-07-07T19:16:02 点赞:0