首页 > 编程知识 正文

python迷宫小游戏代码(python超简单小游戏代码)

时间:2023-05-03 06:52:48 阅读:932 作者:3928

早上逛某网站首页就见到这么一篇教程。看了一下很有意思,就马上动手实现了一下。这个还是很适合小白拿来练一练手动,有源码分享,看看效果吧:

完整代码:

完整代码:完整代码:

# -*- coding: utf-8 -*-

# 1 -导入库

导入简介及安装

来自pygame.locals import *

导入数学

随机导入

# 2 -初始化游戏

键=[假,假,假,假]

playerpos=[100,100]

acc=[0,0]

箭头=[]

坏定时器=100

坏时间1=0

坏蛋=[[640,100]]

健康值=194

pygame.init()

宽度,高度=640,480

screen=pygame.display.set_mode(宽度、高度))

pygame.mixer.init()

# 3 -加载图像

玩家=pygame。图像。加载('资源/图像/花花公子。png ')

grass=pygame。图像。加载('资源/图像/草。png ')

城堡=pygame。图像。加载('资源/图像/城堡。png ')

箭头=pygame。图像。加载('资源/图像/项目符号。png ')

坏人G1=侏儒怪。图像。加载('资源/图像/坏人。png ')

游戏结束=pygame。图像。加载('资源/图像/游戏结束。png ')

你赢了=pygame。图像。加载('资源/图像/你赢了。png ')

健康吧=pygame。图像。加载('资源/图像/健康栏。png ')

健康=pygame。图像。加载('资源/图像/运行状况。png ')

badguyimg=badguyimg1

# 3.1 -加载音频

命中=pygame。混合器。声音('资源/音频/爆炸。wav’)

敌人=pygame.mixer.Sound('资源/音频/敌人。wav’)

shot=pygame。混合器。声音('资源/音频/镜头。wav’)

hit.set_volume(0.05)

敌人。设定_体积(0.05)

开枪了。set _ volume(0.05)

pygame。混合器。音乐。load(' resources/audio/mooth。wav’)

pygame.mixer.music.play(-1,0.0)

pygame。混合器。音乐。set _ volume(0.25)

# 4 -继续循环

运行=1

exitcode=0

运行:时

badtimer-=1

# 5 -在再次绘制之前清除屏幕

screen.fill(0)

# 6 -绘制屏幕元素

对于范围内的x(宽度/草。get _ width()1):

对于范围内的y(高度/草。get_height() 1):

screen.blit(草,(x*100,y*100))

screen.blit(castle,(0,30))

screen.blit(castle,(0,135))

screen.blit(castle,(0,240))

screen.blit(castle,(0,345))

#screen.blit(播放器,(100,100))

#screen.blit(播放器、playerpos)

position=pygame.mouse.get_pos()

angle=math.atan2(位置[1]-(playerpos[1] 32),positio

n[0]-(playerpos[0]+26)) playerrot = pygame.transform.rotate(player, 360-angle*57.29) playerpos1 = (playerpos[0]-playerrot.get_rect().width/2, playerpos[1]-playerrot.get_rect().height/2) screen.blit(playerrot, playerpos1) # 6.2 - Draw arrows for bullet in arrows: index=0 velx=math.cos(bullet[0])*10 vely=math.sin(bullet[0])*10 bullet[1]+=velx bullet[2]+=vely if bullet[1]<-64 or bullet[1]>640 or bullet[2]<-64 or bullet[2]>480: arrows.pop(index) index+=1 for projectile in arrows: arrow1 = pygame.transform.rotate(arrow, 360-projectile[0]*57.29) screen.blit(arrow1, (projectile[1], projectile[2])) # 6.3 - Draw badgers if badtimer==0: badguys.append([640, random.randint(50,430)]) badtimer=100-(badtimer1*2) if badtimer1>=35: badtimer1=35 else: badtimer1+=5 index=0 for badguy in badguys: # 6.3.1 - Attack castle badrect=pygame.Rect(badguyimg.get_rect()) badrect.top=badguy[1] badrect.left=badguy[0] if badrect.left<64: hit.play() healthvalue -= random.randint(5,20) badguys.pop(index) #6.3.2 - Check for collisions index1=0 for bullet in arrows: bullrect=pygame.Rect(arrow.get_rect()) bullrect.left=bullet[1] bullrect.top=bullet[2] if badrect.colliderect(bullrect): enemy.play() acc[0]+=1 badguys.pop(index) arrows.pop(index1) index1+=1 # 6.3.3 - Next bad guy if badguy[0]<-64: badguys.pop(index) badguy[0]-=7 index+=1 for badguy in badguys: screen.blit(badguyimg, badguy) # 6.4 - Draw clock font = pygame.font.Font(None, 24) survivedtext = font.render(str((90000-pygame.time.get_ticks())/60000)+":"+str((90000-pygame.time.get_ticks())/1000%60).zfill(2), True, (0,0,0)) textRect = survivedtext.get_rect() textRect.topright=[635,5] screen.blit(survivedtext, textRect) # 6.5 - Draw health bar screen.blit(healthbar, (5,5)) for health1 in range(healthvalue): screen.blit(health, (health1+8,8)) # 7 - update the screen pygame.display.flip() # 8 - loop through the events for event in pygame.event.get(): # check if the event is the X button if event.type==pygame.QUIT: # if it is quit the game pygame.quit() exit(0) if event.type == pygame.KEYDOWN: if event.key==K_w: keys[0]=True elif event.key==K_a: keys[1]=True elif event.key==K_s: keys[2]=True elif event.key==K_d: keys[3]=True if event.type == pygame.KEYUP: if event.key==pygame.K_w: keys[0]=False elif event.key==pygame.K_a: keys[1]=False elif event.key==pygame.K_s: keys[2]=False elif event.key==pygame.K_d: keys[3]=False if event.type==pygame.MOUSEBUTTONDOWN: shoot.play() position=pygame.mouse.get_pos() acc[1]+=1 arrows.append([math.atan2(position[1]-(playerpos1[1]+32),position[0]-(playerpos1[0]+26)),playerpos1[0]+32,playerpos1[1]+32]) # 9 - Move player if keys[0]: playerpos[1]-=5 elif keys[2]: playerpos[1]+=5 if keys[1]: playerpos[0]-=5 elif keys[3]: playerpos[0]+=5 #10 - Win/Lose check if pygame.time.get_ticks()>=90000: running=0 exitcode=1 if healthvalue<=0: running=0 exitcode=0 if acc[1]!=0: accuracy=acc[0]*1.0/acc[1]*100 else: accuracy=0 # 11 - Win/lose display if exitcode==0: pygame.font.init() font = pygame.font.Font(None, 24) text = font.render("Accuracy: "+str(accuracy)+"%", True, (255,0,0)) textRect = text.get_rect() textRect.centerx = screen.get_rect().centerx textRect.centery = screen.get_rect().centery+24 screen.blit(gameover, (0,0)) screen.blit(text, textRect) else: pygame.font.init() font = pygame.font.Font(None, 24) text = font.render("Accuracy: "+str(accuracy)+"%", True, (0,255,0)) textRect = text.get_rect() textRect.centerx = screen.get_rect().centerx textRect.centery = screen.get_rect().centery+24 screen.blit(youwin, (0,0)) screen.blit(text, textRect) while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0) pygame.display.flip()

源码已分享,这个小游戏特别适合小白拿来说练手哦,记得实现了给个点赞,关注,转发哦~

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。