Python Programmierung Spiel programmieren
Author D.Selzer-McKenzie
Video: http://youtu.be/ZRIeRLbPZ3U
Heute zeige ich Ihnen, wie Sie mit wenig Code ein Spiel
programmieren können.
Den SourceCode finden Sie im Forum
Hier nun der SourceCode:
import pygame, sys, cmath, math
import operator
import random
#########################
#Made By Selzer-McKenzie #
#This is just an example#
#########################
pygame.init()
grid=9
boxsize=32*2
totalbombs=9
window =
pygame.display.set_mode((grid*boxsize,grid*boxsize))
pygame.display.set_caption("Mienensucher Spiel")
myfont = pygame.font.SysFont("monospace",
boxsize//2)
background_color = (150,150,150)
c_sylver = (240,240,240)
c_lgrey = (200,200,200)
c_grey = (150,150,150)
c_dkgrey = (50,50,50)
c_green = (0,255,0)
c_black = (0,0,0)
c_red = (255,0,0)
c_yellow = (255,106,0)
c_blue = (0,0,255)
mouse_pos = pygame.mouse.get_pos()
diameter=5
mouse_pressed=0
menu=0;
#spritesssss con le timelines seta ultra
Cubo= pygame.Surface((boxsize,boxsize))
Cubo.fill(c_lgrey)
pygame.draw.rect(Cubo,c_grey,(0,0,boxsize,boxsize),boxsize//(boxsize//(boxsize//16)))
bubo= pygame.Surface((boxsize,boxsize))
bubo.fill(c_grey)
pygame.draw.rect(bubo,c_dkgrey,(0,0,boxsize,boxsize),boxsize//(boxsize//(boxsize//16)))
pygame.draw.line(Cubo,c_sylver,(boxsize//(boxsize//(boxsize//16)),boxsize-(boxsize//(boxsize//(boxsize//16)))),(boxsize-(boxsize//(boxsize//(boxsize//16))),boxsize-(boxsize//(boxsize//(boxsize//16)))),boxsize//(boxsize//(boxsize//16)))
Bomb= pygame.Surface((boxsize,boxsize))
Bomb.set_colorkey((0,0,0))
pygame.draw.circle(Bomb,c_dkgrey,(boxsize//2,boxsize//2),boxsize//4,0)
pygame.draw.line(Bomb,c_dkgrey,(boxsize//2+boxsize//8,boxsize//8),(boxsize//2,boxsize//2),2)
pygame.draw.circle(Bomb,c_yellow,(boxsize//2+boxsize//8,boxsize//8),boxsize//16,0)
pygame.draw.ellipse(Bomb,c_sylver,(boxsize//2,boxsize//3,boxsize//8,boxsize//10),0)
flag= pygame.Surface((boxsize,boxsize))
flaglist = [(boxsize//(boxsize//(boxsize//16))+(boxsize//16),boxsize//2),(boxsize//2,boxsize//8),(boxsize//2,boxsize//2)]
flag.set_colorkey((0,0,0))
pygame.draw.polygon(flag,c_red,flaglist,0)
pygame.draw.line(flag,c_dkgrey,(boxsize//2,boxsize//8),(boxsize//2,boxsize-(boxsize//8)),2)
label = [0 for i in range(10)]
label[0] = myfont.render("0", 1, (0,0,255))
label[1] = myfont.render("1", 1, (0,255,0))
label[2] = myfont.render("2", 1, (255,255,0))
label[3] = myfont.render("3", 1, (255,106,0))
label[4] = myfont.render("4", 1, (255,0,0))
label[5] = myfont.render("5", 1, (255,0,106))
label[6] = myfont.render("6", 1, (255,0,255))
label[7] = myfont.render("7", 1, (255,0,255))
label[8] = myfont.render("8", 1, (255,0,255))
label[9] = myfont.render("Click to play", 1,
(255,0,0))
#fine stprites
#metiamo le abbombazze ves
begin= [0 for i in range(2)]
Box= [ [ 0 for i in range(grid) ] for j in range(grid) ]
controllo= [0 for i in range(8)]
controllo[0]= [0,+1]
controllo[1]= [0,-1]
controllo[2]= [-1,0]
controllo[3]= [+1,0]
controllo[4]= [+1,+1]
controllo[5]= [-1,-1]
controllo[6]= [-1,+1]
controllo[7]= [+1,-1]
def inside(a,b):
if a>=0 and a<=grid-1 and b>=0 and b<=grid-1:
return True
else:
return False
def Findempty(XX,YY):
for z in range(8):
#print(XX+controllo[z][0],YY+controllo[z][1])
if inside(XX+controllo[z][0],YY+controllo[z][1])==True:
if Box[XX+controllo[z][0]][YY+controllo[z][1]][0]==0:
if Box[XX+controllo[z][0]][YY+controllo[z][1]][1]==0:
Box[XX+controllo[z][0]][YY+controllo[z][1]][0]=1
Findempty(XX+controllo[z][0],YY+controllo[z][1])
gameLoop=True
while gameLoop:
mouse_lpressed=0 #reset mouse button
mouse_rpressed=0 #reset mouse button
window.fill(background_color) #redraw the screen
mouse_pos = pygame.mouse.get_pos()#get mouse pos
for event in pygame.event.get():
if (event.type==pygame.QUIT):
gameLoop=False
if event.type == pygame.MOUSEBUTTONUP:
if event.button == -1:
mouse_lpressed= -1
elif event.button == -3:
mouse_rpressed= -1
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
mouse_lpressed=1
elif event.button == 3:
mouse_rpressed=1
#game logic veszszszszs
if menu==1 and mouse_lpressed==1:
for x in range(grid):
for y in range(grid):
if mouse_pos[0]> x*boxsize and mouse_pos[0]<
x*boxsize+boxsize:
if mouse_pos[1]> y*boxsize and mouse_pos[1]<
y*boxsize+boxsize:
if menu==1:
menu=2
begin[0]=x
begin[1]=y
#print(begin)
if menu==0 or menu==1:
for x in range(grid):
for y in range(grid):
window.blit(Cubo, (x*boxsize,y*boxsize))
if menu==0:
window.blit(label[9],(grid*(boxsize//4),grid*(boxsize//2-(boxsize//16))))
if mouse_lpressed==1:
menu=1
if menu==2:
if totalbombs>(grid*grid):
totalbombs=grid-1
pbombs=0
for x in range(grid):
for y in range(grid):
Box[x][y]=[0,0,0]
while pbombs < totalbombs:
#print(pbombs)
for x in range(grid):
for y in range(grid):
if pbombs >= totalbombs:
break
else:
if Box[x][y][1]!=100 and x!=begin[0] and y!=begin[1]:
if random.randrange(0,10+1)==5 and
random.randrange(0,10+1)==5:
Box[x][y][1]=100
pbombs+=1
#settingnearbynumbers
for x in range(grid):
for y in range(grid):
if Box[x][y][1]==100:
for r in range(8):
if inside(x+controllo[r][0],y+controllo[r][1])==True:
if Box[x+controllo[r][0]][y+controllo[r][1]][1]!=100:
Box[x+controllo[r][0]][y+controllo[r][1]][1]+=1
menu=3
if menu==3:
cellsopened=0
maxcellsopened=grid*grid-totalbombs
bombschecked=0;
if begin[0]!=-1 and begin[1]!=-1:
Box[begin[0]][begin[1]][0]=1
begin[0]=-1
begin[1]=-1
for x in range(grid):
for y in range(grid):
if mouse_lpressed==1 and Box[x][y][2]==0:
if mouse_pos[0]> x*boxsize and mouse_pos[0]<
x*boxsize+boxsize:
if mouse_pos[1]> y*boxsize and mouse_pos[1]<
y*boxsize+boxsize:
if Box[x][y][1]==100:
#print(x,y," Bomb")
Box[x][y][0]=1
print("BOOM!-Game Over")
gameLoop=False
else:
if Box[x][y][1]==0:
Findempty(x,y)
Box[x][y][0]=1
else:
Box[x][y][0]=1
#print(x,y,Box[x][y][1])
#print(mouse_rpressed)
if mouse_rpressed==1:
if mouse_pos[0]> x*boxsize and mouse_pos[0]<
x*boxsize+boxsize:
if mouse_pos[1]> y*boxsize and mouse_pos[1]<
y*boxsize+boxsize:
if Box[x][y][0]==0:
if Box[x][y][2]==0:
Box[x][y][2]=1
else:
Box[x][y][2]=0
#print(Box[x][y][2])
if Box[x][y][0]==0:
window.blit(Cubo, (x*boxsize,y*boxsize))
if Box[x][y][0]==1:
window.blit(bubo, (x*boxsize,y*boxsize))
if Box[x][y][1]!=100:
if Box[x][y][1]!=0:
window.blit(label[Box[x][y][1]],
(x*boxsize+(boxsize//2.5),y*boxsize+(boxsize//4.5)))
else:
window.blit(Bomb, (x*boxsize,y*boxsize))
if Box[x][y][2]==1:
if Box[x][y][0]==0:
Box[x][y][2]==0
if Box[x][y][2]==1:
window.blit(flag, (x*boxsize,y*boxsize))
#check win
if Box[x][y][0]==1:
cellsopened+=1
if Box[x][y][1]==100:
if Box[x][y][2]==1:
bombschecked+=1
if cellsopened==maxcellsopened:
if bombschecked==totalbombs:
print("Success-Game Over")
gameLoop=False
#print("Bombs:",bombschecked,"/",totalbombs)
#print("Cells:",cellsopened,"/",maxcellsopened)
#drawings
#end drawings
pygame.display.flip()
pygame.quit()
Keine Kommentare:
Kommentar veröffentlichen
Hinweis: Nur ein Mitglied dieses Blogs kann Kommentare posten.