用户:探梦查看:0 回复:0 评论:0 创建时间:2022-11-11T20:19:56
【作品展示】

【作品介绍】
精灵冲突
【作品源代码】
import sys
import time
import random
import math
import pygame
from pygame. local import
insert MySprite class definition here
def print text(font, x, y, text, color -= (255, 255, 255)):
imgText font. render(text, True, color)
screen, blit(imgText, (x, y))
def reset arrow():
y = random. randint(250, 350)
arrow.position-800y
imain program begins
pygame.init()
screen pygame. display. set mode((800, 600) >
pygame. display. set caption "Escape The Dragon Game
font pygame. font. Font(Nore, 18)
framerate pygame. time. Clock()
#load bitmaps
bg pygame.image. load("background. png") convert_alpha()
create sprite group
group pygame. sprite. Group()
icreate the dragon sprite
dragon - MySprite(screen)
dragon. load("dragon, png", 260, 150, 3)
dragon position=100, 230
group. add(dragon)
#create the player sprite
player MySprite(screen)
player.load("caveman. png", 50, 喵, 8)
player. first frame 1
player. last frame=7
player. position=400, 303
group. add(player)
#create the arrow sprite
arrow MySprite(screen)
arrow. load("flame. png", 40, 16, 1)
arrow.position=800, 320
group. add(arrow)
arrow vel=8.0
game over=False
you win False
player jumping False
jump vel=0.0
player starty=player, Y
#repeating loop
while True:
framerate. tick(30)
ticks= pygame, time get ticks()
for event in pygame, eventget():
if event.type=pygame.ousys.exit()
keys pygame. key. get _pressed(
if keys[pygame. K_ESCAPE] sys.exit(
elif keys[pygame. K SPACE]
if not player jumping:
player jumping True
jump vel=-8.0
fupdate the arrow
if not game over:
arrow.x -= arrow vel
if arrow.x < -40: reset arrow)
fdid arrow hit player?
if pygame. sprite. collide_rect(arrow, player):
reset arrow()
player.X - 10
#did arrow hit dragon?
if pygame. sprite. collide rect(arrow, dragon):
reset arrow()
dragon.x -= 10
#did dragon eat the player?
if pygame. sprite. collide_rect(player, dragon):
game over True
#did the dragon get defeated?
if dragon.X < -100:
you win=True
game over True
#is the player jumping?
if player jumping:
player, y += jump_vel
jump vel += 0.5
if player.Y player_ start_y:
player jumping False
player.Y player_start_y
jump_vel=0.0
edraw the background
screen.blit(bg, (0, 0))
#update sprites
if not game over:
group. update(ticks, 50)
adraw sprites
group. draw(screen)
print text(font, 350, 560, "Press SPACE to jumpl")
if game over:
print text(font, 360, 100 "GAME OV E R")
if youwin:
print text(font, 330, 130, YOU BEAT THE DRAGON!* )
else:
print text(font, 330, 130, "THE DRAGON GOT YOU!")
pygame. display. update()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn