用户:
evolto_E总查看:24 回复:8 评论:24 创建时间:2023-08-01T22:07:19
如何在Python里搞舞台区
import pgzrun
bg = Actor("青青平原.gif")#要加上后缀名
codemao = Actor("马里喵1.gif",[114,514])#[]内为角色初始坐标
def draw():
bg.draw()
codemao.draw()
def move_x(num):
codemao.x += num
move_x(114)
pgzrun.go()
点赞0
评论
importpgzrun,time,random#导入库 wt=time.time() die=0 score=0 WIDTH=800 HEIGHT=800 #一系列变量 bg=Actor("bg.gif") holes=[] foriinrange(6): hole=Actor("hole.png",[120+i%3*250,300+i//3*250+i%3*40]) hole.wait=random.randint(2,10) holes.append(hole) hammer=Actor("hammer1.png") lost=Actor("失败.png",[-114514,400]) #角色制作 defdraw(): globalscore bg.draw() forholeinholes: hole.draw() hammer.draw() lost.draw() screen.draw.text('分数:'+str(score),[0,0],fontsize=50,color="red") #角色显示 defupdate():#重复执行 globalwt,die,score#全局变量 ifdie==1:#侦测喵 lost.x=400 music.play_once("溜走.mp3") die=2#确保只执行一次 iftime.time()-wt>=1anddie==0:#没有死且每次间隔1秒 forholeinholes:#遍历 ifhole.wait==0:#到达交换时间 ifhole.image!="hole.png"orhole.image[4:11]=="touched":#侦测是否不为空 hole.image="hole.png" hole.y+=65#角色的《特性》 hole.wait=random.randint(2,10)#刷新时间 else: hole.image=random.choice(["飞电鼠","编程猫"])+"-happy.png" hole.y-=65 hole.wait=3 music.play_once("跳.mp3") else: hole.wait-=1 wt=time.time() hammer.image="hammer1.png"#确保点击后锤子恢复 defon_mouse_down(pos):#当鼠标点击 globaldie,score#全局 ifdie==0:#还没喵 hammer.image="hammer2.png" forholeinholes: ifhole.image!="hole.png"andhole.image[4:11]!="touched": music.play_once("咚.mp3") ifhole.collidepoint(pos): ifhole.image=="编程猫-happy.png":#侦测是否打了编程猫 die=1 else: score+=1 hole.image=hole.image[:3]+"-touched.png" hole.wait=1 defon_mouse_move(pos):#锤子跟随 globaldie ifdie==0: hammer.x=pos[0] hammer.y=pos[1] pgzrun.go()
点赞0
评论
# 导入必要的库
import pygame
import random
# 初始化pygame
pygame.init()
# 设置颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# 设置屏幕大小
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# 设置地鼠的大小
MOLE_WIDTH = 50
MOLE_HEIGHT = 50
# 创建屏幕对象
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("打地鼠游戏")
# 定义地鼠类
class Mole(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([MOLE_WIDTH, MOLE_HEIGHT])
self.image.fill(RED)
self.rect = self.image.get_rect()
self.update_position()
def update_position(self):
"""更新地鼠的位置"""
self.rect.x = random.randint(0, SCREEN_WIDTH - MOLE_WIDTH)
self.rect.y = random.randint(0, SCREEN_HEIGHT - MOLE_HEIGHT)
# 创建地鼠精灵组
mole_group = pygame.sprite.Group()
# 添加多个地鼠到精灵组
for _ in range(5): # 创建5个地鼠
mole = Mole()
mole_group.add(mole)
# 记录上次更新地鼠位置的时间
last_update_time = pygame.time.get_ticks()
# 游戏主循环
running = True
while running:
current_time = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
# 检查是否点击到地鼠
clicked_moles = [
m for m in mole_group if m.rect.collidepoint(event.pos)]
for mole in clicked_moles:
mole.update_position()
# 每隔三秒更新地鼠位置
if current_time - last_update_time > 3000: # 3000毫秒等于3秒
for mole in mole_group:
mole.update_position()
last_update_time = current_time
# 清屏
screen.fill(WHITE)
# 绘制地鼠
mole_group.draw(screen)
# 更新屏幕
pygame.display.flip()
# 控制帧率
pygame.time.Clock().tick(60)
pygame.quit
点赞0
评论
看看
alexander-pastukhov.github.io/writing-games-with-python-and-psychopy/whack-a-mole.html
点赞0
评论